Default Constructors and Inheritance in Java

Inheritance in Java (default constructor)

If you specify at least one constructor in a class a default constructor is no longer created for you.

If you do not specify which parent constructor to call in your subclass the one that takes 0 parameters is used by default.

Since you added a constructor to class B that takes two integer parameters you now have to call it like this from in C as there is no longer a constructor that takes no parameters in class B:

public ClassC(int a, int b) {
super(a, b);
System.out.println("This is class C "+a+ "and"+ b );
}

Default constructors in Java

Use the super constructor:

public Bar(int a, double b, ...) {
super(a, b, ...);
}

Inheritance of constructors in java

Don't think of the constructor as creating the instance. Think of it as initializing the instance, with respect to that particular class.

So the initialization process looks something like:

  • Allocate memory
  • Initialize object from perspective of java.lang.Object
  • Initialize object from perspective of your.package.Superclass
  • Initialize object from perspective of your.package.Subclass

(Even though you start with a call to new Subclass(...), the superclass constructor body is executed first.)

The details of object initialization are given in JLS section 12.5.

Inheriting a constructor in java

So I want to know can a subclass inherit the constructor of the super
class?

a constructor cannot be inherited.

Moving on, I've managed to find the source which you're relating to at tutorialspoint.

It's important to note that the section under which it's mentioned is clearly titled as Invoking Superclass Constructor and it states:

If a class is inheriting the properties of another class, the subclass
automatically acquires the default constructor of the superclass. But
if you want to call a parameterized constructor of the superclass, you
need to use the super keyword as shown below.

super(values);

To put it simply, what this is trying to say is if the subclass constructor does not specify which superclass constructor to invoke then the compiler will automatically call the accessible no-args constructor in the superclass.

However, once there is a parameterized constructor within the super class you must invoke it as the first statement within the constructor of the derived class otherwise your program will not compile.

e.g

public class Super { ... }
public class Derived extends Super { ... } // good ! compiler puts in a public default constructor for you.

the code below will not compile and you should get an error stating:

there is no default constructor available in Super

public class Super {
public Super(int id) {...}
}
public class Derived extends Super {...}

meaning you'll need to invoke the Super class constructor explicitly as the first statement within the constructor of the Derived class, providing the necessary arguments, e.g.

public Derived(){ super(12);}

Java Constructor Inheritance

Suppose constructors were inherited... then because every class eventually derives from Object, every class would end up with a parameterless constructor. That's a bad idea. What exactly would you expect:

FileInputStream stream = new FileInputStream();

to do?

Now potentially there should be a way of easily creating the "pass-through" constructors which are fairly common, but I don't think it should be the default. The parameters needed to construct a subclass are often different from those required by the superclass.

Default access modifier of constructor in Inheritance

I did a little web search and came to know that default constructor's
access specifier is same as the access level of class

There is a difference between default constructor and the one which you've declared. The default constructor is the one which you don't declare inside the class.

The current one in your code is not a default constructor. And, it has default(package-private --- no explicit modifier) acccess as you've omitted any access modifier from it.

Which concept am I missing here ?

So, your class from other package is not able to find it, because of the limitation of default access.

Why default constructor is required in a parent class if it has an argument-ed constructor?

There are two aspects at work here:

  • If you do specify a constructor explicitly (as in A) the Java compiler will not create a parameterless constructor for you.

  • If you don't specify a constructor explicitly (as in B) the Java compiler will create a parameterless constructor for you like this:

    B()
    {
    super();
    }

(The accessibility depends on the accessibility of the class itself.)

That's trying to call the superclass parameterless constructor - so it has to exist. You have three options:

  • Provide a parameterless constructor explicitly in A
  • Provide a parameterless constructor explicitly in B which explicitly calls the base class constructor with an appropriate int argument.
  • Provide a parameterized constructor in B which calls the base class constructor

Abstract class java inheritance

Add the constructor to the child class and use super() inside the constructor.

example:

abstract class Parent{
private int x;
Parent(int x){
this.x = x;
}

}

abstract class Child extends Parent{
Child(int y){
super(y);
}

}

Now no compile errors :)



Related Topics



Leave a reply



Submit