Java Inheritance

Advertisements

Java Inheritance

Inheritance means access the properties and features of one class into another class. The class who is going to provide its features to another class will be called base class and the class who is using the properties and features of another class will be called derived class.

Example how to inherit class in java using extend keyword


    //InheritDemo.java

    import java.util.Scanner;

    class Employee
    {

        int Id;
        String Name;
        double Salary;

        void GetData()
        {
            Scanner Sc = new Scanner(System.in);

            System.out.print("\n\tEnter Employee Id : ");
            Id = Integer.parseInt(Sc.nextLine());

            System.out.print("\n\tEnter Employee Name : ");
            Name = Sc.nextLine();

            System.out.print("\n\tEnter Employee Salary : ");
            Salary = Double.parseDouble(Sc.nextLine());
        }

        void PutData()
        {
            System.out.print("\n\n\tEmployee Id : " + Id);
            System.out.print("\n\tEmployee Name : " + Name);
            System.out.print("\n\tEmployee Salary : " + Salary);
        }
    }

    class Company extends Employee         //Statement 1
    {
        int RegNo;
        String CName;

        void ReadData()
        {
            Scanner Sc = new Scanner(System.in);

            System.out.print("\n\tEnter Registration No. : ");
            RegNo = Integer.parseInt(Sc.nextLine());

            System.out.print("\n\tEnter Company Name : ");
            CName = Sc.nextLine();
        }

        void WriteData()
        {
            System.out.print("\n\tRegistration No. : " + RegNo);
            System.out.print("\n\tCompany Name : " + CName);
        }
    }

    class InheritDemo
    {
        public static void main(String args[])
        {
            Company C = new Company();// Creating Object of Derived Class

            C.GetData();              // Calling Base Class Method()
            C.ReadData();             // Calling Derived Class Method()

            C.PutData();              // Calling Base Class Method()
            C.WriteData();            // Calling Derived Class Method()
        }
    }

    Output :

                Enter Employee Id : 1
                Enter Employee Name : Kumar
                Enter Employee Salary : 45000
                Enter Registration No. : 715
                Enter Company Name : Study Of Information & Technology

                Employee Id : 1
                Employee Name : Kumar
                Employee Salary : 45000
                Registration No. : 715
                Company Name : Study Of Information & Technology

In the above example, using statement 1, we are inheriting an Employee class into Company class using extends keyword.

Now, The object of Company class can access the member function GetData() and PutData() of Employee class.

Types of Inheritance in Java

Java supports 3 types of inheritance as follows:

  • Single Inheritance
  • Multilevel Inheritance
  • Heirarchical Inheritance

Note: Java doesn't support multiple inheritance..

Java Single Type Inheritance

A derived class with only one base class is called single inheritance.


	

Java Miltilevel Type Inheritance

A derived class with one base class and that base class is a derived class of another is called multilevel inheritance.


	

Java Hierarchial Type Inheritance

Multiple derived classes with same base class is called heirarchical inheritance.


	

Advertisement