Spaces:
Running
Running
File size: 4,183 Bytes
2ac5973 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
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
|