Home > Software design >  Did the user defined no-argument constructor in Base class is considered default constructor when in
Did the user defined no-argument constructor in Base class is considered default constructor when in

Time:02-12

we know that when the Base class define constructors the first statement in the child class constructor should be a call to the Base class constructor through super(), unless if the base class did not define any constructor so java default no-argument constructor will take place

the question is what if the Base class define a no argument constructor is that considred default constructor and it is not mendatory to call super() in the Child class, or this will be considred user-defined consructor and an explicit call is required

example

class Parent {

    Parent(){
        system.out.println("Hi ");
}

class Child extends{
      Child(){
            super()// is this mendatory or java will provide it

} }

class Child extends{
// and if we write Child like this it will compile like default or not       
}
}

CodePudding user response:

At any conditions, the first line of the constructor has to be a call to the constructor of a parent class. I.e. every time you are creating an instance of a Child class before it will happen a new instance of a Parent class will be also created in memory.

Default constructor - is a no-argument constructor that the compiler generates automatically if the class doesn't provide any constructor. There's no difference whatsoever between the default constructor the one that you can add to a class yourself.

If a Parent class has a no-argument constructor (doesn't matter whether it is declared manually or provided by the compiler) compiler will be able to add the line super() on your behalf.

If there are only parameterized constructors in the Parent class you have to add the call super(name, salary) as the first line in the Child's constructor manually, or else the code will not compile.

Take a look at the classes below.

Person doesn't declare to extend anything but by default, it extends the Object class, whit has a no-argument constructor. So if decide to create Person's constructor manually you don't have to add super();, the compiler will take care of it.

The thing with the constructor of Employee class, the line super(); is optional.

But for the Manager class in order to compile it has to have the call super(name, salary); as its first line. You have to invoke a parameterized constructor of the parent class with the appropriate parameters yourself.

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
    }
}

CodePudding user response:

All classes in Java extends from the class object. When you define no constructor in a class it calls its super.In class Parent it would call the class object , but if you do it in child it would call Parent because Child extends from Parent. Super() is used when you want to do what your parent class does and something else.

  • Related