TurkuBasicOOPinJava / Week 5: Class hierarchies /11A. About parent class constructor++
KaiquanMah's picture
MULTIPLE CONSTRUCTORS, EMPTY CONSTRUCTOR
2ac5973 verified
raw
history blame
4.18 kB
TYPICALLY, METHOD OVERRIDING is used IN CONSTRUCTORS.
As an example, consider the class Account, which now has 3 different constructors.
This allows the client to easily create a creature even in a situation "where not all the data content is known":
class Account {
private String accnumber;
private String owner;
private double balance;
public Account(String accnumber, String owner, double balance) {
this.accnumber = accnumber;
this.owner = owner;
this.balance = balance;
}
public Account(String accnumber, String owner) {
this.accnumber = accnumber;
this.owner = owner;
this.balance = 0.0;
}
public Account() {
this.accnumber = "";
this.owner = "";
this.balance = 0.0;
}
}
Now an object can be created by calling any of the constructor versions:
public static void main(String[] args) {
Account acc1 = new Account();
Account acc2 = new Account("12345", "Randy Rich");
Account acc3 = new Account("54321", "Sally Student", 9.40);
}
In fact, whenever an entity is formed from a child class, a constructor is called from the parent class.
IF the programmer DOES NOT CALL the PARENT CLASS CONSTRUCTOR itself using the 'super' keyword,
Java AUTO CALLS an "empty" CONSTRUCTOR (i.e. a constructor method with NO PARAMS).
If there is no such constructor, the program gives an error.
The constructor's job is, among other things,
to ALLOCATE ENOUGH MEMORY for the object and
to INITIALISE the ATTRIBUTES of the object, and for this reason all parent class constructors must be called.
class Dog {
protected String name;
public Dog (String name) {
this.name = name;
}
}
class SuperDog extends Dog{
protected String superpower;
// Constructor throws an error now since there is no keyword super
// and parent class Dog does not contain a constructor with attribute "superpower"
public SuperDog(String name, String superpower) {
this.name = name;
this.superpower = superpower;
}
}
Program throws a translation error:
Implicit super constructor Dog() is undefined.
Must explicitly invoke another constructor
SOL1
The problem can be fixed either by calling a constructor yourself...
class SuperDog extends Dog{
protected String superpower;
public SuperDog(String name, String superpower) {
super(name);
this.superpower = superpower;
}
}
SOL2
...or by adding a constructor to the parent class that has no formal parameters.
class Dog {
protected String name;
//ADD EMPTY CONSTRUCTOR WO PARAMS
public Dog() {
name = "No name.";
}
public Dog(String name) {
this.name = name;
}
}
class SuperDog extends Dog{
protected String superpower;
// Now no error pops up, because parent class has
// a constructor you can call this way: super();
public SuperDog(String name, String supwerpower) {
this.name = name;
this.superpower = superpower;
}
}
Note that an "empty" constructor is also 'a class for which no constructor is defined'.
class Dog {
protected String name;
//NO CONSTRUCTOR
}
public class Dogtest {
public static void main(String[] args) {
// Works, because Java adds a "default constructor"
Dog dog = new Dog();
}
}
However, if the programmer adds a NEW OVERRIDING CONSTRUCTOR to the class itself, there is no longer an empty constructor.
So the next program will again give an error:
class Dog {
protected String name;
//CONSTRUCTOR
public Dog(String name) {
this.name = name;
}
}
public class Dogtest {
public static void main(String[] args) {
// Doesn't work, since own constructor
// deletes PREVIOUS/default constructor
// AND IN THE NEW USER'S OVERRIDING CONSTRUCTOR
// THERE IS NO EMPTY CONSTRUCTOR
// SO THERE WILL BE 'TRANSLATION ERROR'
Dog dog = new Dog();
}
}
Program throws a translation error:
Implicit super constructor Dog() is undefined.
Must explicitly invoke another constructor