JavaObject-Oriented Programming (OOP)
Inheritance in Java
Learn how inheritance works in Java, its types, advantages, and how it enables code reuse using real-world examples.
Inheritance in JavaLink to this section
Inheritance is an OOP mechanism where one class acquires the properties and behaviors of another class.
It promotes code reusability and establishes an IS-A relationship between classes.
Parent and Child ClassesLink to this section
The class whose properties are inherited is called the parent (superclass). The class that inherits those properties is called the child (subclass).
Types of Inheritance in JavaLink to this section
Java supports the following types of inheritance using classes.
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
note
Multiple inheritance is not supported using classes to avoid ambiguity.
Using the extends KeywordLink to this section
The extends keyword is used by a subclass to inherit a superclass.
tip
A subclass can access non-private members of the parent class.
Method OverridingLink to this section
Method overriding occurs when a child class provides a specific implementation of a method already defined in the parent class.
warning
The method signature must be exactly the same to override a method.
Inheritance in Java
Question 1 of 5Which keyword is used to inherit a class in Java?
Inheritance in Java
- Create a parent class and a child class using inheritance.
- Demonstrate method overriding with a real-world example.
- Explain why Java does not support multiple inheritance with classes.
- Identify IS-A relationships in daily life.
- Write a program using multilevel inheritance.