Showing posts with label Inheritance in Java step by step in details. Show all posts
Showing posts with label Inheritance in Java step by step in details. Show all posts

Inheritance in Java in Details Step by Step



OOPS concepts in Java

Java tutorials for beginners

Java basics concepts

Java Objects and Classes

What is Inheritance in Java?

What is Abstraction in Java?

What is Interface in Java?

What is Abstract Class in Java?

What is Polymorphism in Java?

What is Encapsulation in Java?

Java programming example

How Java Program Work?

Hello World in Java

Java basics for beginners



Inheritance:

The process by which one class access the properties, data and methods of another class is called inheritance

The purpose of inheritance is re-usability code so that a class has to write only the features and rest of the common properties and functionalities can be extended from the another class.

importance in Inheritance


1. Sub class or Child Class.

The class that extends another class and use the features is known as Sub class or Child class.

2. Super Class or Parent Class.

The inherited class whose properties and functionalities are using in another class is known as Parent class / Super class or Base class.

  
Example:

class class Calculator {
   int value;
        
   public void sum(int x, int y) {
      value = x + y;
      System.out.println("The sum of the given numbers is "+value);
   }
        
   public void difference(int x, int y) {
      value = x - y;
      System.out.println("The difference between the given numbers is  "+value);
   }
}

public class MainCalculator extends Calculator {
   public void multiple(int x, int y) {
      value = x * y;
      System.out.println("The multiple of the given numbers is "+value);
   }
        
   public static void main(String args[]) {
      int a = 30, b = 10;
      MainCalculator obj = new MainCalculator ();
      obj.sum(a, b);
      obj.difference(a, b);
      obj.multiple(a, b);
   }
}


Types of Inheritance

1. Single Inheritance. 

2. Multilevel Inheritance.


1. Single Inheritance


In single inheritance, sub classes inherit the features of one super class.

Example:

class class Calculator {
   int value;
        
   public void sum(int x, int y) {
      value = x + y;
      System.out.println("The sum of the given numbers is "+value);
   }
        
   public void difference(int x, int y) {
      value = x - y;
      System.out.println("The difference between the given numbers is  "+value);
   }
}

public class MainCalculator extends Calculator {
   public void multiple(int x, int y) {
      value = x * y;
      System.out.println("The multiple of the given numbers is "+value);
   }
        
   public static void main(String args[]) {
      int a = 30, b = 10;
      MainCalculator obj = new MainCalculator ();
      obj.sum(a, b);
      obj.difference(a, b);
      obj.multiple(a, b);
   }
}




2. Multilevel Inheritance.


In Multilevel Inheritance, a subclass/ derived  class inherit the features of more than one class.

Java does not support multiple inheritance with classes. We can achieve multiple inheritance only through Interfaces.

Example:

public interface InterfaceIA {

         public void sum(int x,int z);
}

public interface InterfaceIB {

         public void subtraction(int x,int z);
}



public class Impl implements InterfaceIA, InterfaceIB, InterfaceIC {

         @Override
         public void sum(int x,int z) {

                 System.out.println("Addition is "+(x+z));
         }

         @Override
         public void substraction(int x,int z) {
                 System.out.println("Subtraction is "+(x-z))
         }

         public static void main(String[] args) {

                 Impl obj= new Impl();

                 obj.sum();

                 obj.subtraction();

             }

   }