CONSTRUCTORS AND SUBCLASSING

Constructors are not inherited into subclasses; you must define each form of constructor that you require. A class constructor that has no constructors defined in the source is given exactly one constructor. This is the default constructor; it takes no arguments and is of public accessibility
Java insists that the object is initialized from the top of the class hierarchy downward:
Example:
class Test {
static int i = 0;
public static void main(String args[]) {
Terrier t = new Terrier(15);
System.out.println("Count: "+ i);
}
}
class Animal {
Animal (int increment) {
Test.i += increment;
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog (int increment) {
super(increment);
Test.i += increment;
System.out.println("Dog constructor");
}
}
class Terrier extends Dog {
Terrier (int increment) {
super(increment);
Test.i += increment;
System.out.println("Terrier constructor");
}
}
Result:
Animal constructor
Dog constructor
Terrier constructor
Count: 45

Calling Superclass Constructors
In the case of constructors you also use the keyword super() as with plain methods but you don't actually need to supply the name of the constructor, just super(arguments).
Example:
class Test {
public static void main(String args[]) {
Dog d = new Dog();
Animal a = new Animal(); // ERROR. No constructor
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");
}
}
class Dog extends Animal {
Dog () {
super(4,true,true);
System.out.println("Dog constructor");
}
}
Result:
Animal constructor
Dog constructor
Legs: 4
Mammal: true
Vertebrate: true
As you can see, if you provide a valid constructor, Java doesn't generate a default constructor anymore. To make the erroneous line valid, you should add the constructor to the Animal class:
Animal() {}
That also means that if you define a argument-based constructor in the superclass, you won't be able to create non-argument objects out of any of the subclasses. For example, suppose you want to add a Bird class to the previous example:
class Bird extends Animal {
Bird() {
System.out.println("Bird constructor");
}
}
You have to define Animal() {} in the Animal if you want to use this new class like this:
Bird b = new Bird();
This happens because Java will try to call Animal() before calling Bird(), and Animal() hasn't been defined. You can specify which constructor of the superclass will be called by using super:
class Bird extends Animal {
Bird() {
super(2,false,true);
System.out.println("Bird constructor");
}
}
Please note that super() must be the first statement in the Bird() constructor in order to be recognized by the compiler.

1 comments:

Anonymous said...

Spot on with this write-up, I actually feel this website needs
a lot more attention. I'll probably be returning to see more, thanks for the info!

Look at my site: anti cellulite treatment

Post a Comment