Java Online - Inheritance in Java

Programming languages or concepts
in  this lesion we are learn about inheritance in java programming language. we cover following questions on inheritance :-
 What is mean by inheritance in java ?

 Explain the Terms used in Inheritance in java?
What is Super Keyword ?
Explain inheritance with it's types in java?
Explain types of inheritance in java ?


Inheritance in Java:-


    inheritance in java is a the mechanism in java by which one class is allowed to inherit the features(fields and methods) of another class. 
       The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).
        The idea behind inheritance in Java is that 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.
 Moreover, you can add new methods and fields in your current class also.
    It is an important part of Object Oriented programming system (OOPs).

We group the "inheritance concept" into two categories:
    subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from

To inherit from a class, use the extends keyword.

Terms used in Inheritance:-

  • Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
  • Sub Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods. Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).  Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class. As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.

How to use inheritance in Java:-
         The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality.
        The keyword used for inheritance is extends
  Syntax : 
        class Subclass-name extends Superclass-name  
            {  
                   //methods and fields  
            }

Example:-

class Student {
 void Student_Details() {
  System.out.println("Student name is:  Saurabh");
 }
}

class Std extends Student {
 void Std_info() {
  System.out.println("Roll_no is:  1");
 }
}

public class school {
 public static void main(String args[]) {
  Std s = new Std();
  s.Student_Details();
  s.Std_info();
 }
}

Student name is: Saurabh
Roll_no is: 1


Invoking Superclass Constructor

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.

super(values);

The super keyword

The super keyword is similar to this keyword. Following are the scenarios where the super keyword is used.

  • It is used to differentiate the members of superclass from the members of subclass, if they have same names.

  • It is used to invoke the superclass constructor from subclass.



Types of Inheritance in Java

Below are the different types of inheritance which are supported by Java.
        • Single Inheritance:
        • Multilevel Inheritance
        • Hierarchical Inheritance:
        • Hybrid Inheritance(Through Interfaces):
        • Multiple Inheritance (Through Interfaces):
  • Single Inheritance:-
        In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B.
        
            

import java.io.*;
import java.lang.*;
import java.util.*;

class first {
public void display()
{
System.out.println("Single inheritance");
}
}

class second extends first {
public void print() { System.out.println(" in java"); }
}
// Driver class
public class Main {
public static void main(String[] args)
{
second sec = new second();
sec.display();
sec.print();
}
}



Single inheritance
in java


  • Multilevel Inheritance :-
            When a class extends a class, which extends anther class then this is called multilevel inheritance. 



a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the above image, class 1 serves as a base class for the derived class 2, which in turn serves as a base class for the derived class 3.




Tags:
close