java3
java3
Inheritance
In Java, inheritance is a mechanism where a class (the subclass or child class) inherits properties and
behaviors (fields and methods) from another class (the superclass or parent class), promoting code
reusability and hierarchical class structures.
Here's a more detailed explanation:
What it is:
Inheritance allows you to create a new class (the subclass) based on an existing class (the
superclass). The subclass automatically gains all the features (fields and methods) of the superclass,
and you can then extend or modify these features, or add new ones.
Why use it:
Code Reusability: You don't have to rewrite code that's already implemented in the
superclass.
Hierarchical Structure: Inheritance helps organize classes in a hierarchy, making it
easier to understand and maintain code.
Extensibility: You can easily extend the functionality of a superclass by creating
subclasses.
Types of Inheritance:
Single Inheritance: A subclass inherits from only one superclass.
Multiple Inheritance (not directly supported): Java does not directly support
multiple inheritance of classes (meaning a class inheriting from multiple classes) to
avoid the "diamond problem". However, you can achieve a similar effect using
interfaces.
Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
Hybrid Inheritance: A combination of different types of inheritance.
Key Terms:
Superclass (or Parent Class): The class from which another class inherits.
Subclass (or Child Class): The class that inherits from another class.
extends keyword: Used to indicate inheritance.
super keyword: Used to access members of the superclass from within the subclass.
How it works:
You use the extends keyword to indicate that a class inherits from another.
For example:
In Java, it is possible to inherit attributes and methods from one class to another. We group the
"inheritance concept" into two categories:
subclass (child) - the class that inherits from another class
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
1. Single-level inheritance
Single-level inheritance in Java is a fundamental concept where a subclass inherits from only one
superclass. This means that the subclass can acquire the properties and methods of the superclass,
enabling code reuse and the extension of existing functionality.
class Animal {
void makeSound() {
System.out.println("Generic animal sound");
}
}
2. Multi-level Inheritance
Multilevel inheritance in Java is a feature that allows a class to inherit properties and behaviours
from another class, which in turn inherits from another class, forming a "chain" of inheritance. This
mechanism enables a class to inherit methods and fields from multiple ancestors but in a direct line,
where each class in the chain inherits from one class directly above it.
4. Multiple Inheritance
Multiple inheritance refers to a feature in object-oriented programming where a class can inherit
properties and methods from more than one parent class. This concept allows a subclass to inherit
behaviours and attributes from multiple base (or super) classes, creating a rich, multifaceted
hierarchy.
Instead of multiple inheritance for classes, Java provides a mechanism to achieve polymorphism and code
reuse through interfaces.
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance, such as hierarchical,
multiple, multilevel, or single inheritance. It integrates various inheritance mechanisms to achieve a
specific design or functionality. However, it's important to note that due to Java's restriction on
extending multiple classes directly (to avoid complexity and ambiguity issues like the Diamond
Problem), hybrid inheritance in Java is primarily achieved through a mix of class inheritance (using
the extends keyword) and interface implementation (using the implements keyword).
In Java, hybrid inheritance can involve:
Single and Multilevel Inheritance through classes: A class inherits from one superclass, and
then another class inherits from that subclass, forming a linear inheritance chain.
Hierarchical Inheritance through classes: Multiple classes inherit from a single parent class.
Multiple Inheritance through Interfaces: A class implements multiple interfaces, inheriting
the abstract methods from these interfaces.
Combining Class Inheritance and Interface Implementation: A class extends another class
and also implements one or more interfaces.
// Parent class
class Animal {
void eat() {
System.out.println("Eating...");
}
}
// Intermediate class (multilevel inheritance)
class Mammal extends Animal {
void walk() {
System.out.println("Walking...");
}
}
HybridAnimal() {
animal = new Animal();
mammal = new Mammal();
bird = new Bird();
}
void displayBehaviors() {
animal.eat();
mammal.walk();
bird.fly();
}
}
public class Main {
public static void main(String[] args) {
HybridAnimal hybrid = new HybridAnimal();
hybrid.displayBehaviors();
}
}