Super Class and Subclass
🔹 What is a Superclass?
A superclass (also called base class or parent class) is a class that is inherited by another class. It defines common attributes and methods that can be shared by other classes.
✅ Key points:
-
A superclass can be reused by multiple subclasses.
-
It may be abstract (not directly instantiated).
-
It provides common functionality to be extended or overridden.
🔹 What is a Subclass?
A subclass (also called derived class or child class) is a class that inherits from another class using the extends
keyword. It can:
-
Use the methods and variables of the superclass.
-
Add new methods or fields.
-
Override superclass methods.
📌 Syntax of Inheritance in Java:
📘 Example:
Output:
🧠 Key Differences
Feature | Superclass | Subclass |
---|---|---|
Definition | Class being extended | Class that extends another |
Inheritance Role | Provider of features | Receiver of features |
Reusability | Offers reusable code | Reuses and adds functionality |
Access | Cannot access subclass directly | Can access superclass members |
🔐 Accessing Superclass Members
A subclass can access public and protected members of the superclass. It cannot access private members directly.
Comments
Post a Comment