当前位置:网站首页>If a parent class defines a parameterless constructor, is it necessary to call super ()?
If a parent class defines a parameterless constructor, is it necessary to call super ()?
2022-07-01 02:57:00 【A wild man on the verge of success】
Is it necessary to call super() if a Parent class defines a no-argument constructor?
on any condition , The first line of the constructor Must be a call to the constructor of the parent class . in other words , Every time you create an instance of a subclass , Will create a new instance of the parent class in memory .
Default constructor - Is a parameterless constructor , If the class does not provide any constructors , The compiler generates it automatically . There is no difference between the default constructor and the parameterless constructor that you can add to your class yourself .
If a parent class has a parameterless constructor ( Whether it is declared manually or provided by the compiler ), The compiler will be able to add super() a line .
If the parent class Only parameterization Constructor for , You must be in the constructor of the subclass Add calls manually super(name, salary) As the first line , Otherwise the code will not compile .
for example :
Person There is no claim to extend anything , But by default it extends Object class , There is one in this category Parameterless constructor . therefore , If you decide to create it manually Person Constructor for , you There is no need to add super();, The compiler will handle it .
stay Employee Class in the constructor ,super(); This line is optional .
But for Manager Category , To compile , Its first line Must call super(name, salary);. You must call the parameterized constructor of the parent class with the appropriate parameters .
public class Person {
public Person() {
super(); // optional, can be added by the compiler
}
}
public class Employee extends Person {
protected String name;
protected int salary;
public Employee(String name, int salary) {
super(); // optional, can be added by the compiler
this.name = name;
this.salary = salary;
}
}
public class Manager extends Employee {
public Manager(String name, int salary) {
super(name, salary); // this line is mandatory
}
}边栏推荐
- Poj-3486-computers[dynamic planning]
- Nacos configuration center tutorial
- A small document of JS method Encyclopedia
- Voici le programme de formation des talents de SHARE Creators!
- 【PR #5 A】双向奔赴(状压DP)
- 记一次服务部署失败问题排查
- [QT] add knowledge supplement of third-party database
- Magnetic manometer and measurement of foreign coins
- Introduction to kubernetes resource objects and common commands (II)
- robots. Txt restrict search engine inclusion
猜你喜欢

PCB defect detection based on OpenCV and image subtraction

Restcloud ETL WebService data synchronization to local
![[applet project development -- Jingdong Mall] classified navigation area of uni app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[applet project development -- Jingdong Mall] classified navigation area of uni app

Introduction and basic knowledge of machine learning
性能测试常见面试题

xxl-job使用指南

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

kubernetes资源对象介绍及常用命令(二)

【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)

Xception学习笔记
随机推荐
鼠标悬停效果十
Mnasnet learning notes
Poj-3486-computers[dynamic planning]
鼠标悬停效果九
PTA 1017
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
Introduction and basic knowledge of machine learning
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
RestCloud ETL WebService数据同步到本地
php批量excel转word
鼠标悬停效果八
Mouse over effect IV
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
Lenovo x86 server restart management controller (xclarity controller) or TSM method
Magnetic manometer and measurement of foreign coins
How do I hide div on Google maps- How to float a div over Google Maps?
Restcloud ETL practice data row column conversion
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
Scale SVG to container without mask / crop
Is it safe to open an account online in a small securities firm? Will my money be unsafe?