OVERLOADING CONSTRUCTORS 1

Overloading constructors behave just like overloading methods. When you have different constructors and want to call one version from another, you use the keyword this.

Example:

class Test {

public static void main(String args[]) {

Animal d = new Animal(true);

System.out.println("Legs: "+ d.legs);

System.out.println("Mammal: "+ d.mammal);

System.out.println("Vertebrate: "+ d.vertebrate);

}

}

class Animal {

int legs = 0;

boolean mammal = false;

boolean vertebrate = false;

Animal (int l, boolean m, boolean v) {

legs = l;

mammal = m;

vertebrate = v;

System.out.println("Animal constructor - all arguments");

}

Animal (boolean m) {

this(4,m,true);

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

}

}

Result:

Animal constructor - all arguments

Animal constructor - mammal argument

Legs: 4

Mammal: true

Vertebrate: true

The keyword this must also be the first sentence of the constructor. Thus you can't use super() and this() at the same time in the same constructor. However, super() is implicitly called even if you don't use it because the parents of the class are always initialised first and before the body of the constructor.

Example #1:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

}

class Reptile extends Animal {

Reptile (int l) {

legs = l;

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

}

}

Result:

Reptile constructor - leg argument

The compiler calls the default constructor for Animal and then the argument version of Reptile. Let's define our own default constructor just to be sure.

Example #2:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

Animal() {

System.out.println("Animal constructor - default");

}

}

class Reptile extends Animal {

Reptile (int l) {

legs = l;

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

}

}

Result:

Animal constructor - default

Reptile constructor - leg argument

Oops! We should also be able to define the number of legs for all animals, not only reptiles.

Example #3:

class Test {

public static void main(String args[]) {

Reptile r = new Reptile(4);

}

}

class Animal {

int legs = 0;

boolean cold_blood = false;

Animal() {

System.out.println("Animal constructor - default");

}

Animal (int l) {

legs = l;

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

}

}

class Reptile extends Animal {

Reptile (int l) {

legs = l;

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

}

}

Results:

Animal constructor - default

Reptile constructor - leg argument

Java keeps calling the default constructor. If we take it out maybe Java will try to use the remaining method Animal (int l):

Example #4:

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) {

legs = l;

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

}

}

Result:

ERROR. constructor Animal () not found

So what happened?. We have said before that if at least one constructor is defined, Java doesn't provide one by default. The error occurs because Reptile(int l) will call Animal() before the body of the constructor is executed as if a super() was there. We don't need to get back to our Animal() constructor. We only need to customize super() which is currently implicit:

0 comments:

Post a Comment