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.
- 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.
- 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.
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
}
Below are the different types of inheritance which are supported by Java.
- 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.
- 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.
- 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.
- 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.
- 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.
Stay tuned for another chapter on OOPs Concept.
Jr.Android Developer
6 个月Thanks for posting
Student at GTUPGSCHOOL
6 个月Thanks for posting