OVERLOADING CONSTRUCTORS 2

Example #5:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

Animal (int l) {

legs = l;

System.out.println("Animal constructor - leg argument");

}

}

class Reptile extends Animal {

Reptile (int l) {

super(l);

System.out.println("Reptile constructor - leg argument");

}

}

Result:

Animal constructor - leg argument

Reptile constructor - leg argument

If the Reptile class hasn't the Reptile (int l) constructor it wouldn't be possible to initialise it with that argument as constructors are not inherited as methods. Something like this would also be invalid:

Reptile r = new Reptile();

Animal a = new Animal();

Animal() and Reptile() aren't defined. Java doesn't supply default constructors because one is already defined.

Because of this special behavior you must take special care with this() because a super() call is produced automatically:

Example #6:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

Animal (int l) {

legs = l;

System.out.println("Animal constructor - leg argument");

}

Animal (boolean b) {

cold_blood = b;

System.out.println("Animal constructor - blood argument");

}

}

class Reptile extends Animal {

Reptile (int l) {

this(true); // WATCH OUT!!!

System.out.println("Reptile constructor - leg argument");

}

Reptile (boolean b) {

cold_blood = b;

System.out.println("Reptile constructor - blood argument");

}

}

Result:

ERROR. constructor Animal () not found

There are two ways to solve this problem, we can add a Animal() constructor, or we can make sure that the proper Animal constructor gets called, like this:

Example #7:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

Animal (int l) {

legs = l;

System.out.println("Animal constructor - leg argument");

}

Animal (boolean b) {

cold_blood = b;

System.out.println("Animal constructor - blood argument");

}

}

class Reptile extends Animal {

Reptile (int l) {

this(true);

System.out.println("Reptile constructor - leg argument");

}

Reptile (boolean b) {

super(b);

System.out.println("Reptile constructor - blood argument");

}

}

You have to understand that each parent class will be initialised somehow and you must tell Java which constructors should be used if you don't want Java to insist on calling the default constructor all the time.

So, do not forget:

  • In a hierarchy of classes, Java calls the default constructor of each parent class before the body of the current constructor is executed. You cannot prevent Java from calling a constructor for each class but you can tell Java which constructor should be chosen by the parent class using the super() keyword.
  • Java provides a default constructor for each class if NO CONSTRUCTOR is defined by the programmer. If just one constructor is defined, Java doesn't put up a default constructor anymore.
  • Constructors are not inherited, you must define all the constructors again or reuse the functionality of a parent constructor using super().

1 comments:

Anonymous said...

I’m not that much of a online reader to be honest
but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back in the future. All the best

Feel free to surf to my page ... natural cellulite treatment

Post a Comment