当前位置:网站首页>Chapter VI succession
Chapter VI succession
2022-07-25 16:53:00 【Take charge of [email protected]】
1、 The basic syntax of inheritance
**【 brief introduction 】:** stay Java Use in extends Keyword to indicate that one class inherits another class .
【 grammar 】:
public class Sub extends Base{
}
【 Case presentation 】:
public class Base {
public int a;
private int b;
int c;
protected int sum(int a, int b) {
return a + b;
}
}
public class Sub extends Base {
public void sub() {
a = 2;
b = 2;
c = 2;
sum(1, 2);
}
}
【 Graphical error 】:
【 matters needing attention 】:
- When Sub and Base When in the same package ,Sub Class inheritance Base Class public、protected、 And the member variables and methods of the default access level .
- When Sub and Base When not in the same package ,Sub Class inheritance Base Class public、protected Access level member variables and methods .
- A class can only have one direct parent class , Cannot inherit multiple parent classes directly at the same time , But you can indirectly inherit multiple parent classes .
- Each class inherits by default Object class .
- The constructor of a parent class cannot be inherited by a child class ( Constructor must be the same as class name )
2、 Method overloading (Overload)
【 Application scenarios 】: There are many ways to realize the function of class , Choose that way , Depends on the parameters given by the caller .
【 Meet the conditions of overload 】:
- Same method name
- Parameter types in methods 、 Number 、 At least one item in the order is different
- The return value types of methods can be different .
- The modifiers of methods can be different
3、 Methods cover (Override)
【 Application scenarios 】: When the method in the parent class does not meet the requirements of the child class , You need to override the methods in the parent class .
【 Method covers the constraints that are satisfied 】:
- Name of subclass method 、 Parameter signature 、 And return value types must be the same as the parent class .
【 Correct code demonstration 】:
public class Base {
public void method() {
}
}
public class Sub extends Base {
public void method() {
}
}
【 error analysis 】:
- A subclass method cannot reduce the access rights of a parent method .( reason : Conflicts with polymorphic mechanisms .)
【 Error code demonstration 】:
public class Base {
public void method() {
}
}
public class Sub extends Base {
private void method() {
// Subclasses narrow the access rights of the methods of the parent class
}
}
【 error analysis 】:
- Subclass methods cannot throw more exceptions than the parent class .( Cause program to terminate abnormally )
- Method overrides only exist between subclasses and superclasses , Methods in the same class can only be overloaded and cannot be overridden .
- A static method of a parent class cannot be overridden as a non static method by a child class .
【 Error code demonstration 】:
public class Base {
public static void method() {
}
}
public class Sub extends Base {
public void method() {
// Compilation error
}
}
【 error analysis 】:
- Subclasses can define static methods with the same name as the static methods of the parent class , In order to hide the static method of the parent class in the subclass .
【 Correct code demonstration 】:
public class Base {
public static void method() {
}
}
public class Sub extends Base {
public static void method() {
}
}
- Private methods of a parent class cannot be overridden by subclasses
- Abstract methods of a parent class can be overridden by subclasses in two ways (1、 Abstract methods that implement the parent class 2、 Redeclare the abstract methods of the parent class )
【 Correct code demonstration 】:
public abstract class Base {
abstract void method1();
abstract void method2();
}
public abstract class Sub extends Base {
// Realize and expand access
public void method1() {
}
// Redeclare the abstract methods of the parent class , It didn't happen
public abstract void method2();
}
- Non abstract methods of the parent class can be overridden as abstract methods .
public class Base {
public void method() {
}
}
public abstract class Sub extends Base {
public abstract void method();
}
4、 The difference between method coverage and method overloading
【 The same thing 】:
- All require the method to have the same name
- Can be used between abstract methods and non abstract methods
【 Difference 】:
- Method coverage requires that parameter signatures must be consistent , Method overloading requires that parameter signatures must be inconsistent
- Method override requires that the return value type must be consistent , Method overloading has no limit
- Method overrides occur between subclasses and superclasses , Method overloading is used for all methods in the same class
- Method coverage has certain restrictions on the access rights of methods and the exceptions thrown , Method overloading has no restrictions
- Methods of a parent class can only be overridden by subclasses once , Overloading can be repeated
5、super keyword
- super Keyword indicates the parent of the current subclass
- super Keyword can be used to access the constructor of the parent class , The default subclass will access the parameterless constructor of the parent class .
- Access non private fields and methods of the parent class ( The method here can only be non static )
6、 polymorphic
【 Definition 】:
Receive subclass objects with parent type or parent interface . That is, multiple states of an object at runtime .
【 Precautions for use 】:
- For variables of reference type ,Java The compiler will process according to the type it declares . for example : If you receive a subclass object with a parent type , When you need to access the unique attributes in the subclass , You must cast the object to a subclass type , Otherwise, compile and report an error .
- For variables of reference type , Runtime 、Java The virtual opportunity is handled according to the object it actually refers to .
- In a runtime environment , When accessing the methods and properties of the referenced object by referencing type variables ,Java The virtual machine adopts the following binding rules :
- The instance method is bound to the method of the object actually referenced by the reference variable , This kind of binding belongs to dynamic binding , Because it is run-time by Java Dynamically determined by the virtual machine .
- The static method is bound to the method of the type declared by the reference variable , This kind of binding belongs to static binding , Because actually binding has been done in the compilation stage .
- Member variables ( Including static variables and instance variables ) And the member variable of the type declared by the reference variable
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251650542758.html
边栏推荐
- Two methods of importing sqllite table from MySQL
- From digitalization to intelligent operation and maintenance: what are the values and challenges?
- 中国芯片自给率大幅提升,导致外国芯片库存高企而损失惨重,美国芯片可谓捧起石头砸自己的脚...
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- Outlook 教程,如何在 Outlook 中搜索日历项?
- Hcip notes 11 days
- 【obs】转载:OBS直播严重延迟和卡顿怎么办?
- Rosen's QT journey 99 QML table control tableview
- Getting started with easyUI
- Unity is better to use the hot scheme Wolong
猜你喜欢
![[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart](/img/73/2b6fe0cf69fa013894abce331e1386.png)
[mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart

After 20 years of agitation, the chip production capacity has started from zero to surpass that of the United States, which is another great achievement made in China

城市燃气安全再拉警钟,如何防患于未“燃”?

【目标检测】YOLOv5跑通VOC2007数据集(修复版)

2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part5-完结):信息检索与结果组装

152. 乘积最大子数组

WPF implements user avatar selector

Outlook 教程,如何在 Outlook 中搜索日历项?

数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
随机推荐
自定义mvc项目登录注册和树形菜单
3D semantic segmentation - PVD
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
[redis] redis installation
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
easyui入门
Fudan University EMBA peer topic: always put the value of consumers in the most important position
MySQL视图
IAAs infrastructure cloud cloud network
Dynamic planning topic record
【云驻共创】探秘GaussDB如何助力工商银行打造金融核心数据
柏睿数据加入阿里云PolarDB开源数据库社区
微信公众号开发之消息的自动回复
2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
HCIP笔记十一天
IaaS基础架构云 —— 云网络
使用Huggingface在矩池云快速加载预训练模型和数据集
一百个用户眼中,就有一百个QQ
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
搜狗批量推送软件-搜狗批量推送工具【2022最新】