Subclass in Java: In Java programming language, a subclass is a class that inherits properties and behaviors from another class known as the superclass. This relationship is established using the extends keyword.
Subclasses inherit fields and methods from the superclass, allowing for code reuse and the creation of a class hierarchy. Using Subclasses, we can also provide their own implementations of inherited methods, known as method overriding.
What Is A Subclass In Java?
When we want to build a flexible and organized code structure in the Java programming world, the time “subclass” plays an important role. As we all know, Java is an object-oriented programming language that relies on classes and inheritance.
A subclass is also called a derived class or child class; in a subclass, we derive the characteristics, attributes, and behaviors from another class known as the superclass or parent class.
The primary purpose of a subclass is to extend or specialize the functionality of the superclass. This is achieved by using the extends keyword in Java, which signifies that a new class is inheriting the properties and methods of an existing one.
This inheritance forms the basis of a hierarchy among classes, where a superclass can have multiple subclasses, creating a tree-like structure of related classes.
One of the key advantages of using subclasses is code reusability. Instead of writing the same code multiple times for different classes with common features, you can define them in a superclass and have multiple subclasses inherit them.
Subclasses can not only inherit but also override the methods and fields inherited from the superclass, allowing for customization of behavior while preserving the shared characteristics.
Subclass Syntax
This relationship between two classes can be established by using the “extends” keyword. Here’s a brief explanation:
class Superclass { // Superclass members and methods } class Subclass extends Superclass { // Subclass members and methods }
In the above example, the subclass inherits all the fields and methods from the superclass.
Key Aspects of Subclasses in Java
Here are some of the key points about subclass in Java:
- Inheritance: Subclasses inherit their superclass members (fields and methods). This enables code reusability and establishes an “is-a” relationship between the subclass and superclass. For example, if we have a superclass “Animal” and a subclass “Dog,” we can say that a Dog “is a” type of Animal.
- Extends Keyword: In Java, you use the extends keyword to declare that one class is a subclass of another.
- Method Overriding: Subclasses can override methods inherited from their superclass. This means they can implement a method with the same name, allowing for polymorphic behavior, also called method overriding. This feature is crucial for achieving dynamic method dispatch.
- Access Control: Subclasses can access public and protected members of their superclass. However, they cannot directly access private members of the superclass. This encapsulation ensures data integrity while allowing controlled access to superclass members.
- Constructor Chaining: When a subclass is instantiated, the superclass’s constructor is invoked before the subclass’s constructor. This ensures that the initialization of superclass members takes place before subclass-specific initialization.
Java supports single inheritance, meaning a class can inherit from only one superclass. However, it can implement multiple interfaces to achieve a form of multiple inheritance.
Example:
package com.softwaretestingo.subclass; //Define a superclass called "Vehicle" class Vehicle { String brand; int year; public Vehicle(String brand, int year) { this.brand = brand; this.year = year; } public void start() { System.out.println("Starting the vehicle."); } public void stop() { System.out.println("Stopping the vehicle."); } } //Define a subclass called "Car" that inherits from "Vehicle" class Car extends Vehicle { int numberOfDoors; public Car(String brand, int year, int numberOfDoors) { // Call the constructor of the superclass super(brand, year); this.numberOfDoors = numberOfDoors; } // Override the start method to provide a specific implementation for cars @Override public void start() { System.out.println("Starting the car's engine."); } } public class SubClassEx { public static void main(String[] args) { // Create an instance of the "Car" subclass Car myCar = new Car("TATA", 2023, 4); // Access fields and methods from the superclass System.out.println("Brand: " + myCar.brand); System.out.println("Year: " + myCar.year); // Call overridden method from the subclass myCar.start(); // Call method from the superclass myCar.stop(); } }
Output:
Brand: TATA Year: 2023 Starting the car's engine. Stopping the vehicle.
In this program, we have tried to implement the following:
- We define a superclass Vehicle with fields brand and year and methods start() and stop().
- We then define a subclass Car that inherits from the Vehicle class. The Car class adds an additional field number of doors and overrides the start() method to provide a specific implementation for cars.
- In the main() method, we create an instance of the Car class and demonstrate how to access fields and methods from both the super and subclass.
Conclusion:
We came to the end of this blog post; in short, let me explain that we’ve explored the concept of “Subclass in Java” and understand how subclass plays a crucial role in object-oriented programming. Also, we understand how it inherits properties and behaviors from a superclass by using the extends keyword, promoting code reusability and organization.
If you have any doubts or questions regarding the “Subclass in Java” topic or want to share suggestions to improve this article, please feel free to comment below. Your feedback is valuable, and we are here to help with any clarifications or additional information you may need.