[Java]Points in Invoking Constructors in Super Class - Java Programming Language

来源:互联网 发布:c语言学会了有什么用 编辑:程序博客网 时间:2024/04/30 03:52
• To invoke a parent constructor, you must place a call to super in the first line of the constructor.
• You can call a specific parent constructor by the arguments that you use in the call to super.
• If no this or super call is used in a constructor, then the compiler adds an implicit call to super() that calls the parent no argument constructor (which could be the default constructor).
  If the parent class defines constructors, but does not provide a no-argument constructor, then a compiler error message is issued.

Supposed that there is a super class called Employee

For example:

public class Manager extends Employee {private String department;public Manager(String name, double salary, String dept) {super(name, salary);department = dept;}public Manager(String name, String dept) {super(name);department = dept;}public Manager(String dept) { // This code fails: no super()department = dept;}//more Manager code...}


Now you can see what happened here.

原创粉丝点击