General

Inheritance in Java

General May 14, 2019
Java Inheritance

Inheritance in Java

First, the question comes in our mind, what is inheritance? Thus, this article will show you the definition of inheritance, its meanings, and its types. Let us start with understanding the concept of inheritance.

Definition of inheritance

Inheritance is the procedure in which one class access the properties of another class known as a subclass. For instance, the kid inherits or gains the features, manners, and characteristics of his/her parents. With the inheritance concept, we can just reuse the methods and properties of the existing class. Thus, inheritance provides the reusability feature and is an essential property of OOPs.

Example:

class Parentclass extends Childclass 
{ 
   //methods and fields 
}

Now if you are clear with the meaning and concept of inheritance, let us move ahead to the types of inheritance.

Different types of inheritance

There are Two Types of Inheritance

  • Single Inheritance
  • Mulilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance:

In the concept of single inheritance, one class provides an extension to another class (only one class). It simply means that class A extends to class B. In such a way, Class B extends only class A. This way class A is known as a superclass and class B is known as the subclass.

Multilevel Inheritance:

In this, one class can access and inherit properties from the derived class. Thus, the derived class becomes the base class for the new class. For instance, class C is considered as the subclass of B and B is the subclass of class A.

Hierarchical Inheritance:

In this concept of hierarchical inheritance, one class is inherited by many subclasses. For instance, class A, B, and C inherit the same class D.

Multiple Inheritance:

In the concept of multiple inheritances, one class extends more than one class. Hence, Java is not able to support multiple inheritances. For instance, class C extends both class A and class B respectively.

Hybrid Inheritance:

It is just a combination of single and multiple inheritances. For instance, all the protected and public members of class A are inherited into the class D, first through class B and another through class C.

 Applications of Java Inheritance

  • In Java, when an ‘I-A’ relationship occurs between the two classes we use inheritance.
  • The parent class is defined as the superclass and the inherited class is known as the superclass.
  • The ‘extend’ keyword is used by the subclass for inheriting the features of the superclass.
  • Inheritance is essential since it focuses on reusability of code.

Syntax:

class subClass extends superclass
{
    //fields and method declaration
}

Example:

class Doctor
{
    Void Doctor_details(){
        System.out.println(“doctor Details…..”);
}
}
Class surgeon extends Doctor 
{
    Void Surgeon_details()
    {
        System.out.println(“surgeon details…”);
    }
}
Public class Hospital
{
    Public static void main(String args[])
    {
        Surgeon s = new Surgeon();
        s.doctor_details();
        s.Surgeon_details();
    }
}

Super keyword

The keyword ‘super’ is similar to the ‘this’ keyword. The keyword ‘super’ can be helpful and useful for accessing any data member or methods of the parent class. The super keyword can be utilized at method, variable and constructor level.

 Syntax

Super <method name>();

Inheritance with the example and concept of OOPs

let us consider the same application of banking from the earlier instances. We are supposed to open the two distinct types of accounts, one for saving and another for checking which is also recognized as the current saving.

The two types of accounts are:

  • Current  and checking account
  • Saving account

Then, compare and study the approach of coding from the object-oriented and structured perspective of programming. The approach of structural programming, we will make two functions –

  • For the deposit action
  • One to withdraw

Thus, the working of these functions stays the same in all the accounts.

Approach of OOPs

Using the approach of OOPs, we can make two classes.

  • Each has the execution of the withdraw and deposit functions
  • This will reduce extra work.
Request for change in the software

Now there is the requirement of change in the specification for the thing which is so known in the software company. You are supposed for adding the functionality of privileged account of banking with the facility. For the background, the overdraft is the feature where you can withdraw any amount available in the balance of your account.

Structured approach

Using the approach functionally, you need to edit the function, that is baselined and tested. Ass the methods and fields will take care of such new requirements.

Click Here-> Get Java Training with Real-time Projects

Besant Technologies – Chennai & Bangalore Branch Locations

Besant Technologies - Velachery Branch

Plot No. 119, No.8, 11th Main road, Vijaya nagar,

Velachery, Chennai - 600 042

Tamil Nadu, India

Landmark - Reliance Digital Opposite Street

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Tambaram Branch

No.2A, 1st Floor, Duraisami Reddy Street,

West Tambaram, Chennai - 600 045

Tamil Nadu, India

Landmark - Near By Passport Seva

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - OMR Branch

No. 5/318, 2nd Floor, Sri Sowdeswari Nagar,

OMR, Okkiyam Thoraipakkam, Chennai - 600 097

Tamil Nadu, India

Landmark - Behind Okkiyampet Bus Stop,

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Porur Branch

No. 180/84, 1st Floor, Karnataka Bank Building,

Trunk Road, Porur, Chennai - 600 116

Tamil Nadu, India

Landmark - Opposite to Gopalakrishna Theatre

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Anna Nagar Branch

Plot No:1371, 28th street kambar colony,

I Block, Anna Nagar, Chennai - 600 040

Tamil Nadu, India

Landmark - Behind Reliance Fresh

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - T.Nagar Branch

Old No:146/2- New No: 48,

Habibullah Road,T.Nagar, Chennai - 600 017

Tamil Nadu, India

Landmark - Opposite to SGS Sabha

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Thiruvanmiyur Branch

22/67, 1st Floor, North mada street, Kamaraj Nagar

Thiruvanmiyur, Chennai 600041

Tamil Nadu, India

Landmark - Above Thiruvanmiyur ICICI Bank

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Siruseri Branch

No. 4/76, Ambedkar Street, OMR Road, Egatoor, Navallur,

Siruseri, Chennai 600130

Tamil Nadu, India

Landmark - Near Navallur Toll Gate, Next to Yamaha Showroom

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - Maraimalai Nagar Branch

No.37, Ground Floor, Thiruvalluvar Salai,

Maraimalai Nagar,Chennai 603209

Tamil Nadu, India

Landmark - Near to Maraimalai Nagar Arch

  +91-8099 770 770

  info@besanttechnologies.com

Besant Technologies - BTM Layout Branch

No 2, Ground floor, 29th Main Road,

Kuvempu Nagar,BTM Layout 2nd Coming from Silkboard,

AXA company signal, Stage, Bangalore - 560 076

Karnataka, India

Landmark - Next to OI Play School

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Marathahalli Branch

No. 43/2, 2nd Floor, VMR Arcade,

Varthur Main Road, Silver Springs Layout,

Munnekollal, Marathahalli, Bengaluru - 560037

Karnataka, India

Landmark - Near Kundalahalli Gate Signal

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Rajaji Nagar Branch

No. 309/43, JRS Ecstasy, First Floor,

59th Cross, 3rd Block, Bashyam Circle,

Rajaji Nagar, Bangalore - 560 010

Karnataka, India

Landmark - Near Bashyam Circle

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Jaya Nagar Branch

No. 2nd Floor,1575,11th Main Road,

4th T-Block, Pattabhirama Nagar,

Jaya Nagar, Bangalore - 560 041

Karnataka, India

Landmark - Opposite to Shanthi Nursing Home

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Kalyan Nagar Branch

No.513, 4th Cross Rd

2nd Block, HRBR Layout,

Kalyan Nagar, Bangalore - 560 043

Karnataka, India

Landmark - Opposite to kalayan nagar Axis Bank

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Electronic City Branch

No. 7, 3rd Floor, Ganga Enclave,

Neeladri Road, Karuna Nagar, Doddathoguru Village,

Electronics City Phase 1, Electronic City,

Bangalore - 560100, karnataka, India

Landmark - Adjacent to HDFC Bank and State Bank Of India

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Indira Nagar Branch

No.54, 1st Floor,

5th Main kodihalli,

Bengaluru, Karnataka 560008, India

Landmark - Behind Leela Palace Hotel,

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - HSR Layout Branch

Plot No. 2799 & 2800, 27th Main,

1st Sector, HSR Layout,

Bengaluru, Karnataka 560102, India

  +91-8767 260 270

  info@besanttechnologies.com

Besant Technologies - Hebbal Branch

No.29, 2nd Floor, SN Complex,

14th Main Road, E-Block Extention, Sahakara Nagar,

Bengaluru, Karnataka -560092, India

  +91-8767 260 270

  info@besanttechnologies.com

Scroll Up