-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_45_Inheritance.java
53 lines (44 loc) · 1.13 KB
/
cwh_45_Inheritance.java
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
package company;
class Base
{
int x;
void setX(int p)
{
System.out.println("\nI am in Base setting x.");
x = p;
}
int getX()
{
return x;
}
}
class Derived extends Base
{
int y;
void setY(int p)
{
System.out.println("\nI am in Derived setting y.");
y = p;
}
int getY()
{
return y;
}
}
public class cwh_45_Inheritance
{
public static void main(String[] args)
{
/* -Inheritance is used to borrow properties & methods from an existing class.
-Inheritance in Java is declared using keyword "extends".
-When a class inherits from a superclass, it inherits parts of superclass' methods and fields.
It cannot inherit some parts of super-class methods & fields, due to access modifiers.
-Java doesn't support multiple inheritance, i.e. two classes cannot be super-classes for a sub-class */
Base b = new Base();
b.setX(4);
System.out.println(b.getX());
Derived d = new Derived();
d.setX(5);
System.out.println(d.getX());
}
}