当前位置:网站首页>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 .
边栏推荐
- temp
- Record your vulnhub breakthrough record
- Do you feel like you've learned something and forgotten it?
- 剑指Offer07. 重建二叉树
- What is more elegant for flutter to log out and confirm again?
- Low code platform international multilingual (I18N) technical solution
- Project video based on Linu development
- Detailed explanation of the most complete constraintlayout in history
- The difference between lambda and anonymous inner class
- context. Getexternalfilesdir() is compared with the returned path
猜你喜欢

Differences between initial, inherit, unset, revert and all

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

剑指Offer03. 数组中重复的数字【简单】

2.8 overview of ViewModel knowledge

2.7 overview of livedata knowledge points

并网-低电压穿越与孤岛并存分析

Eureka自我保护

idea将web项目打包成war包并部署到服务器上运行

Implement verification code verification

The future of cloud computing cloud native
随机推荐
Swift5.7 扩展 some 到泛型参数
2.6 preliminary cognition of synergetic couroutines
Swift return type is a function of function
Is it safe to open an account for online stock speculation? Who can answer
JVM内存模型
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
(latest version) WiFi distribution multi format + installation framework
写一个简单的nodejs脚本
电压环对 PFC 系统性能影响分析
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
OpenStack节点地址改变
lambda与匿名内部类的区别
2020-09_ Shell Programming Notes
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
How to convert a decimal number to binary in swift
Xctf mobile--rememberother problem solving
The latest version of blind box mall thinkphp+uniapp
【ManageEngine】IP地址扫描的作用
Differences between initial, inherit, unset, revert and all