Skip to main content

Inheritance in Java

Welcome back to my blog! In this post, you will learn about Inheritance in Java.
Basically, Inheritance in Java is like a parent-child relationship. The child class acquires all the properties of the parent class.

You can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. You can also add new methods and fields.

Why Inheritance is used:


  •  For method over-riding,
  •  To reuse code.



Syntax: class subclass-name extends superclass-name

{
//methods and fields
}

Here, the keyword "extends" is used to derive all the properties from the superclass, i.e, the parent class.



Types of Inheritance in Java:


1. Single Inheritance,
2. Multilevel Inheritance,
3. Hierarchical Inheritance,
4. Multiple Inheritance,
5. Hybrid Inheritance.

1. Single Inheritance:

    In single inheritance, a child class extends its parent class.


2. Multilevel Inheritance:

    In multilevel inheritance, a parent class has a child class and that child class has its own child class and so on. Each parent class is extended by its own child class.


3. Hierarchical Inheritance:

    There are only one parent class and more than one child class. The children classes extend its parent class.


4. Multiple Inheritance:

    One child class extends more than one parent class.


5. Hybrid Inheritance:

Note: Multiple Inheritance is not supported in Java.


Examples:


1. Simple Inheritance:

class Vehicle {  
void run() {
System.out.println("running...");
          }  
}  
                      class Car extends Vehicle {  
                      void move() {
                      System.out.println("moving...");
                                    }  
                                }  
             class TestInheritance {  
             public static void main(String args[]) {  
             Car c=new Car();  
             c.move();  
           c.run();  
                }
}


Output:

moving...
running...



2. Multilevel Inheritance:


class Car{
   public Car()
      {
       System.out.println("Class Car");
      }
   public void vehicleType()
       {
       System.out.println("Vehicle Type: Car");
       }
}
class Suzuki extends Car{
     public Suzuki()
     {
        System.out.println("Class Suzuki");
     }
     public void brand()
      {
          System.out.println("Brand: Suzuki");
       }
   public void speed()
        {
        System.out.println("Max: 100Kmph");
        }
}
public class Suzuki-X extends Suzuki{

   public Suzuki-X()
       {
         System.out.println("Suzuki Model: Z");
       }
   public void speed()
       {
        System.out.println("Max: 80Kmph");
        }
   public static void main(String args[])
   {
     Suzuki-X obj=new Suzuki-X();
     obj.vehicleType();
     obj.brand();
     obj.speed();
   }
}


Output:


Class Car
Class Suzuki
Suzuki Model: X
Vehicle Type: Car
Brand: Suzuki
Max: 100Kmph


3. Hierarchical Inheritance:


class HierarchicalInheritance {
    void DisplayA()
        {
            System.out.println("This is a content of parent class");
        }
}

//B.java
class A extends HierarchicalInheritance {
    void DisplayB()
         {
          System.out.println("This is a content of child class 1");
         }
}

//c.java
class B extends HierarchicalInheritance {
    void DisplayC()
       {
        System.out.println("This is a content of child class 2");
       }
}

//MainClass.java
class HierarchicalInheritanceMain {
    public static void main(String args[])
        {
        System.out.println("Calling for child class C");
        B b = new B();
        b.DisplayA();
        b.DisplayC();
        System.out.println("Calling for child class B");
        A a = new A();
        a.DisplayA();
        a.DisplayB();
    }
}


Output:


Calling for child class C
This is a content of parent class
This is a content of child class 2
Calling for child class B
This is a content of parent class
This is a content of child class 1



 4. Hybrid Inheritance:


class C
  {
   public void disp()
   {
    System.out.println("C");
   }
}

class A extends C
{
   public void disp()
   {
    System.out.println("A");
   }
}

class B extends C
{
   public void disp()
   {
    System.out.println("B");
   }
   
}

class D extends A
{
   public void disp()
   {
    System.out.println("D");
   }
   public static void main(String args[]){

    D obj = new D();
    obj.disp();
   }
}



Output:


D




Thank you for coming to my blog. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!

For more Java tutorials, visit my blog:
www.javaoptimist.blogspot.com

Comments

Popular posts from this blog

VLOOKUP in MS Excel

VLOOKUP in MS Excel H ello and welcome back to Learn Java. It's been very long since I wrote my last post. This post will give you a short and simple tutorial on using VLOOKUP in MS Excel. Verticle Lookup or VLOOKUP is a function in MS Excel that helps a user look for a specific value in a column   based on another particular value in the first column. A simple example: Your boss asks you to find the price of a food item based on its name. Syntax: VLOOKUP(the value you want to look up, where you want to look for it(array/column/table), the column number the value is in, return an approximate or exact match) Let's try to understand it more clearly: Here's a little data set containing a table of my favourite food items.  Step 1: Select a specific cell/text value you would like to search for in the given table/data set. Here, we are looking for the price of Pizza, so we use cell value "Pizza" which is also cell reference A3. We are making sure to use a cell value/re

Simple Java program

J ava is a high level programming language which is used almost everywhere- in Android mobile phones, set top boxes, in Google Maps, video games, etc. Today let's see how to make the simplest Java program- "Hello, world" in Eclipse. Step #1. Select your file location where you want to save your work. Step #2. Select File>New>Project as shown in the picture below: Step #3. Select Java>Java Project>Next as shown below: Step #4. Enter the project name in Project name as shown below: Step #5. Select Finish. Step #6. Select Yes. Step #7. Double click one the project name. You will see a folder named as "src" under the the project HelloWorld. Step #8. Right click on the folder "src">New>Class. Step #9. Enter the name of the class. Then click on Finish. Step #10. You will see a screen like this: Step #11. Write the following code: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!!"); }