Inheritance.java
· 703 B · Java
Raw
class A
{
public void methodA() {
System.out.println("method of Class A");
}
}
class B extends A{
public void methodB(){
System.out.println("method of Class B");
}
}
class C extends A{
public void methodC(){
System.out.println("method of Class C");
}
}
class D extends A{
public void methodD(){
System.out.println("method of Class D");
}
}
class InheritncExm{
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
//All classes can access the method of class A
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}
1 | class A |
2 | { |
3 | public void methodA() { |
4 | System.out.println("method of Class A"); |
5 | } |
6 | } |
7 | class B extends A{ |
8 | public void methodB(){ |
9 | System.out.println("method of Class B"); |
10 | } |
11 | } |
12 | class C extends A{ |
13 | public void methodC(){ |
14 | System.out.println("method of Class C"); |
15 | } |
16 | } |
17 | class D extends A{ |
18 | public void methodD(){ |
19 | System.out.println("method of Class D"); |
20 | } |
21 | } |
22 | class InheritncExm{ |
23 | public static void main(String args[]) |
24 | { |
25 | B obj1 = new B(); |
26 | C obj2 = new C(); |
27 | D obj3 = new D(); |
28 | //All classes can access the method of class A |
29 | obj1.methodA(); |
30 | obj2.methodA(); |
31 | obj3.methodA(); |
32 | } |
33 | } |
34 |