-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_46_super.java
57 lines (47 loc) · 1.48 KB
/
ch_46_super.java
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
class Base1 {
int number;
Base1() {
System.out.println("I am a base constructor");
}
Base1(int x) {
number = 10;
System.out.println("I am an overloaded constructor of base with value of x as: " + x);
}
public void print() {
System.out.println("Base number : " + number);
}
}
class Derived1 extends Base1 {
int number;
Derived1() {
// super(0);
System.out.println("I am a derived class constructor");
}
Derived1(int x, int y) {
super(x);
number = 100;
System.out.println("I am an overloaded constructor of Derived with value of y as: " + y);
}
public void print() {
System.out.println("Base number (super): " + super.number);
System.out.println("Derived number : " + number);
}
}
public class ch_46_super {
public static void main(String[] args) {
// Base1 b = new Base1();
// Derived1 d = new Derived1();
Derived1 d = new Derived1(14, 9);
d.print();
}
}
/*
* When object is created for derived class, base class constructor will be called first(if exixts)
and then, derived class constructor will be called.
* "super" keyword is used to overcome method overriding
* we can use 'super' keyword to invoke base class methods or data members when
* they have same name as in derived. We can also use 'super()' which will
* invoke the constructor of base class, but not required as that is done
* implicitly.
*
*/