Building Hierarchies: Mastering Inheritance in OOPs

Building Hierarchies: Mastering Inheritance in OOPs

I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming concept here in a simple and easy way from my experience. This will be very useful for beginner Devs and Students.

We have seen class and object in previous article now in this article, we will learn about Inheritance in OOPs.

Inheritance in Java is a fundamental feature of object-oriented programming that allows classes to inherit fields and methods from other classes. This enables code reusability and facilitates the creation of a hierarchical relationship among classes.

Why use inheritance in java

  • Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code.
  • Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism.
  • Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user.

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/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/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: 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 is used for inheritance in Java. Using the extends keyword indicates you are derived from an existing class.?In other words, “extends” refers to increased functionality.

class SubclassName extends SuperclassName  
{  
   //methods and fields  
}          

Example :

Super Class :

super class in java

Sub Class :

sub class in java

Used in Main Method :

inheritance in java

Types of inheritance in java :

Below are the different types of inheritance which are supported by Java.

  1. Single Inheritance: In single inheritance, a class inherits properties and behaviors from only one superclass. Java supports single inheritance as it helps maintain simplicity and avoids the complications that may arise from multiple inheritance.
  2. Multilevel Inheritance: In multilevel inheritance, a derived class (subclass) inherits properties and behaviors from another class, which itself is derived from another class. This forms a chain of inheritance where each class serves as both a subclass and a superclass.
  3. Hierarchical Inheritance: In hierarchical inheritance, multiple classes are derived from a single superclass. Each subclass inherits properties and behaviors from the common superclass, creating a hierarchical structure of classes.
  4. Multiple Inheritance (through Interfaces): Java does not support multiple inheritance of classes, meaning a class cannot directly inherit from more than one class. However, Java supports multiple inheritance through interfaces. A class can implement multiple interfaces, allowing it to inherit abstract methods from each interface.
  5. Hybrid Inheritance: Hybrid inheritance is a combination of multiple types of inheritance. For example, a class may exhibit both single inheritance and hierarchical inheritance simultaneously.

That's it for now.

Thanks,

Stay tuned for another chapter on OOPs Concept.





Brijesh Prajapati

Jr.Android Developer

6 个月

Thanks for posting

Hardik Jadav

Student at GTUPGSCHOOL

6 个月

Thanks for posting

要查看或添加评论,请登录

Naimish Trivedi的更多文章

  • Load a thumbnail image of a PDF file using Glide.

    Load a thumbnail image of a PDF file using Glide.

    I am Naimish Trivedi, I have a vast experience in native and cross platform programming. I have presented the Android…

    5 条评论
  • What are the Differences Between KSP and KAPT in Android?

    What are the Differences Between KSP and KAPT in Android?

    I am Naimish Trivedi, I have a vast experience in native and cross platform programming. I have presented the Android…

  • Launch modes in Android

    Launch modes in Android

    I am Naimish Trivedi, I have a vast experience in native and cross platform programming. I have presented the…

    2 条评论
  • The Art of Object Encapsulation

    The Art of Object Encapsulation

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    2 条评论
  • Abstraction in OOPs

    Abstraction in OOPs

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    3 条评论
  • Magic of Polymorphism in Object-Oriented Programming

    Magic of Polymorphism in Object-Oriented Programming

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    1 条评论
  • Class / Object in OOPs

    Class / Object in OOPs

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    6 条评论
  • OOPs Concept Ladder

    OOPs Concept Ladder

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    10 条评论
  • Lateinit vs Lazy in Kotlin

    Lateinit vs Lazy in Kotlin

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    5 条评论
  • Introduction of OOPs concept

    Introduction of OOPs concept

    I am Naimish Trivedi, I have a far experience in native and hybrid programming. I have presented the programming…

    4 条评论

社区洞察

其他会员也浏览了