Java Notes (Inheritance)
Java Notes (Inheritance)
1. Encapsulation:
2. Inheritance:
3. Polymorphism:
4. Abstraction:
Inheritance
Inheritance is one of the fundamental concepts in Object-Oriented Programming
(OOP) that allows a class (subclass or derived class) to inherit properties and
behaviors from another class (super class or base class).
Inheritance creates a IS-A relationship between classes, where the subclass is a
specialized version of the super class.
The class that is being inherited from is called the super class, and the class that
inherits from the super class is called the subclass.
The subclass extends the capabilities of the super class by inheriting its attributes and
methods. It can also define additional attributes and methods of its own.
The key benefits of inheritance are code reuse and the establishment of a hierarchical
organization of classes.
When a subclass inherits from a super class, it gains access to all the public and protected
members (attributes and methods) of the super class.
This means that the subclass can use the inherited members directly without having to redefine
them.
It can also override inherited methods to provide a specialized implementation, tailoring the
behavior to its specific needs.
The syntax for defining inheritance in most programming languages, including Java, is as
follows:
// Defining a superclass
class Superclass {
// superclass members
}
Single Inheritance
Single inheritance involves a subclass inheriting from a single superclass. In this form of
inheritance, a class can have only one direct superclass. Most programming languages,
including Java, support single inheritance.
class A {
// superclass A
}
class B extends A {
// subclass B inherits from A
}
Multilevel Inheritance
Multilevel inheritance involves a chain of inheritance, where a subclass inherits from another
subclass, and so on. In this case, a class serves as both a superclass and a subclass at
different levels of the hierarchy.
class A {
// superclass A
}
class B extends A {
// subclass B inherits from A
}
class C extends B {
// subclass C inherits from B
}
Hierarchical Inheritance
Hierarchical inheritance refers to a situation where multiple subclasses inherit from a single
superclass. In other words, a superclass has several derived classes.
class Animal {
// superclass Animal
}
Diamond Problem
One of the main issues with multiple inheritance is the "diamond problem." This
problem arises when a class inherits from two or more classes that have a common super
class.
If both super classes provide a method with the same name and the subclass does
not override it, the compiler cannot determine which method to use, leading to ambiguity.
class A {
void display() {
System.out.println("A");
}
}
class B {
void display() {
System.out.println("B");
}
}
By not supporting multiple inheritance of classes, Java aims to strike a balance between
flexibility and maintainability, providing an object-oriented programming model that is powerful,
yet clear and manageable.
Developers can still achieve many benefits of code reuse and extensibility through interfaces,
composition, and other design patterns.