当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Xctf mobile--app3 problem solving

Record your vulnhub breakthrough record

Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!

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

The latest version of blind box mall thinkphp+uniapp

T430 toss and install OS majave 10.14

Detailed explanation of the most complete constraintlayout in history
![Sword finger offer04 Search in two-dimensional array [medium]](/img/c4/002c951f8d914aaea4f4133685ebd1.png)
Sword finger offer04 Search in two-dimensional array [medium]

最新版盲盒商城thinkphp+uniapp

【综合题】【数据库原理】
随机推荐
记录自己vulnhub闯关记录
studio All flavors must now belong to a named flavor dimension. Learn more
Swift5.7 扩展 some 到泛型参数
Solve the problem of VI opening files with ^m at the end
2020-10_ Development experience set
Adult adult adult
【综合题】【数据库原理】
Unicode查询的官方网站
Swift5.7 extend some to generic parameters
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?
Flinksql can directly create tables and read MySQL or Kafka data on the client side, but how can it automatically flow and calculate?
双链笔记·思源笔记综合评测:优点、缺点、评价
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
Self made pop-up input box, input text, and click to complete the event.
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
ImportError: No module named examples. tutorials. mnist
1-2 project technology selection and structure
node的ORM使用-Sequelize
Detailed explanation of the most complete constraintlayout in history
[embedded] - Introduction to four memory areas