Inheritance in Java

 Inheritance in Java

  • 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.


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
How to use inheritance in Java:-

The keyword used for inheritance is extends.

Syntax :

class derived-class extends base-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);  
        }  
output:-Programmer salary is:40000.0  

Bonus of programmer is:10000

Types of inheritance in java:-

On the basis of class, there can be three types of inheritance in java: 
1.single
2.multilevel 
3.hierarchical.

1.single:-
Example:-
class Animal
{  
void eat()
{
System.out.println("eating...");
}  
}  
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}  
}  
class TestInheritance
{  
public static void main(String args[])
{  
Dog d=new Dog();  
d.bark();  
d.eat();  
            }
}  

       2.Multilevel :-


Example:-

class Animal
{  
void eat()
{
System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark()
{
System.out.println("barking...");}  
}  
class BabyDog extends Dog
{  
void weep()
{
System.out.println("weeping...");}  
}  
class TestInheritance2
{  
public static void main(String args[])
{  
BabyDog d=new BabyDog();  
d.weep();  
d.bark();  
d.eat();  
            }
}  
 
3.Hierarchical Inheritance :-

 
Example:-
class Animal{  
   void eat()
   {
    System.out.println("eating...");}  
            }  
class Dog extends Animal{  
void bark()
{
System.out.println("barking...");}  
           }  
class Cat extends Animal
{  
void meow()
{
System.out.println("meowing...");}  
          }  
class TestInheritance3
{  
          public static void main(String args[])
{  
                Cat c=new Cat();  
                c.meow();  
                c.eat();  
}
}  



Comments

Popular posts from this blog

Type Conversions

Life cycle of a Thread and creating thread class