Home / JAVA / Inheritance in Java

Inheritance in Java

Inheritance in Java

Inheritance is the feature of object-oriented programming concept for extending the data member of existing class at another class. It means we can share the data members like variables, member function etc at another class. It is basically used to reuse the classes. Java programming language allows us a method to manage the data members between multiple classes and execute with the object of another class.

 

Access Specifier in Inheritance

 

keywords in java
Image Source:- https://lh4.googleusercontent.com

Image Source:- https://lh4.googleusercontent.com

 

Public
It is a keyword that allows to define the data member as public that can access outside of the class or package by default the data member are public for package.
Private
It is a keyword that declare data member as private and it cannot access outside of the existing class.
Protected
It is a keyword that allow to define the function as protected that can access at single level of inheriting and declare with protected keyword.
Base Class
It is a class that allow to extended the feature of data members to another class. It is also known as Super Class.
Derived Class
It is a class that extend the feature of base class. This class able to execute the function of base class. It is also known as Sub Class.

 

 

Single Level Inheritance

Single Level is a type of inheriting that extend the feature of the base class into derive class at a single level. A keyword extends is used to get the data member and feature of the base class.

 

Inheritance in java
Image Source:- https://www.sitesbay.com

 

Example:-

import java.util.*;

Class A

{

   void t()

    {

       System.out.print ("Demo of SLI");

         }

    void s()

     {
         int a, b;

         Scanner sc = new Scanner ( System.in);

         System.out.print ("Enter value");

         a = sc.nextInt();

         System.out.print ("Enter value");

         b = sc.nextInt();

         int c = a+b;

         System.out.print ("Sum is" + c);

          }

}

Class B extends A

{
   void m()

    {
       Scanner sc = new Scanner ( System.in);

       System.out.print ("Enter value");

       int c = sc.nextInt();

        if (c>0)

        {

           System.out.print ("value positive");

             }

         else {

           System.out.print ("value negative");

             }

}

public static void main (String s[])

{

    B b = new B();

     b.t();

     b.s();

      b.m();

      }

}

 

B extends A :- It is an instruction that allows to extend the feature of the base class into deriving class. Here, B is a base class and A is derived class. We create an object of B to run the function of A and B also.

 

Multi-level Inheritance

Multi-level inheritance is a way to extend the feature of classes to a different level. A base class extended at its derive class and derive class also extended to another derive class an execute with the help of object of derived class.

 

category of class
Image Source:- http://csharpcorner.mindcrackerinc.netdna-cdn.com

 

Example:-

import java.util.*;

Class A

{

   void t()

    {

       System.out.print ("Demo of MLI");

         }

}

Class B extends A

{
    void s()

    {

        int a, b;

        Scanner sc = new Scanner (System.in);

        System.out.print ("Enter value");

         a = sc.nextInt();

         System.out.print ("Enter value");

         b = sc.nextInt();

         int c = a+b;

          System.out.print ("sum is" + c);

           }

}

Class BB extends B

{

    void m()

     {
        Scanner sc = new Scanner (System.in);

         System.out.print ("Enter value");

         int c = sc.nextInt();

         if (c>0)

          {

              System.out.print ("value positive");

                }

           else {

              System.out.print ("value negative");

                 }

}

   public static void main (String s[])

   {
       B b = new B();

        b.t();

         b.s();

          b.m();

           }

}

 

 

About Udit Mutreja

Check Also

Java For Loop

Loops Statement: The programming instructions are defined in a sequence and perform on the base …

Leave a Reply

Your email address will not be published. Required fields are marked *