< Prev
Next >



Inheritance in Java





In Java, inheritance is used when a class wants to use/inherit the features of another existing class. The class that wants to use the feature of another class is called subclass, whereas the class whose features are to be used/inherited is referred to as superclass. Hence, the feature of inheritance gives Java the power of reusability.

Note : A class inherits another class by using extends keyword.




What features a subclass inherits from its superclass, through inheritance?



Note- Members marked with private access modifiers of a superclass are never inherited.




A simple example of inheritance



class A
{
int a=10;			//default
protected int b=20;		//protected

public void nameA()		//public 
{
System.out.println("A");
}

}


class B extends A // Class B inheriting class A
{
public static void main(String... ar)
{
B ob= new B();
ob.nameA();

System.out.println(ob.a);
System.out.println(ob.b);
}

}


Output is :


A
10
20


Program Analysis

Program Analysis


  • In this code, class B has inherited class A by using extends keyword, thereby class B has inherited




    A subclass can't inherit a private member of its superclass.


    
    class A
    {
    private int a=10;	//private access
    
    private void nameA()	//private access
    {
    System.out.println("A");
    }
    }
    
    
    class B extends A // Class B inheriting class A
    {
    public static void main(String... ar)
    {
    
    B ob= new B();
    ob.nameA();
    
    System.out.println(ob.a);
    
    }
    
    }
    


    Output -:


    
    A.java:18: error: cannot find symbol
    ob.nameA();
      ^
      symbol:   method nameA()
      location: variable ob of type B
    A.java:20: error: a has private access in A
    System.out.println(ob.a);
                         ^
    2 errors
    


    Program Analysis







    Types of inheritance :








    A java class cannot extend two classes at the same time. But, a class can implement two interfaces at the same time.
    interface Sum 				//interface1
    {
    public void add();
    }
    
    interface Substract			//interface2
    {
    public void difference();
    }
    
    
    class Math implements Sum,Substract 	//class Math has implemented multiple interfaces
    {
    public void add()
    {
    System.out.println("Addition");
    }
    
    public void difference()
    {
    System.out.println("Difference");
    }
    }





    Why inheritance is used?



    Let's understand this by an example. We are creating a specialized class, Mango, by adding some specialized methods of Mango and by inheriting some general features of Fruit class.
    //A general class, Fruit
    
    class Fruit
    {
    public void showFruit()
    {
    System.out.println("This is a fruit");
    }
    }
    
    
    //A specialized class, Mango
    
    class Mango extends Fruit		//class Mango inheriting class Fruit
    {
    public void fruitColor()
    {
    System.out.println("Color - Yellow");
    }
    
    public void fruitName()
    {
    System.out.println("Name - Mango");
    }
    
    public void hasSeed()
    {
    System.out.println("Seed - Yes");
    }
    }
    
    
    class A
    {
    public static void main(String... ar)
    {
    Mango ob= new Mango();
    ob.showFruit();
    ob.fruitName();
    ob.fruitColor();
    ob.hasSeed();
    }
    }


    Output -:


    This is a fruit
    Name - Mango
    Color - Yellow
    Seed - Yes


    Program Analysis









    Please share this article -




    < Prev
    Next >
    < Encapsulation
    Aggregation and Composition >
  • Please subscribe our social media channels for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    -
    Python

    Ad2