Java Method Overriding
What is Java Overriding?
- Java Method Overriding is a mechanism by which a subclass provides its own implementation of a method that is already provided by its parent class.
- To override a method in Java, the subclass must have the same method name, parameters, and return type as the method in the parent class.
- When a subclass overrides a method, it provides its own implementation of that method instead of the implementation provided by its parent class.
- To understand Java Overriding, Read the Complete Article.
Java Overriding Rules:
- When overriding a method in Java, there are several rules that must be followed:
- The name of the method in the subclass must exactly match the name of the method in the superclass.
- The parameters of the method in the subclass must match the parameters of the method in the superclass.
- The return type of the method in the subclass must be the same or a subtype of the return type of the method in the superclass.
- The access level of the method in the subclass must be the same or less restrictive than the access level of the method in the superclass.
- The exception thrown by the method in the subclass must be the same or a subclass of the exception thrown by the method in the superclass.
- The method in the subclass cannot be declared as static, because static methods are not overridden, they are hidden
- If any of these rules are violated, the code will not compile and will result in a compilation error.
Super Keyword in Java Overriding :
In Java, the super keyword is used to refer to the superclass of the current subclass. When overriding a method in a subclass, the super keyword can be used to call the overridden method in the superclass.
Access Specifiers in Method Overriding :
Access specifiers in Java control the visibility of classes, methods, and other class members. When overriding a method in Java, the access level of the overriding method must be the same or less restrictive than the access level of the overridden method in the superclass.
Here are the access specifiers available in Java:
public: Accessible from anywhere.
protected: Accessible within the same package or by subclasses.
default (no specifier): Accessible within the same package only.
private: Accessible within the same class only.
When overriding a method, the access level of the method in the subclass cannot be more restrictive than the access level of the method in the superclass.
Let’s look at the Java Method Overriding to perform certain operations.
Example 1: Example in Method Overriding
Run
class Animal { public void makeSound() { System.out.println("Animal is making a sound"); } } class Dog extends Animal { @Override public void makeSound() { System.out.println("Dog is barking"); } } class Cat extends Animal { @Override public void makeSound() { System.out.println("Cat is meowing"); } } public class Main { public static void main(String[] args) { Animal animal = new Animal(); animal.makeSound(); // Output: Animal is making a sound Dog dog = new Dog(); dog.makeSound(); // Output: Dog is barking Cat cat = new Cat(); cat.makeSound(); // Output: Cat is meowing } }
Output
Animal is making a sound Dog is barking Cat is meowing
Explanation:
In this program, we have a parent class Animal that has a makeSound() method that prints "Animal is making a sound".
The Dog and Cat classes both extend the Animal class and override the makeSound() method with their own implementations. When we create instances of Dog and Cat and call their makeSound() methods, we see that their respective implementations of the method are called instead of the one defined in the Animal class.
Example 2 :Super Keyword in Java Overriding.
Run
class Animal { public void makeSound() { System.out.println("Animal is making a sound"); } } class Dog extends Animal { @Override public void makeSound() { super.makeSound(); // Call the makeSound() method in the superclass System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); } }
Output
Animal is making a sound Dog is barking
Explanation:
In this example, the Dog class overrides the makeSound() method from the Animal class.
The super.makeSound() statement in the Dog class calls the makeSound() method in the Animal class, and then the Dog class adds its own implementation of the method. When we create an instance of the Dog class and call its makeSound() method, we see that both the Animal class's implementation and the Dog class's implementation of the method are called.
The super keyword can also be used to access variables or methods in the superclass that have been hidden by variables or methods with the same name in the subclass.
Example 3 : Access Specifiers in Method Overriding
Run
class Animal { public void makeSound() { System.out.println("Animal is making a sound"); } } class Dog extends Animal { @Override public void makeSound() { super.makeSound(); // Call the makeSound() method in the superclass System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.makeSound(); } }
Output
Animal is making a sound Animal is sleeping Animal is eating Dog is barking Dog is sleeping Dog is eating
Explanation:
In this example, the Animal class has four methods with different access modifiers. The Dog class overrides the makeSound() method with a public method, the sleep() method with a protected method, and the eat() method with a public method.
The Dog class defines a new private run() method with the same name as the private run() method in the Animal class, but it cannot override it since it has a more restrictive access modifier.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment