Method Overriding in Java
Method overriding in Java is a feature that allows a subclass to provide a specific implementation for a method that is already defined in its superclass. The method in the subclass should have the same name, return type, and parameters as the method in the superclass. Overriding is used to achieve runtime polymorphism, allowing a subclass to modify or enhance the behavior of a superclass method.
Key Points about Method Overriding:
- Same Method Signature: The overridden method must have the same name, return type, and parameter list as the method in the superclass.
- Inheritance: Overriding can only occur in a subclass, which inherits a method from its superclass.
- Annotations: It is a good practice to use the
@Override
annotation to indicate that a method is intended to override a method in the superclass. - Access Modifiers: The access level of the overriding method cannot be more restrictive than the overridden method. For example, if the superclass method is
public
, the subclass method cannot beprotected
orprivate
.
Example:
// Superclass
class Animal {
// Method to be overridden
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass
class Dog extends Animal {
// Overriding the sound method
@Override
void sound() {
System.out.println("Dog barks");
}
}
// Main class to test the method overriding
public class Main {
public static void main(String[] args) {
// Creating an instance of Animal
Animal myAnimal = new Animal();
myAnimal.sound(); // Calls the method in Animal class
// Creating an instance of Dog
Dog myDog = new Dog();
myDog.sound(); // Calls the overridden method in Dog class
// Using Animal reference to Dog object
Animal myPet = new Dog();
myPet.sound(); // Calls the overridden method in Dog class due to polymorphism
}
}
Why Use Method Overriding?
- Runtime Polymorphism: Method overriding allows Java to support runtime polymorphism. This enables dynamic method dispatch, where the method to be invoked is determined at runtime based on the object's actual type.
- Behavior Customization: Subclasses can customize or extend the behavior of methods defined in the superclass to provide specific functionality.
- Code Reusability: By reusing method names and signatures, code becomes more maintainable and easier to understand.
Program to try
// Superclass
class BankAccount {
double balance;
// Constructor
BankAccount(double balance) {
this.balance = balance;
}
// Method to be overridden
double calculateInterest() {
return balance * 0.02; // Default interest rate
}
}
// Subclass SavingsAccount
class SavingsAccount extends BankAccount {
// Constructor
SavingsAccount(double balance) {
super(balance);
}
// Overriding the calculateInterest method
@Override
double calculateInterest() {
return balance * 0.03; // Savings account interest rate
}
}
// Subclass CheckingAccount
class CheckingAccount extends BankAccount {
// Constructor
CheckingAccount(double balance) {
super(balance);
}
// Overriding the calculateInterest method
@Override
double calculateInterest() {
return balance * 0.01; // Checking account interest rate
}
}
// Main class to test the method overriding
public class Main {
public static void main(String[] args) {
// Create instances of each account type
BankAccount savings = new SavingsAccount(1000);
BankAccount checking = new CheckingAccount(1000);
// Display interest for each account
System.out.println("Savings Account Interest: " + savings.calculateInterest());
System.out.println("Checking Account Interest: " + checking.calculateInterest());
}
}
Comments
Post a Comment