Thursday 25 September 2014

Session 10:- Inheritance In java

What is inheritance ?


  •  Inheritance is the process of acquiring the properties of one class to other class.
  • extends” keyword which can be used to inheritance class. 
  • The class which is getting acquired or the class which has common properties it called has as parent class or super class or based class. 
  • The class which acquires the properties which called as child class or derived class or sub class. 
  •  Inheritance only states and behaviors will be inherited but not the constructor.
  • When we create an object of child class, we can access parent class properties and Child class properties. 
  • parent class must have common generic properties which are applicable for most of its child class.
  • Inheritance can be consider as “Is-A” Relationship 
  •   Type of inheritance

  1.   Single level inheritance 
  2. Multi level inheritance 
  3.  Multiple inheritance( By using class is not possible)
  4. Hierarchical inheritance  
  5.   Hybrid inheritance
Here, In Below fig You can see the Inheritance hierarchy.

Advantage of Inheritance:- 
  • Code Re-Usability
  • Consumes less time .
  • Easy  to access.
  • You just create the child class object and access the parent as well child class properties. 
Example:-Simple Inheritance 


                       public class Student {
 //parent class state(common properties)

      public String s_name;

      public int rollno;

      public String collegeName;

      public String course;

public void reading()
{
System.out.println ("Student is Reading :super class Method ");
}
public void exam()
{
System.out.println ("Student give exam: super class Method ");     
         }   
 } 
         // inheritance the student class

public class EngineeringStudent extends Student 

{

// Child specific properties

public int semester;   
public void setInformation(String name, String sub ,int r, String clg, int sem)
     {
     s_name=name;    
//super class properties inheritance the sub class 
     rollno =r;
     course=sub;
     collegeName =clg;
     semester=sem;
            }
          public void data(){
 System.out.println ("Child method");
     }
}  


public class Run{
public static void main(String [] a) 
{
     EngineeringStudent estudent=new EngineeringStudent ();
estudent.setInformation ("Rahul","java",001,''JNext",5);
System.out.println("your name :"+ estudent.s_name);
System.out.println("your collegeName :"+estudent.collegeName);
System.out.println("your rollno :"+ estudent.rollno);
System.out.println("your course : "+ estudent.course);
System.out.println("your semester :"+ estudent.semester);
      estudent.reading();   
    // Child class method(interties super class)
     estudent.exam();
     estudent.data();
               }        } 

OUTPUT:  

            your name :Rahul
            your collegeName :JNext
            your rollno :001
            your course :java
            your semester :5
            Student is Reading: super class Method
            Student gives the exam: super class Method
            Child method
 

 


No comments:

Post a Comment