Object Oriented Programming in JAVA – Inheritance
What is Inheritance?
- The main purpose of the inheritance is code extensibility whenever we are extending automatically the code is reused.
- In inheritance one class giving the properties and behavior and another class is taking the properties and behavior.
- Inheritance is also known as is-a relationship means two classes are belongs to the same hierarchy.
- By using extends keyword we are achieving inheritance concept.
- In the inheritance the person who is giving the properties is called parent the person who is taking the properties is called child.
- To reduce length of the code and redundancy of the code sun peoples introducing inheritance concept.
Types of Inheritance
Another Inheritance
class ParentClass { public void P1() { System.out.println("Parent Class Method is called"); } } class childclass extends ParentClass{ public void C1() { System.out.println("Child Class Method is called"); } public void C2() { System.out.println("Child Class Another Method is called"); } } //For Multilevel Inheritance class superchildclass extends childclass { public void SC1() { System.out.println("Super Child Class Method is called"); } }
public class InheritanceEx { public static void main(String[] args) { // TODO Auto-generated method stub childclass cobj=new childclass(); cobj.C1(); cobj.C2(); cobj.P1(); ParentClass pobj=new ParentClass(); pobj.P1(); //For Multilevel Inheritance superchildclass scobj=new superchildclass(); scobj.SC1(); } }
Good tips provided really helpful blog you have shown the really important aspects keep sharing more