If the same method is defined in both the superclass and
the subclass, then the method of the subclass class
overrides the method of the superclass. This is known as
method overriding.
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
@Override
public void displayInfo() {
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
In the above program, the displayInfo() method is present in both
the Animal superclass and the Dog subclass.
When we call displayInfo() using the d1 object (object of the subclass),
the method inside the subclass Dog is called. The displayInfo() method of
the subclass overrides the same method of the superclass.
Notice the use of the @Override annotation in our example. In Java,
annotations are the metadata that we used to provide information to the
compiler. Here, the @Override annotation specifies the compiler that the
method after this annotation overrides the method of the superclass.
It is not mandatory to use @Override . However, when we use this, the
method should follow all the rules of overriding. Otherwise, the compiler will
generate an error.
Java Overriding Rules
Both the superclass and the subclass must have the same method
name, the same return type and the same parameter list.
We cannot override the method declared as final and static .
super Keyword in Java Overriding
A common question that arises while performing overriding in Java is:
Can we access the method of the superclass after overriding?
Well, the answer is Yes. To access the method of the superclass from the
subclass, we use the super keyword.
Example 2: Use of super Keyword
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Dog extends Animal {
public void displayInfo() {
super.displayInfo();
System.out.println("I am a dog.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Run Code
Output:
I am an animal.
I am a dog.
In the above example, the subclass Dog overrides the
method displayInfo() of the superclass Animal .
When we call the method displayInfo() using the d1 object of
the Dog subclass, the method inside the Dog subclass is called; the method
inside the superclass is not called.
Inside displayInfo() of the Dog subclass, we have
used super.displayInfo() to call displayInfo() of the superclass.
OVERLOADING
In Java, two or more methods may have the same name if they differ in
parameters (different number of parameters, different types of parameters,
or both). These methods are called overloaded methods and this feature is
called method overloading. For example:
void func() { ... }
void func(int a) { ... }
float func(double a) { ... }
float func(int a, float b) { ... }
Here, the func() method is overloaded. These methods have the same
name but accept different arguments.
Note: The return types of the above methods are not the same. It is
because method overloading is not associated with return types.
Overloaded methods may have the same or different return types, but they
must differ in parameters.
Why method overloading?
Suppose, you have to perform the addition of given numbers but there can
be any number of arguments (let’s say either 2 or 3 arguments for
simplicity).
In order to accomplish the task, you can create two methods sum2num(int,
int) and sum3num(int, int, int) for two and three parameters
respectively. However, other programmers, as well as you in the future may
get confused as the behavior of both methods are the same but they differ
by name.
The better way to accomplish this task is by overloading methods. And,
depending upon the argument passed, one of the overloaded methods is
called. This helps to increase the readability of the program.
How to perform method overloading in Java?
Here are different ways to perform method overloading:
1. Overloading by changing the number of parameters
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
private static void display(int a, int b){
System.out.println("Arguments: " + a + " and " + b);
}
public static void main(String[] args) {
display(1);
display(1, 4);
}
}
Output:
Arguments: 1
Arguments: 1 and 4
2. Method Overloading by changing the data type of
parameters
class MethodOverloading {
// this method accepts int
private static void display(int a){
System.out.println("Got Integer data.");
}
// this method accepts String object
private static void display(String a){
System.out.println("Got String object.");
}
public static void main(String[] args) {
display(1);
display("Hello");
}
}
Output:
Got Integer data.
Got String object.
Here, both overloaded methods accept one argument. However, one
accepts the argument of type int whereas other accepts String object.
Important Points
Two or more methods can have the same name inside the same
class if they accept different arguments. This feature is known as
method overloading.
Method overloading is achieved by either:
o changing the number of arguments.
o or changing the data type of arguments.
It is not method overloading if we only change the return type of
methods. There must be differences in the number of parameters.