0% found this document useful (0 votes)
130 views3 pages

Non-Default Constructors Are Not Inherited

Constructors are not inherited by default in Java because it would make the compiler's job of creating implicit constructors much more difficult. Non-default constructors must be explicitly called using the super keyword. Destructors are also not inherited since Java does not support destructors.

Uploaded by

ginger227
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views3 pages

Non-Default Constructors Are Not Inherited

Constructors are not inherited by default in Java because it would make the compiler's job of creating implicit constructors much more difficult. Non-default constructors must be explicitly called using the super keyword. Destructors are also not inherited since Java does not support destructors.

Uploaded by

ginger227
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Why are constructors/descructors not inherited?

<non-default constructor is not inherited>


In one weak sense, Java constructors are inherited.
If you had the following code

class A {
public A() {
System.out.println("In A ctor");
}
}

class B extends A {
public B() {
System.out.println("In B ctor");
}

public static void main(String [] args) {


B obj;
obj = new B();
}
}

you would see that it prints first "In A ctor" and


then "In B ctor". So, the B constructor implicitly
invokes the A constructor. This is the only form of
constructor inheritance in Java.

In all other senses, Java constructors are not inherited.


If you had this other code:

class A {
protected int x;
public A(int v) {
System.out.println("In A ctor");
x = v;
}
}

class B extends A {
private float z;
public static void main(String [] args) {
B obj;
obj = new B(3);
}
}
then you'd get a compiler error when you tried to
compile it. Why? Because when the compiler tries
to create an implicit constructor for B and cannot
find a corresponding constructor of A to call.

Now, why did the designers of Java do it this way?


Because they realized that to allow constructors to be
fully inherited would make the compiler's job MUCH
harder. Think about it, using the example above.
When the compiler compiles class B, it must create an
implicit constructor that initialize the object and gives
an initial value of 0.0f to private member variable z.
How could it do this if we allowed inheritance for A's
constructor?

As for destructors, there is no inheritance of


destructors because Java does not have destructors.

<non-default constructor is not inherited>


Modified version make the compilation successful.
class A {
protected int x;
public A(int v) {
System.out.println("In A ctor");
x = v;
}
}

public class B extends A {


private float z;
public static void main(String [] args) {

B obj;
obj = new B(3);
}
B(int i)
// because call "new B(3)" here, constructor is not inherited,
// must pass the appropriate parameters to match up with the version
// of the parental constructor you require., and "super(i)" is used to
// call the single integer version of the constructor in the base class.
{
super(i);
}
}

class Person {
String name = "No name";
//public Person(){};
public Person(String nm) {
name = nm;
}
}

class Employee extends Person {


String empID = "0000";
public Employee(String id) {
super("No name"); //constructor is not inherited by default, because
// Employee is subclass of Person, it must call super class b4 it can
initiate subclass
// therefore, super is needed here, or compile error saying
//"Implicit super constructor Person() is undefined. Must explicitly
invoke another constructor"
// if no super("No name"); specified here,
// then a default Person constructor must be explicitly defined,
because Compiler implicitly
// insert a default constructor in the subclass, and calls default
constructor in superclass and
// if could not find one, it generate compile error.
empID = id;
}
}

public class EmployeeTest {


public static void main(String[] args) {

Employee e = new Employee("4321");


System.out.println(e.empID);
}
}

You might also like