Method Overriding in Java - Example Program
Method Overriding in Java - Example Program
Because changing the coding inside method is not a good idea. You should
extend that class and override it by specifying a new implementation in the
subclass.
Let’s take a simple example program to understand the need for method
overriding in Java.
Assume that a young couple wants to marry. They have fixed the date of
engagement and marriage. Let’s write code for it.
But due to Christmas day on 25 Dec, the young couple wants to change date
of marrying. So, what will you do?
You will open class and change the date of marrying. It is the worst practice in
Java because as per the object-oriented programming concept, the best
practice is that class should not open for modification.
So, If you want to add new functionality to the existing class or if you want to
modify the existing functionality of the class, you should not disturb the
existing class.
You should always write subclass of existing class and add new functionality
in subclass like this:
public class Change extends Marry
{
// Overriding method.
void marrydate()
{
System.out.println("Marry will be on 27 Dec");
}
}
public class MyClass
{
public static void main(String[] args)
{
Change obj = new Change();
obj.engagementDate();
obj.marrydate();
}
Output:
Engagement will be done on 23 Dec.
Hope that you will have understood the need or purpose of using method
overriding in java with the help of this example program.
There are mainly three reasons for which we need to create subclass of
superclass in Java. They are as follows:
But in the future, if we get a new requirement to add one more property
“address” for that student, we should make a subclass of that class and add a
new property address in the subclass.
2. To override or change the existing functionality of superclass method.
There are the following features of method overriding in Java. They are as
follows:
When you are overriding superclass method in a subclass, you need to follow
certain rules. They are as follows.
5. Subclass method’s access modifier must be the same or less than the
superclass method access modifier. Look at the below figure to understand
better.
Key Points:
1. While the overriding method, we can increase the visibility of the overriding
method but cannot reduce it. For example, if superclass method is protected,
we can override as a public method in the subclass.
a. Without annotation
In superclass
private void msg()
{
System.out.println("Hello");
}
In subclass
private void msg()
{
System.out.println("Hi")
Compile-time: OK
Runtime: OK
b. With annotation
In superclass
private void msg()
{
System.out.println("Hello");
}
In subclass
@Override
private void msg()
{
System.out.println("Hi");
Runtime: OK
So, he can modify it easily in the future. At least he/she will understand that
there is some relationship between the two methods.
But the method in superclass and subclasses have the same signatures. Then
how JVM decides which method is called by the programmer?
In other words, you always check the reference variable is pointing to the
object of which class?
Output:
A-m1
A-m2
A-m1
B-m2
B-m3
A-m1
B-m2
Explanation:
1. a.m1() will call m1() method of class A because the reference variable ‘a’ is
pointing to the object of class A. Similarly, a.m2() will also call m2() method of
class A.
2. b.m1() will call m1() method of class B because m1() of class A is available
by default to class B due to inheritance and the reference variable ‘b’ is
pointing to the objects of class B.
Similarly, b.m2() will call m2() method of class B because ‘b’ is pointing to the
objects of the class. b.m3() will call the method of class B.
3. A a = new B() tells that the reference variable ‘a1’ of class A is pointing to
the objects of class B. That is, the parent reference variable can hold child
class object. a1.m1() will call the m1() method of class B because it is
available by default.
a1.m2() will call m2() method of class B because the reference variable a1 is
pointing to objects of class B.
At runtime, JVM will check that the reference variable is pointing to the which
class object? parent class object or child class object.
If it is pointing to the parent class object and m2() method is available in the
parent class, it will execute the m2() method of the parent class.
But if it is pointing to the child class object, JVM will immediately check that in
the child class, the m2 method is overriding or not.
If it is not overriding in the child class, JVM will call the default parent m2
method available in the child class.
Thus, JVM will execute the parent method only. If the m2 is overriding in the
child class, at the runtime, JVM will execute the child class method based on
the runtime object (new B()).
In the overriding method, the resolution is always based on the runtime object
by JVM.
Output:
Name of my college is PIET
It was established in 2001
Output:
1. A a1 = new B();
2. a1.m2();
3. The above line is compiled as class A has m2().
4. At runtime, we will get the output of m1 which is there at class B.
5. As at compile time, we think m2 of class A will get called but at
runtime m2 of class B got executed because, in method
overriding, method resolution is always based on runtime object
(new B()).
}
public class XY
{
public static void main(String[] args)
{
Y y = new Y();
y.m1(); // Compile time error because method m1() from type X is not visible
in class Y.
}
Let’s make a program where we will declare the overriding method as final in
the subclass and will try to override method in sub – sub class.
Note: Method overriding occurs only when the signatures of the super and
subclasses methods are identical. If they are not, then both methods are
simply overloaded methods.