forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructorCall.java
More file actions
42 lines (33 loc) · 915 Bytes
/
Copy pathConstructorCall.java
File metadata and controls
42 lines (33 loc) · 915 Bytes
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
package methods.constructor;
/**
* First line in a constructor is implicit call (if not explicitly written) to own other constructors or parent class's constructor
* If parent class doesn't have default constructor, child constructor must EXPLICITLY call parent's non default constructor
*/
class Animal { // Does not have default constructor
Animal(int i) { }
Animal(boolean b) { }
}
class Dog extends Animal {
//Dog (){} // DOES NOT COMPILE
Dog() {
this(5); //super(6); // Mandatory call to parent's constructor
}
Dog(int a) {
super(true); // Mandatory call to parent's constructor
}
}
// Below 3 classes are equivalent because compiler converts them too last one
class Pet1 {
}
class Pet2 {
Pet2() {}
}
class Pet3 {
Pet3(){
super();
}
}
public class ConstructorCall {
public static void main(String[] args) {
}
}