Java Constructor Inheritance

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.

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

Why are constructors not inherited in java?

In simple words, a constructor cannot be inherited, since in subclasses it has a different name (the name of the subclass).

class A {
A();
}

class B extends A{
B();
}

You can do only:

B b = new B();  // and not new A()

Methods, instead, are inherited with "the same name" and can be used.

As for the reason:
It would not have much sense to inherit a constructor, since constructor of class A means creating an object of type A, and constructor of class B means creating an object of class B.

You can still use constructors from A inside B's implementation though:

class B extends A{
B() { super(); }
}

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.

java Having multiple constructors for superclass

Well, there is something in Java to simplify your superclass. You can invoke another constructor in the same class using this();. So, instead of setting each variable for each constructor, use one variable-setting constructor and use this(); to pass it defaults. For your superclass, you could use these instead:

public Person(String name){
this(name, 18, "Atlanta");
}

public Person(String name, int age){
this(name, age, "Atlanta");
}

public Person(String name, int age, String homeTown){
this.name = name;
this.age = age;
this.homeTown = homeTown;
}

For the subclass, I'd create a private method called setVars which takes in the three variables you'd use: double avgGPA, int ID, and String[] classes. So, instead of setting them in each constructor, your class could look like this:

public Student(double avgGPA, int ID, String[] classes, String name){
super(name);
setVars(avgGPA, ID, classes);
}

public Student(double avgGPA, int ID, String[] classes, String name, int age){
super(name, age);
setVars(avgGPA, ID, classes);
}

public Student(double avgGPA, int ID, String[] classes, String name, int age, String homeTown){
super(name, age, homeTown);
setVars(avgGPA, ID, classes);
}

private void setVars(double avgGPA, int ID, String[] classes) {
this.avgGPA = avgGPA;
this.ID = ID;
this.classes = classes;
}

I think that's about as efficient as you'd get, unless you want to create a static initialization method as QueenSvetlana's answer recommended.

Java: Inherit constructor

You can't. If you want to have a parameterized constructor in your base class - and no no-argument constructor - you will have to define a constructor (and call super() constructor) in each of descendant classes.



Related Topics



Leave a reply



Submit