当前位置:网站首页>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
}
}
边栏推荐
猜你喜欢
性能测试常见面试题
Restcloud ETL WebService data synchronization to local
记一次服务部署失败问题排查
Share Creators萌芽人才培养计划来了!
Multithreaded printing
Mnasnet learning notes
Record a service deployment failure troubleshooting
Sampling Area Lights
Const and the secret of pointers
[machine learning] vectorized computing -- a must on the way of machine learning
随机推荐
A small document of JS method Encyclopedia
Xception learning notes
[applet project development -- JD mall] uni app commodity classification page (Part 2)
Huawei operator level router configuration example | configuration static VPLS example
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
使用ipmitool配置X86服务器的BMC网络和用户信息
Restcloud ETL data realizes incremental data synchronization through timestamp
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
Mouse over effect V
[exsi] transfer files between hosts
Nacos configuration center tutorial
Dart training and sphygmomanometer inflation pump power control DPC
[PR # 5 A] two way running (state pressure DP)
# 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
Gartner研究:在中国,混合云的采用已成为主流趋势