当前位置:网站首页>Express abstract classes and methods
Express abstract classes and methods
2022-07-03 12:45:00 【Persia_ Pepper】
The relationship between abstract classes and abstract methods :
Abstract classes can define 0-n Abstract methods ;
The role of abstract classes :
The main role of abstract classes , Is to create a generic template for subclasses ;
Subclasses can be developed on the basis of this template , This is also in line with the application in the development of some enterprises , First override the abstract method in the parent class , The latter subclass can extend its own content ;
Abstract class design also avoids the randomness of design , Through abstract classes , The design of subclasses will become relatively strict , Limit to a certain extent
Code
According to every comment I write , Try tapping one by one , You know the limitations of abstract method definitions
//4. If a method in a class is an abstract method , Then this class should also become an abstract class .
//5. An abstract class can have 0-n Abstract methods
public abstract class Person {
//1. In a class , There will be a kind of method , Subclasses are very satisfied with this method , No need to rewrite , Use it directly
public void eat(){
System.out.println(" I'm so hungry for a meal ");
}
//2. In a class , There will be a kind of method , Subclasses are never satisfied with this method , This method will be overridden .
//3. Remove the method body of a method , Then be abstract modification , So this method becomes an abstract method
public abstract void say();
public abstract void sleep();
}
//6. Abstract classes can be inherited by other classes :
//7. A class inherits an abstract class , Then this class can become an abstract class
//8. Generally, subclasses will not be added abstract modification , It is common for subclasses to override abstract methods in the parent class
//9. Subclass inherits abstract class , You have to rewrite all the abstract methods
//10. If the subclass does not override all the abstract methods of the parent class , Then the subclass can also become an abstract class .
class Student extends Person{
@Override
public void say() {
System.out.println(" I'm from Northeast , I like speaking Northeast Dialect ..");
}
@Override
public void sleep() {
System.out.println(" Northeast people like sleeping on Kang ..");
}
}
class Demo{
// This is a main Method , It's the entrance to the program :
public static void main(String[] args) {
//11. Create objects of abstract classes :--> Abstract classes cannot create objects
//Person p = new Person();
//12. Create subclass objects :
Student s = new Student();
s.sleep();
s.say();
//13. Polymorphic writing : A parent class reference only wants child class objects :
Person p = new Student();
p.say();
p.sleep();
}
}
Interview questions
(1) Abstract class cannot create object , So whether there is a constructor in the abstract class ?
There must be a constructor in an abstract class . The role of the constructor When initializing objects for subclasses, first super Call the constructor of the parent class .
(2) Whether abstract classes can be final modification ?
Can not be final modification , Because the original intention of abstract class design is for subclass inheritance . If by final Modified this abstract class , There is no inheritance , There are no subclasses .
边栏推荐
- 最新版抽奖盲盒运营版
- 基于同步坐标变换的谐波电流检测
- [download attached] password acquisition tool lazagne installation and use
- Use bloc to build a page instance of shutter
- 2.8 overview of ViewModel knowledge
- Is it OK to open an account for online stock speculation? Is the fund safe?
- The solution to change the USB flash disk into a space of only 2m
- elastic_ L01_ summary
- Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
- Sword finger offer05 Replace spaces
猜你喜欢

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Eureka自我保护

公纵号发送提示信息(用户微服务--消息微服务)

(latest version) WiFi distribution multi format + installation framework

Self made pop-up input box, input text, and click to complete the event.

What is more elegant for flutter to log out and confirm again?

Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)

If you can't learn, you have to learn. Jetpack compose writes an im app (I)

基于同步坐标变换的谐波电流检测

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
随机推荐
公纵号发送提示信息(用户微服务--消息微服务)
写一个简单的nodejs脚本
Xctf mobile--rememberother problem solving
2.7 overview of livedata knowledge points
Adult adult adult
Official website of Unicode query
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
idea将web项目打包成war包并部署到服务器上运行
ImportError: No module named examples. tutorials. mnist
Sword finger offer03 Repeated numbers in the array [simple]
阿里大于发送短信(用户微服务--消息微服务)
剑指Offer06. 从尾到头打印链表
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Swift return type is a function of function
剑指Offer03. 数组中重复的数字【简单】
The difference between lambda and anonymous inner class
Cloud Computing future - native Cloud
[download attached] password acquisition tool lazagne installation and use
Everything comes to him who waits
剑指Offer05. 替换空格