-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.java
More file actions
154 lines (107 loc) · 4.64 KB
/
inheritance.java
File metadata and controls
154 lines (107 loc) · 4.64 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
Inheritance
// Inheritance is the mechanism by which one class acquires the properties and features of another class. The class that inherits the properties is called as a sub-class (child class) while the class from which the property is inherited is called as the super-class (parent class).
// A child class inherits properties of parent class with the help of extends keyword.
// Syntax:
// class childClass extends parentClass {
// //any code
// }
// Inheritance can be further divided into the following types:
// Single level
// Multi-level
// Hierarchical
// Multiple
// Hybrid
// Multiple and hybrid inheritance is not directly supported in java, instead it is achieved through the use of interfaces in java.
// i. Single Inheritance
// When a single class inherits the attributes and methods of another class, it is known as single inheritance.
// Example:
// class FundamentalForce {
// void Force() {
// System.out.println("There are four fundamental forces.");
// }
// }
// class Gravitational extends FundamentalForce {
// void Gravity() {
// System.out.println("Fruits fall to the ground due to gravitational Force.");
// }
// }
// class SingleInheritance {
// public static void main(String[] args) {
// Gravitational G = new Gravitational();
// G.Force();
// G.Gravity();
// }
// }
// Output:
// There are four fundamental forces.
// Fruits fall to the ground due to gravitational Force.
// In this example, we see how class Gravitational can inherit the method of Its parent class (FundamentalForce). This is an perfect example of parent-child relationship.
// ii. Multi-level Inheritance
// When a class 3 inherits attributes and methods from class 2 which in turn inherits its attributes and methods from class 1, it is called a multi-level inheritance.
// It forms a child-parent-grandparent (or a parent-child-grandchild) relationship. Meaning that child inherits from the parent while the parent inherits from the grandparent.
// Example:
// class NuclearForce extends FundamentalForce {
// void Nuclear() {
// System.out.println("Nuclear Forces are of two types;");
// System.out.println("Strong Nuclear Force");
// System.out.println("Weak Nuclear Force");
// }
// }
// class StrongNuclearForce extends NuclearForce {
// void Strong() {
// System.out.println("Strong Nuclear Force is responsible for the underlying stability of matter.");
// }
// }
// class MultilevelInheritance {
// public static void main(String[] args) {
// StrongNuclearForce st = new StrongNuclearForce();
// st.Force();
// st.Nuclear();
// st.Strong();
// }
// }
// Output:
// There are four fundamental forces.
// Nuclear Forces are of two types;
// Strong Nuclear Force
// Weak Nuclear Force
// Strong Nuclear Force is responsible for the underlying stability of matter.
// In this example, we see how class StrongNuclearForce inherits the method of NuclearForce which in turn inherits the method of FundamentalForce. This is a classic example of a child-parent-grandparent relationship.
// iii. Hierarchical Inheritance
// Hierarchical inheritance is when two or more classes inherit from a single class. This can be easily visualized as a parent with more than one child. Here each child can inherit the properties of a parent.
// Example:
// class FundamentalForce {
// void Force() {
// System.out.println("There are four fundamental forces.");
// }
// }
// class Gravitational extends FundamentalForce {
// void Gravity() {
// System.out.println("Fruits fall to the ground due to gravitational Force.");
// }
// }
// class Electromagnetic extends FundamentalForce {
// void Particles() {
// System.out.println("The electromagnetic force acts between charged particles");
// }
// }
// class HierarchicalInheritance {
// public static void main(String[] args) {
// System.out.println("Child 1:");
// Gravitational G = new Gravitational();
// G.Force();
// G.Gravity();
// System.out.println();
// System.out.println("Child 2");
// Electromagnetic em = new Electromagnetic();
// em.Force();
// em.Particles();
// }
// }
// Output:
// Child 1:
// There are four fundamental forces.
// Fruits fall to the ground due to gravitational Force.
// Child 2
// There are four fundamental forces.
// The electromagnetic force acts between charged particles