Monday, August 12, 2019

OOPS - Inheritance


What is inheritance:
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.
With inheritance, we can reuse the fields and methods of the existing class. Hence, inheritance facilitates Reusability and is an important concept of OOPs.

Important terminology:
·        Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).
·        Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
·        Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

The keyword used for inheritance is extends.

Syntax :
class sub-class extends Super-class 
{ 
   //methods and fields 
}



Example:


class Employee
{  
 float salary=40000;  
}  
class Programmer extends Employee
{  
int bonus=10000;  
 public static void main(String args[])
{  
   Programmer p=new Programmer();  
   System.out.println("Programmer salary is:"+p.salary);  
   System.out.println("Bonus of Programmer is:"+p.bonus);  
}  
}

Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.

In the above example, Programmer object can access the field of own class as well as of Employee class i.e. code reusability

Note:
The derived class inherits all the members and methods that are declared as public or protected. If the members or methods of super class are declared as private then the derived class cannot use them directly. The private members can be accessed only in its own class. Such private members can only be accessed using public or protected getter and setter methods of super class as shown in the example below.


class Teacher {
   private String designation = "Teacher";
   private String collegeName = "Beginnersbook";
   public String getDesignation() {
                   return designation;
   }
   protected void setDesignation(String designation) {
                   this.designation = designation;
   }
   protected String getCollegeName() {
                   return collegeName;
   }
   protected void setCollegeName(String collegeName) {
                   this.collegeName = collegeName;
   }
   void does(){
                   System.out.println("Teaching");
   }
}

public class JavaExample extends Teacher{
   String mainSubject = "Physics";
   public static void main(String args[]){
                   JavaExample obj = new JavaExample();
                   /* Note: we are not accessing the data members
                    * directly we are using public getter method
                    * to access the private members of parent class
                    */
                   System.out.println(obj.getCollegeName());
                   System.out.println(obj.getDesignation());
                   System.out.println(obj.mainSubject);
                   obj.does();
   }
}

Inheritance and Method Overriding
When we declare the same method in child class which is already present in the parent class the this is called method overriding. In this case when we call the method from child class object, the child class version of the method is called. However we can call the parent class method using super keyword as I have shown in the example below:
class ParentClass{
   //Parent class constructor
   ParentClass(){
                   System.out.println("Constructor of Parent");
   }
   void disp(){
                   System.out.println("Parent Method");
   }
}
class JavaExample extends ParentClass{
   JavaExample(){
                   System.out.println("Constructor of Child");
   }
   void disp(){
                   System.out.println("Child Method");
        //Calling the disp() method of parent class
                   super.disp();
   }
   public static void main(String args[]){
                   //Creating the object of child class
                   JavaExample obj = new JavaExample();
                   obj.disp();
   }
}
The output is :
Constructor of Parent
Constructor of Child
Child Method
Parent Method


No comments:

Post a Comment

If any suggestions or issue, please provide