Method Overriding in JAVA
Method Overriding in JAVA AnandMethod Overriding in Java
Java is an object-oriented programming language that allows developers to build flexible and reusable software systems. One of the important concepts in object-oriented programming is method overriding. Method overriding allows a subclass to provide its own implementation of a method that is already defined in its parent class.
Method overriding is mainly used to achieve runtime polymorphism in Java. It allows a program to determine which method should be executed during runtime depending on the object that calls the method.
For students studying the ITI COPA (Computer Operator and Programming Assistant) trade, understanding method overriding is important because it helps create dynamic and flexible Java programs.
What is Method Overriding?
Method overriding occurs when a subclass defines a method with the same name, return type, and parameter list as a method in its superclass. The subclass method replaces or overrides the implementation of the parent class method.
When the overridden method is called using an object of the subclass, the method defined in the subclass is executed instead of the one in the parent class.
Syntax of Method Overriding
class ParentClass {
void display() {
System.out.println("Parent class method");
}
}
class ChildClass extends ParentClass {
void display() {
System.out.println("Child class method");
}
}
In this example, the display() method in the child class overrides the display() method in the parent class.
Example Program of Method Overriding
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class TestOverride {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Output:
Dog barks
In this program, the Dog class overrides the sound() method defined in the Animal class.
Rules of Method Overriding
There are certain rules that must be followed when overriding methods in Java.
- The method name must be the same as the parent class method.
- The parameter list must be identical.
- The return type must be the same or compatible.
- The overriding method must not reduce the access level.
- The method must be inherited from the parent class.
Following these rules ensures that method overriding works correctly.
The @Override Annotation
Java provides a special annotation called @Override which indicates that a method is overriding a method from the parent class.
Using this annotation helps the compiler detect errors during compilation.
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
@Override
void eat() {
System.out.println("Dog eats meat");
}
}
Although the @Override annotation is optional, it is recommended for better readability and error checking.
Using the super Keyword
The super keyword is used to refer to the parent class object. It can be used to call the parent class method from the child class.
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("Dog eats meat");
}
}
In this example, the Dog class first calls the eat() method of the Animal class and then adds its own behavior.
Runtime Polymorphism
Method overriding helps achieve runtime polymorphism, which means the method call is resolved during program execution.
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class Test {
public static void main(String[] args) {
Animal a = new Cat();
a.sound();
}
}
Output:
Cat meows
Although the reference type is Animal, the method of the Cat class is executed at runtime.
Difference Between Method Overloading and Method Overriding
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name with different parameters | Subclass redefines parent class method |
| Class Location | Same class | Parent and child classes |
| Inheritance | Not required | Required |
| Execution | Compile-time | Runtime |
Advantages of Method Overriding
- Supports runtime polymorphism
- Allows modification of inherited behavior
- Improves program flexibility
- Promotes code reuse
These advantages make method overriding an important feature in object-oriented programming.
Applications of Method Overriding
Method overriding is used in many real-world Java applications.
- Graphical user interface frameworks
- Game development
- Web applications
- Enterprise software systems
Many Java frameworks rely heavily on overridden methods to allow developers to customize behavior.
Importance for ITI COPA Students
For students studying the ITI COPA trade, learning method overriding helps in understanding how object-oriented programs behave at runtime.
This concept allows students to design flexible software where different classes can provide their own implementations of methods.
Understanding method overriding also prepares students for advanced Java concepts such as polymorphism, interfaces, and framework development.
Conclusion
Method overriding is an important concept in Java that allows a subclass to redefine the behavior of a method inherited from its parent class.
It plays a key role in achieving runtime polymorphism and creating flexible software applications.
For ITI COPA students, mastering method overriding helps build a strong foundation in object-oriented programming and prepares them for developing professional Java applications.