-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh10_44_Inheritance.java
More file actions
59 lines (47 loc) · 1.71 KB
/
Ch10_44_Inheritance.java
File metadata and controls
59 lines (47 loc) · 1.71 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
class Base{ // Called Type
int a;
public int getA() {
System.out.print("Printing A: ");
return a;
}
public void setA(int a) {
this.a = a;
}
public void printText(){
System.out.println("I am learning Constructor!!");
}
}
// Now since we have already prepared a class for a , we will inherit properties from it to make a class for b.
// We will not copy paste because then we will have to change each property manually taking a lot of time
class InheritingBase extends Base{ // Called SubType
int b;
public int getB() {
System.out.print("Printing B: ");
return b;
}
public void setB(int b) {
this.b = b;
}
}
public class Ch10_44_Inheritance {
public static void main(String[] args) {
System.out.println();
// Inheritance - borrowing properties and methods from an existing class
/* Syntax -
class Inherited_Class_Name extends Old_Class_Name{
New Properties
} */
// Superclass -> Subclass
// Just by setting the properties , it will inherit all the printing material etc from the base.
InheritingBase pBase = new InheritingBase();
pBase.setA(5);
System.out.println(pBase.getA());
System.out.println();
// Even on making object of inherited class we can use commands of Base
// Because the basics are made from the Base class
// Important note: Even though you can access A from Inherited class , B cannot be accessed on Original class
// Because inherited class will contain support for A but original won't for B
pBase.setB(13);
System.out.println(pBase.getB());
}
}