-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritanceInJava.java
More file actions
41 lines (36 loc) · 850 Bytes
/
InheritanceInJava.java
File metadata and controls
41 lines (36 loc) · 850 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
class Base{
public int x;
public int getX() {
return x;
}
public void setX(int x){
System.out.println("I am in base and setting x now");
this.x = x;
}
public void printMe(){
System.out.println("I am a constructor");
}
}
class Derived extends Base {
public int y;
public int getY() {
return y;
}
public void setY(int y) {
System.out.println("I am in Derived and setting y now");
this.y = y;
}
}
public class InheritanceInJava{
public static void main(String[] args) {
// Creating an Object of base class
Base b = new Base();
b.setX(4);
System.out.println(b.getX());
// Creating an object of derived class
Derived d = new Derived();
d.setY(43);
System.out.println(d.getY());
// b.printMe();
}
}