Please enable JavaScript.
Coggle requires JavaScript to display documents.
Inheritance in Java - Coggle Diagram
Inheritance in Java
- A subclass inherits from a single superclass (Parent -> Child).
- Example:
class Dog extends Animal.
- A class inherits from a class, which in turn inherits from another class (Grandparent -> Parent -> Child).
- Example:
class Labrador extends Dog.
- Multiple subclasses inherit from a single superclass (Parent -> Child1, Child2, ...).
- Example:
Dog, Cat, and Cow all extend Animal.
- Multiple Inheritance (via Interfaces)
- Not supported for classes in Java to avoid complexity.
- Can be achieved using interfaces, allowing a class to implement multiple interfaces.
- A combination of different types of inheritance.
- Achievable using interfaces.
- Used to create an inheritance relationship between classes.
- Used by a class to inherit from an interface.
- Refers to the superclass object.
- Used to call the superclass constructor or method.
- Prevents a class from being inherited when used with the class declaration.
- Prevents a method from being overridden when used with a method.
- Avoids duplicating code by inheriting features from a superclass.
- Provides a specific implementation of a method that is already defined in the superclass.
- Achieved through method overriding.
- New features can be added by creating new subclasses.
- A mechanism where one class acquires the properties and behaviors of another class.
- Creates an "is-a" relationship (e.g., a
Dog "is a" Animal).
- Promotes code reusability and polymorphism.
- Superclass (Parent Class)
- The existing class from which a new class is derived.
-
- The new class that inherits from the superclass.
- Can add its own fields and methods.
- Non-private members are inherited.
-
private members of the superclass are not directly accessible.
- Deep inheritance hierarchies can be difficult to manage.
- Changes in the superclass can affect all its subclasses.