Inheritance
- For Method Overriding ( polymorphism).
- For Code Reusability.
- Single inheritance
- Multiple inheritance
- Multi level inheritance
- Hierarchical inheritance
Class subclass-name extends superclass-name{ } |
Single inheritance
class a{ void j1(){ System.out.println("java"); } } class b extends a{ void j2(){
System.out.println("python"); } } public class
Pairosoft{ public static void main(String[] args){ b obj=new b(); obj.j1(); obj.j2(); } } |
java python |
Multiple inheritance
class A { statements ; } class B { statements ; } class C extends A, B { statements ; } |
Multi level inheritance
class a{ void j1(){ System.out.println("java"); } } class b extends a{ void j2(){
System.out.println("python"); } } class c extends b{ void j3(){ System.out.println("c++"); } } public class
Pairosoft{ public static void main(String[] args){ c obj=new c(); obj.j1(); obj.j2(); obj.j3(); } } |
java python c++ |
Hierarchical inheritance
class a{ void j1(){ System.out.println("java"); } } class b extends a{ void j2(){
System.out.println("python"); } } class c extends a{ void j3(){ System.out.println("c++"); } } public class
Pairosoft{ public static void main(String[] args){ c obj=new c(); obj.j1(); //obj.j2(); error obj.j3(); } } |
Output:
Java c++ |
0 Comments