-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC14_Constructor_Inheritance.java
More file actions
75 lines (65 loc) · 1.6 KB
/
C14_Constructor_Inheritance.java
File metadata and controls
75 lines (65 loc) · 1.6 KB
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
// constructor
// constructor overloading
class Base1 {
Base1() {
System.out.println("I am a constructor");
}
Base1(int a) {
System.out.println("This is a constructor contains: " + a);
}
int x;
public int getx() {
return x;
}
public void setx(int x) {
this.x = x;
}
public void PrintMeB() {
System.out.println("I am in Base class");
}
}
class derived1 extends Base1 {
derived1() {
super(0);
System.out.println("This is a derived class constructor");
}
derived1(int a, int b) {
super(20);
System.out.println("This is a constructor contains: " + a + " " + b);
}
int y;
public int gety() {
return y;
}
public void sety(int y) {
this.y = y;
}
public void PrintMeD() {
System.out.println("I am in derived class");
}
}
class Ddrived extends derived1 {
Ddrived() {
System.out.println("This is a D derived class");
}
Ddrived(int a) {
super(10, 15);
System.out.println("This is a D derived class with: " + a);
}
}
public class C14_Constructor_Inheritance {
public static void main(String[] args) {
Base1 b = new Base1();
b.PrintMeB(); // use b
System.out.println();
derived1 a = new derived1();
a.PrintMeD(); // use a
System.out.println();
derived1 d = new derived1(4, 9);
d.PrintMeB(); // use d
System.out.println();
Ddrived c = new Ddrived(5);
c.PrintMeD(); // use c
System.out.println();
}
}