当前位置:网站首页>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 .
边栏推荐
- [ArcGIS user defined script tool] vector file generates expanded rectangular face elements
- 初入职场,如何快速脱颖而出?
- Adult adult adult
- ImportError: No module named examples. tutorials. mnist
- Use Tencent cloud IOT platform to connect custom esp8266 IOT devices (realized by Tencent continuous control switch)
- 云计算未来 — 云原生
- Xctf mobile--app1 problem solving
- Display time with message interval of more than 1 minute in wechat applet discussion area
- Keep learning swift
- 公纵号发送提示信息(用户微服务--消息微服务)
猜你喜欢
剑指Offer04. 二维数组中的查找【中等】
剑指Offer05. 替换空格
剑指Offer03. 数组中重复的数字【简单】
Social community forum app ultra-high appearance UI interface
Differences between initial, inherit, unset, revert and all
Alibaba is bigger than sending SMS (user microservice - message microservice)
initial、inherit、unset、revert和all的区别
Xctf mobile--app2 problem solving
Eureka self protection
Togaf certification self-study classic v2.0
随机推荐
Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
低代码平台国际化多语言(i18n)技术方案
Sword finger offer04 Search in two-dimensional array [medium]
Adult adult adult
Project video based on Linu development
[ManageEngine] the role of IP address scanning
(latest version) WiFi distribution multi format + installation framework
Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.
Redhat5 installing socket5 proxy server
ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
Day 1 of kotlin learning: simple built-in types of kotlin
The latest version of blind box mall thinkphp+uniapp
Tensorflow binary installation & Failure
剑指Offer06. 从尾到头打印链表
基于Linu开发的项目视频
1-1 token
Simple use and precautions of kotlin's array array and set list
Xctf mobile--app1 problem solving
Sqoop1.4.4原生增量导入特性探秘
Using swift language features, write a pseudo-random number generator casually