当前位置:网站首页>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 .
边栏推荐
- Adult adult adult
- Unicode查询的官方网站
- Solve the problem of VI opening files with ^m at the end
- Drop down refresh conflicts with recyclerview sliding (swiperefreshlayout conflicts with recyclerview sliding)
- 2.7 overview of livedata knowledge points
- It feels great to know you learned something, isn‘t it?
- Adult adult adult
- Kubectl_ Command experience set
- 【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
- 【ManageEngine】IP地址扫描的作用
猜你喜欢
随机推荐
TOGAF认证自学宝典V2.0
Swift return type is a function of function
Recovery of website address and method of Amazon account login two-step verification failure caused by mobile phone number becoming empty
Attack and defense world mobile--ph0en1x-100
Adult adult adult
启用MemCached的SASL认证
Kung Fu pays off, and learning is done
Enable SASL authentication for memcached
Ten workplace rules
Differences between initial, inherit, unset, revert and all
Apache Mina开发手册
Define a list, store n integers, and calculate the length, maximum value, minimum value and average value of the list
lambda与匿名内部类的区别
记录自己vulnhub闯关记录
2.7 overview of livedata knowledge points
Nodejs+express+mysql realizes login function (including verification code)
JVM内存模型
It feels great to know you learned something, isn‘t it?
elastic_ L01_ summary
Node.js: express + MySQL的使用