METHOD OVERLOADING
& OVERRIDNG IN JAVA
PREPARED BY
Dr.P.Kavitha
Assistant Professor
Dept. of Computer Applications –
PG
School of computing
Sciences
VISTAS.
Method Overloading in Java
In Java, Method Overloading allows us to define multiple methods with the same name
but different parameters within a class. This difference can be in the number of parameters,
the types of parameters, or the order of those parameters.
Method overloading in Java is also known as Compile time Polymorphism ,Static
Polymorphism, or Early binding, because the decision about which method to call is
made at compile time.
When there are overloaded methods that accept both a parent type and a child type, and the
provided argument could match either one, Java prefers the method that takes the more
specific (child) type. This is because it offers a more better match.
Key features of Method
Overloading
Multiple methods can share the same name in a class when their parameter lists are
different.
Overloading is a way to increase flexibility and improve the readability of code.
Overloading does not depend on the return type of the method, two methods cannot be
overloaded by just changing the return type.
Example
public class Sum {
// Overloaded sum()
// This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum()
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum()
// This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Output
30
60
31.0
Advantages of Method Overloading
Method overloading improves the Readability and reusability of the program.
Method overloading reduces the complexity of the program.
Using method overloading, programmers can perform a task efficiently and effectively.
Using method overloading, it is possible to access methods performing related functions
with slightly different arguments and types.
Objects of a class can also be initialized in different ways using the constructors.
Method Overriding in Java
Method Overriding is a type of runtime polymorphism. In method overriding, a method in a
derived class has the same name, return type, and parameters as a method in its parent class.
The derived class provides a specific implementation for the method that is already defined in
the parent class.
Key Points:
Method Overriding requires inheritance.
In method overriding, method signature including name and parameter must match exactly.
In method overriding, return type must be same.
In method overriding, private and final methods can not be overridden.
@Override annotation ensures correct overriding.
Example
import java.io.*;
// Base Class
class Animal {
void eat() {
System.out.println("eat() method of base class");
System.out.println("Animal is eating.");
}
}
// Derived Class
class Dog extends Animal {
@Override
void eat() {
System.out.println("eat() method of derived class");
System.out.println("Dog is eating.");
}
// Method to call the base class method
void eatAsAnimal() {
super.eat();
}
}
// Driver Class
class Methodoverride {
// Main Function
public static void main(String args[]) {
Dog d1 = new Dog();
Animal a1 = new Animal();
// Calls the eat() method of Dog class
d1.eat();
// Calls the eat() method of Animal class
a1.eat();
// Polymorphism: Animal reference pointing to Dog object
Animal animal = new Dog();
// Calls the eat() method of Dog class
animal.eat();
// To call the base class method, you need to use a Dog reference
((Dog) animal).eatAsAnimal();
}
}
Output
eat() method of derived class
Dog is eating.
eat() method of base class
Animal is eating.
eat() method of derived class
Dog is eating.
eat() method of base class
Animal is eating.
Advantages of Method Overriding
The advantages of Method Overriding are listed below:
It allows for dynamic method dispatch which enhance the
flexibility.
It allows changes to specific class behaviors without
changing the entire class hierarchy.
We can easily extend classes and modify behaviors
without altering the parent class.
Method Overloading vs Method Overriding
Method Overloading Method Overriding
Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism.
Method overriding is used to grant the specific
Method overloading helps to increase the readability
implementation of the method that is already provided
of the program.
by its parent class or superclass.
It occurs within the same class or across different It is performed in two classes with inheritance
classes. relationships.
Method overloading does not require inheritance. Method overriding always needs inheritance.
In method overloading, methods must have the same In method overriding, methods must have the same
name but different signatures. name and the same signature.
In method overloading, the return type can or can not In method overriding, the return type must be the same
be the same, but the parameter list must differ. or covariant.
Static binding is used for overloaded methods. Dynamic binding is used for overriding methods.
Private and final methods can be overloaded. Private and final methods can't be overridden.
The argument list should be different while doing The argument list should be the same in method
method overloading. overriding.
Thank You