Method Overriding in Java
In parent class we have a method, and the same method is declared in child class is known as method overridding. Before we have seen method overloading, now we will discuss method overriding in detail.
Rules of Method overriding:
- method in child class must have the same name as method in parent class.
- method must have the same no. of parameters
- method must have the same data types of parameters
- method must have the same return type
- must be a IS-A relationship i.e. inheritance
- The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class.
- private, static and final methods cannot be overridden.
- Binding of overridden methods happen at run time which is known as dynamic binding.
We will look into the implementation of all these rules with examples.
First, understand the simple usage of method overriding with the below example.
We have 2 classes called Color and TestColor. Create an object of Color class and call the method to get the color.I want red color, but it will print green as mentioned in the method getColor.
class Color{ void getColor(){ System.out.println("I will get green"); } } public class TestColor extends Color{ public static void main(String[] args) { TestColor color = new TestColor(); color.getColor(); } }
I wanted the color Red, but it prints green because that is by default output of the method in parent class Color which is extended by the child class TestClass.
How to resolve this issue or say how to get red color?
You have to override the method and use specific implementation.
class Color{ // parent class void getColor(){ System.out.println("I will get green"); } } public class TestColor extends Color{ //child class extending parent class public static void main(String[] args) { TestColor color = new TestColor(); color.getColor(); } void getColor(){ System.out.println("I will get Red"); } }
Let’s understand with one more practical example.
Consider we have one general class Company which returns the notice period in months. Now, notice period may vary from company to company. Say IBM has 1 month notice period and Yahoo another class has 2 months notice perios. Now, how to set the specific implementation as per need of different companies.
package com.test; class Company{ // parent class int noticePeiodInMonths(){ return 1; } } class IBM extends Company{ // parent class int noticePeiodInMonths(){ return 2; } } class Yahoo extends Company{ // parent class int noticePeiodInMonths(){ return 3; } } public class CheckNoticePeriod{ public static void main(String[] args) { IBM ibm = new IBM(); Yahoo y = new Yahoo(); System.out.println("Notice period in IBM "+ibm.noticePeiodInMonths()+" months"); System.out.println("Notice period in Yahoo "+y.noticePeiodInMonths()+" months"); } }
Notice period in Yahoo 3 months
Can we override static methods?
No, we can not override static method. Reason is that static method is bound with class whereas instance method is bound with object.
Can we override main method?
No, because main method is static.
1 Response
[…] In the previous session, we have seen the usage of method overriding. […]