INHERITANCE
Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class. In addition, you can add new fields and methods to your current class as well.
Inheritance promotes code reusability, method overriding, and polymorphism, which makes the Java program more modular and efficient.
In Java, inheritance is implemented using the extends keyword. The class that inherits is called the subclass (child class), and the class being inherited from is called the superclass (parent class).
Types of Inheritance in Java:
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Java does not support multiple inheritance directly through classes. This means a class cannot extend more than one class. The primary reason for this limitation is to avoid the "complexities" and "ambiguities" associated with multiple inheritance, particularly the "diamond problem," where a class inherits from two classes that have a common ancestor.
METHOD OVERLOADING
Method overloading can be performed within a single class, whereas method overriding occurs between a parent class and its child class, taking advantage of inheritance. Number of Methods: Method overloading allows the existence of multiple methods with the same name but different parameters.
METHOD OVERRIDING
Overriding in Java occurs when a subclass or child class implements a method that is already defined in the superclass or base class. When a subclass provides its own version of a method that is already defined in its superclass, we call it method overriding. The subclass method must match the parent class method's name, parameters, and return type.
Top comments (0)