Inheritance In Java

Introduction

  
I am discussing one of the most important pillars of object-oriented programming which is known as inheritance. Today, I am giving you the fundamental concept of inheritance likewise how we assume the project needs inheritance relationship.
  

What is Inheritance?

  • The approach of inheritance is simple. When we add a new class in any language and there already exists a class that includes some of the code that you need, you can derive your new class from the existing class. By doing this, we can reuse the fields and methods of the existing class.
      
  • Inheritance provides a powerful mechanism for organizing and structuring the software.

How do we assume Inheritance?

  • The best approach of assuming inheritance is that you first complete paperwork which means you draw all types of diagram ERD, Dataflow and etc. and then make a function which is used in inheritance.
     
  • After completing paperwork now you assume that the similar things which are used again and again in project you mark on it.

The scenario of Selling Store

 
Consider a scenario: I am a developer and one client comes who is a shop seller selling different types of things and he asks me to build a system which performs some functionalities, then he tells me the fundamentals of the shop. All he asked is that I  draw the shape which is necessary for the system and I created these three diagrams after listening to the listed scenario.
 
scenario
 
Now I see that there are some basic functionalities which are using same class again and again now the time comes to implement OOP concepts on the basic system approach; now I make a basic class and put those functionalities which are similar. Now I draw this type of diagram after assuming the OOP concept.
 
concept
 
Inheritance terminologies in different languages
  • Inheritance is done with some keywords; some of the language of  keywords is given below,
     
    Java public class Album extends Product { …
     
    C# public class Album: Product { …
     
    VB.NET public class Album inherits Product { …
     
    Ruby public class Album > Product { …
     
    C++ public class: Product { …
     
    Objective-C @interface class Album: Product { … 
Calling a method in the Super/Parent/Base Class
  • This is some method when you call a method in the following class,
     
    Java super.dosomething ();
     
    C# base.dosomething ();
     
    VB.NET Mybase.dosomething();
     
    Ruby super do_something
     
    C++ [super someMethod];
     
    Objective-C NamedBaseClass::dosomething();
Implementation
  • First I add class A and make the method of subtraction and pass two-parameter.
     
    pass two parameter
     
  • I add B Class but now I extend with class A because we need to add the function which is used in class A.
     
    b.java
     
  • Now in the main program file, I make an instance of class B and call the function which is located in Class A, because B Class extends A Class and all the functionalities in class A inherit class B, this is the real benefit of inheritance.
     
    inherit.java
     
Read more articles on Java: