⬅ Previous Topic
Inheritance in JavaNext Topic ⮕
Java Abstraction in OOP⬅ Previous Topic
Inheritance in JavaNext Topic ⮕
Java Abstraction in OOPMethod Overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This enables dynamic behavior and helps in achieving runtime polymorphism.
Imagine you’re building a system where the parent class defines a generic action — like makeSound()
— but the child classes need to provide their own unique version of this behavior. That’s where method overriding shines. It lets subclasses tailor inherited behavior to fit their specific needs.
@Override
annotation is optional but highly recommended for clarity and error detection.class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Which method runs?
}
}
Dog barks
Even though myDog
is declared as type Animal
, the makeSound()
method of the Dog
class is invoked. This is because of runtime polymorphism — Java determines the method to call based on the actual object type at runtime, not the reference type.
The @Override
annotation tells the compiler that we intend to override a method. If the method signature doesn't exactly match, the compiler throws an error. This helps catch typos and logic bugs early.
class Parent {
void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child show");
}
}
Sometimes, we want to extend the functionality of the superclass method rather than completely replacing it. We can do that using super.methodName()
.
class Vehicle {
void start() {
System.out.println("Vehicle is starting");
}
}
class Car extends Vehicle {
@Override
void start() {
super.start(); // Call the superclass method
System.out.println("Car is starting");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
Vehicle is starting
Car is starting
If a method is declared with the final
keyword in the parent class, it cannot be overridden by any subclass. Attempting to do so will result in a compile-time error.
class Shape {
final void draw() {
System.out.println("Drawing Shape");
}
}
class Circle extends Shape {
// Compilation error
void draw() {
System.out.println("Drawing Circle");
}
}
Feature | Method Overriding | Method Overloading |
---|---|---|
Definition | Redefining a method of the parent class in the child class | Defining multiple methods in the same class with the same name but different parameters |
Inheritance | Requires inheritance | No inheritance required |
Runtime/Compile-time | Runtime polymorphism | Compile-time polymorphism |
Suppose you're creating a payment system. You define a generic processPayment()
method in the superclass. Credit cards, UPI, and net banking can each override this method to implement their specific logic.
class Payment {
void processPayment() {
System.out.println("Processing generic payment");
}
}
class CreditCardPayment extends Payment {
@Override
void processPayment() {
System.out.println("Processing credit card payment");
}
}
@Override
to avoid unintentional mistakes.class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Cat extends Animal {
void speak() {
System.out.println("Cat meows");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
a.speak();
}
}
class Vehicle {
void start() {
System.out.println("Vehicle is starting");
}
}
class Car extends Vehicle {
@Override
void start() {
super.start();
System.out.println("Car is starting");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
⬅ Previous Topic
Inheritance in JavaNext Topic ⮕
Java Abstraction in OOPYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.