Inheritance in Java

Inheritance is one of the most important OOPS concepts where one class inherits or acquires the properties of another class.

The class from which the properties are inherited is called Super class or Parent class

The class which inherit the properties is called Subclass or Child class.

We use “extends” keyword to achieve the same.

The whole idea behind inheritance is “Reusability”.

We can create more specific classes by inheriting fields and methods from the Generic class.

And we can add additional fields and methods in the Subclass.

Syntax

  1. class Subclass-name extends Superclass-name  
  2. {  
  3.    // fields  and  methods
  4. }
class Subclass-name extends Superclass-name  
{  
   // fields  and  methods 
} 

Example

  1. Class A{
  2.       //Fields and methods
  3. }
  4.  
  5. Class B extends A{
  6.  
  7. }
Class A{
      //Fields and methods
}

Class B extends A{

}


We use “extends” keyword to inherit the properties of another class as shown above.

In the above example, class “A” is the parent class and class “B” is the subclass.

class “B” inherits the properties from class “A”.

Let us see the real-time example


Inheritance

We can observe in the above diagram that Developer class is inheriting all the properties of Employee class.

Let’s achieve the same using below code


Employee.java

  1. package com.kb.inheritance;
  2.  
  3. class Employee{
  4.    
  5.      String empId ;
  6.      String name;
  7.      float salary;
  8.      Public void assignProject(){
  9.     System.out.println("Project assigned to  "+ empId);
  10.  
  11.   }
  12. }
package com.kb.inheritance;
 
class Employee{
    
     String empId ;
     String name;
     float salary;
     Public void assignProject(){
    System.out.println("Project assigned to  "+ empId);

  }
}


Developer.java

  1. package com.kb.inheritance;
  2. public class Developer extends Employee {
  3. float bonus;
  4.  
  5.  
  6. Public void codeReview(){
  7.     System.out.println("Code review is done by the employee  "+ empId);
  8.  
  9.  }
  10. }
package com.kb.inheritance;
public class Developer extends Employee {
float bonus;


Public void codeReview(){
    System.out.println("Code review is done by the employee  "+ empId);

 }
}


InheritanceTest.java

  1. package com.kb.inheritance;
  2. class InheritanceTest{
  3. public static void main(String[] args) {
  4.     Developer dev = new Developer();
  5.     dev.empId=101;
  6.     dev.name=”John”;
  7.     dev.salary=34000.5f;
  8.     dev.bonus=20000.5f;
  9.  
  10.     System.out.println("bonus is "+dev.bonus);
  11.     Dev.codeReview();
  12.  
  13.   }
  14. }
package com.kb.inheritance;
class InheritanceTest{
public static void main(String[] args) {
    Developer dev = new Developer();
    dev.empId=”101”;
    dev.name=”John”;
    dev.salary=34000.5f;
    dev.bonus=20000.5f;

    System.out.println("bonus is "+dev.bonus);
    Dev.codeReview();

  }
}




In the above example, we can understand that Employee is the Parent class and Developer is the subclass.

All the attributes of Employee class are inherited to Developer class and hence we don’t need to declare them again in the Developer class.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on