Skip to content

Commit c2c9495

Browse files
Inheritance in Java
1 parent d9c4252 commit c2c9495

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Base{
2+
public int x;
3+
4+
public int getX() {
5+
return x;
6+
}
7+
8+
public void setX(int x) {
9+
System.out.println("I am in base and setting x now");
10+
this.x = x;
11+
}
12+
13+
public void printMe(){
14+
System.out.println("I am a constructor");
15+
}
16+
}
17+
18+
class Derived extends Base{
19+
public int y;
20+
21+
public int getY() {
22+
return y;
23+
}
24+
25+
public void setY(int y) {
26+
this.y = y;
27+
}
28+
}
29+
30+
public class cwh_45_inheritance {
31+
public static void main(String[] args) {
32+
// Creating an Object of base class
33+
Base b = new Base();
34+
b.setX(4);
35+
System.out.println(b.getX());
36+
37+
// Creating an object of derived class
38+
Derived d = new Derived();
39+
d.setY(43);
40+
System.out.println(d.getY());
41+
}
42+
}

0 commit comments

Comments
 (0)