当前位置:网站首页>What is an abstract class? How to define abstract classes?
What is an abstract class? How to define abstract classes?
2022-06-23 16:39:00 【User 7353950】
When defining a class , It is often necessary to define some member methods to describe the behavior characteristics of the class , But sometimes the implementation of these methods is uncertain . for example , In defining Animal Class time ,shout( ) This method is used to describe animal calls , But the sounds of different animals are different , So in shout( ) The method can not accurately describe the animal cry . For the situation described above ,Java Abstract methods are provided to meet this need . The abstract method is to use abstract Keyword decorated member method , Abstract methods do not need to implement the method body when they are defined . The definition format of the abstract method is as follows :
abstract void Method name ( Parameters );When a class contains abstract methods , This class must be abstract . Abstract classes are the same as abstract methods , You have to use abstract Keyword modification . The definition format of the abstract class is as follows :
abstract class Abstract class name {
Access permission return value type Method name ( Parameters ){
return [ Return value ] ;
Access right abstract Return value type abstract method name ( Parameters ) ;
// Abstract method , There is no method
}From the above format, you can find , Abstract classes have more abstract methods than ordinary classes , The composition of other places is basically the same as that of ordinary classes . The definition rules of abstract classes are as follows . (1) A class containing more than one abstract method must be an abstract class . (2) Both abstract classes and abstract methods use abstract Keyword declaration . (3) Abstract methods only need to be declared but not implemented . (4) If a class inherits an abstract class , Then the subclass must implement all abstract methods in the abstract class . Let's learn how to use abstract classes through a case , Such as file 4-10 Shown . file 4-10 Example10.java
// Defining abstract classes Animal
abstract class Animal(
// Define abstract methods shout ()
abstract void shout () ;
}
// Definition Dog Class inherits abstract class Animal
class Dog extends Animal {
// Implement abstract methods shout ()
void shout ()System.out.println (" Wang Wang ..... ;
}}
// Define test classes
public class Example10 {public static void main (String[] args) {Dog dog=new Dog(); // establish Dog class
dog. shout() ; // call dog Object's shout () Method
}
}In the file 4-10 in , The first 2~5 Lines of code declare an abstract class Animal, And in Animal Class declares an abstract method shout ( ); The first 9~11 Lines of code in subclasses Dog Implementing a parent class in Anima Abstract method of shout( ); The first 17 Line code calls... Through instantiated objects of subclasses shout( ) Method . Be careful : Use abstract Abstract methods decorated with keywords cannot use private modification , Because abstract methods must be implemented by subclasses , If used private Statement , The subclass cannot implement the method .
边栏推荐
- leetcode:面試題 08.13. 堆箱子【自頂而下的dfs + memory or 自底而上的排序 + dp】
- Generating binary search balanced tree [using tree recursion]
- golang gob实现网络数据的传输
- Identify and stop the process that's listening on port 8080 or configure this application
- SaaS cloud tool, a sharp tool for change under the industrial Internet
- How NPM contracts and deletes packages
- Golang write file code example
- Image reading: image open(ImgPath)
- 三分钟学会如何找回mysql密码
- ADB 按键名、按键代码数字、按键说明对照表
猜你喜欢

机器人方向与高考选专业的一些误区

Solution: in the verification phase, the first batch does not report errors, and the second batch reports CUDA exceeded errors

Matlab: how to know from some data which data are added to get a known number

元宇宙带来的社会结构和资本制度演变

2022九峰小学(光谷第二十一小学)生源摸底
![[solution] NPM warn config global ` --global`, `--local` are deprecated Use `--location=global`](/img/14/a2b3df1e8d0cf06ec96d6df9104a4a.png)
[solution] NPM warn config global ` --global`, `--local` are deprecated Use `--location=global`

Block, non block, multiplexing, synchronous, asynchronous, bio, NiO, AIO

stylegan3:alias-free generative adversarial networks

How did Tencent's technology bulls complete the overall cloud launch?

坚持五件事,带你走出迷茫困境
随机推荐
ADB key name, key code number and key description comparison table
Implementation of network data transmission by golang Gob
Safe and comfortable, a new generation of Qijun carefully interprets the love of the old father
ADC数字地DGND、模拟地AGND的谜团!
R language uses the image of magick package_ The scale function resizes the image. You can customize the scaling from the angle of width or height
R language plot visualization: plot visualization adds bar chart with error bars with plot in R
谈谈redis缓存击穿透和缓存击穿的区别,以及它们所引起的雪崩效应
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
Spin lock using CAS
Is your PCB ground wire right? Why should we have a master land?
Jmeter压力测试教程
数字经济加速落地,能为中小企业带来什么?
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的gt_sparkline函数以表格的形式可视化分组数据的线图(line plot)、包含分组类别、分组类别对应的数值
Counter attack by flour dregs: MySQL 66 question! Suggested collection
数学分析_证明_第1章:可数个可数集之并为可数集
[solution] NPM warn config global ` --global`, `--local` are deprecated Use `--location=global`
泰山OFFICE技术讲座:使用字体斜体的四种情形
Apache foundation officially announced Apache inlong as a top-level project
读书郎通过上市聆讯:平板业务毛利率走低,2021年利润同比下滑11%
golang gob实现网络数据的传输