-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInheritance
More file actions
70 lines (67 loc) · 1.52 KB
/
Copy pathInheritance
File metadata and controls
70 lines (67 loc) · 1.52 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
//AIM: Write a program in Java to demonstrate single inheritance, multilevel inheritance and hierarchical inheritance.
//CODE:
class pin{ //it is parent class and print pincode
int gpin=380009;
int hpin=382350;
char o;
void pin(char o){
this.o=o;
if(o=='H'){
System.out.println(hpin);
}
else{
System.out.println(gpin);
}
}
}
//we have to use extends keyword in order to inherit functionality of other class
//simple inheritance
class India extends pin{ //it is child class of pin class
char o;
void indiaadd(char o){
this.o=o;
System.out.print(",India");
super.pin(o);
}
}
//multilevel
class Guj extends India{ //it is multilevel because this class inhertis India class which (India) is child class of pin class that's how there is multiple level of inheritance
char o;
void gujadd(char o){
this.o=o;
System.out.print(",Gujarat");
super.indiaadd(o);
}
}
class ahm extends Guj{ //multilevel
char o;
void ahmadd(char o){
this.o=o;
System.out.print(",Ahmedabad");
super.gujadd(o);
}
}
//hierarchical inheritance
class home extends ahm{
void homeadd(){
System.out.print("C-203,Surabhi Residency,Nikol");
super.ahmadd('H');
}
}
//hierarchical inheritance
class gpg extends ahm{
void gpgadd(){
System.out.print("GPG, opp. PRL ");
super.ahmadd('G');
}
}
public class inheritance_ex{
public static void main(String args[]){
System.out.println("\n\nCollege Address");
gpg obj1 = new gpg();
obj1.gpgadd();
System.out.println("\n\nHome Address");
home obj2 = new home();
obj2.homeadd();
}
}