Using classes with inheritance in Main class Java
Categories:
Mastering Inheritance in Java: A Guide to Superclasses and Subclasses

Explore how to effectively use inheritance in Java to create reusable and organized code, focusing on the extends
keyword, super
reference, and method overriding.
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit properties and behaviors from another class. In Java, this mechanism promotes code reusability and establishes a natural 'is-a' relationship between classes. This article will guide you through implementing inheritance, understanding the role of the super
keyword, and demonstrating its application with practical examples.
Understanding Java Inheritance Basics
At its core, inheritance in Java involves a superclass (also known as a parent class or base class) and a subclass (also known as a child class or derived class). A subclass extends
a superclass, gaining access to its public and protected members (fields and methods). This allows you to define common attributes and behaviors in a superclass and then specialize them in subclasses without duplicating code.
classDiagram Animal <|-- Dog Animal <|-- Cat Animal : +String name Animal : +void eat() Dog : +void bark() Cat : +void meow()
Class diagram illustrating the inheritance relationship between Animal, Dog, and Cat classes.
class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + " is eating.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name); // Calls the Animal class constructor
}
public void bark() {
System.out.println(name + " is barking.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
public void meow() {
System.out.println(name + " is meowing.");
}
}
Basic Superclass and Subclass definitions in Java.
The super
Keyword and Constructor Chaining
The super
keyword in Java serves two primary purposes: calling the superclass's constructor and accessing superclass members (methods or fields) that might be hidden or overridden in the subclass. When a subclass constructor is invoked, the superclass's constructor is implicitly called first. If you need to call a specific superclass constructor, you must explicitly use super()
as the first statement in the subclass constructor. This process is known as constructor chaining.
class Shape {
String color;
public Shape(String color) {
this.color = color;
System.out.println("Shape constructor called with color: " + color);
}
public void draw() {
System.out.println("Drawing a " + color + " shape.");
}
}
class Circle extends Shape {
double radius;
public Circle(String color, double radius) {
super(color); // Calls the Shape class constructor
this.radius = radius;
System.out.println("Circle constructor called with radius: " + radius);
}
@Override
public void draw() {
super.draw(); // Calls the draw() method of the superclass
System.out.println("Specifically, drawing a " + color + " circle with radius " + radius + ".");
}
}
public class Main {
public static void main(String[] args) {
Circle myCircle = new Circle("Red", 5.0);
myCircle.draw();
}
}
Demonstrating super()
for constructor chaining and super.method()
for calling superclass methods.
super()
must be the very first statement in a subclass constructor if you choose to call it explicitly. If omitted, Java automatically inserts a call to the superclass's no-argument constructor.Method Overriding and Polymorphism
Method overriding occurs when a subclass provides its own implementation of a method that is already defined in its superclass. The @Override
annotation is highly recommended for clarity and compile-time checking. This concept is crucial for polymorphism, allowing you to treat objects of different subclasses uniformly through a superclass reference. When an overridden method is called via a superclass reference, the version in the actual object's class (the subclass) is executed.
class Vehicle {
public void drive() {
System.out.println("Vehicle is driving.");
}
}
class Car extends Vehicle {
@Override
public void drive() {
System.out.println("Car is driving on the road.");
}
}
class Bicycle extends Vehicle {
@Override
public void drive() {
System.out.println("Bicycle is pedaling.");
}
}
public class MainClass {
public static void main(String[] args) {
Vehicle myVehicle = new Vehicle();
Vehicle myCar = new Car(); // Polymorphism
Vehicle myBicycle = new Bicycle(); // Polymorphism
myVehicle.drive();
myCar.drive();
myBicycle.drive();
}
}
Example of method overriding and polymorphism in Java.