-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSimpleInheritance.java
More file actions
50 lines (38 loc) · 1006 Bytes
/
SimpleInheritance.java
File metadata and controls
50 lines (38 loc) · 1006 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
43
44
45
46
47
48
49
50
package chapter8;
class A {
int i, j;
void showij() {
System.out.println("i and j " + i + " " + j);
}
}
class B extends A {
int k;
void showk() {
System.out.println("k = " + k);
}
void sum() {
System.out.println(" i + j + k : " + (i + j + k));
}
}
public class SimpleInheritance {
public static void main(String[] args) {
A superOb = new A();
B subOb = new B();
//The superclass may be used by itself
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents of superOb : ");
superOb.showij();
System.out.println();
//The subclass has access to all members of its superclass
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents of subOb : ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i , j and k in subOb : ");
subOb.sum();
}
}