Method and Constructor
Method Overloading, Constructor Overloading and Method Overriding in Java Polymorphism is one of the important concepts in Object-Oriented Programming (OOP). It allows the same method name or interface to behave differ
Method Overloading, Constructor Overloading and Method Overriding in Java
Polymorphism is one of the important concepts in Object-Oriented Programming (OOP). It allows the same method name or interface to behave differently in different situations. Java supports polymorphism through method overloading and method overriding. Constructor overloading is another useful feature that provides different ways to initialize objects.
1. Method Overloading
Method overloading means creating multiple methods with the same name but different parameters within a class. The parameters can differ in number, data type, or order.
Example:
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
double add(double a, double b) {
return a + b;
}
}
Here, the add() method is defined three times with different parameters. Java automatically identifies which method to execute based on the arguments passed during the method call.
Important points:
- Method names must be the same.
- Parameters must be different.
- Changing only the return type is not method overloading.
- It is an example of compile-time polymorphism.
2. Constructor Overloading
Constructor overloading means creating multiple constructors within the same class with different parameters. It allows us to initialize objects in different ways.
Example:
class Student {
String name;
int age;
Student() {
name = "Unknown";
age = 0;
}
Student(String n) {
name = n;
}
Student(String n, int a) {
name = n;
age = a;
}
}
Here, the Student class contains three constructors. When an object is created, Java selects the appropriate constructor based on the arguments passed.
For example:
Student s1 = new Student();
Student s2 = new Student("Arun");
Student s3 = new Student("Kumar", 22);
Each object is initialized differently.
Important points:
- Constructor names must match the class name.
- Constructors do not have return types.
- Constructors can have different parameter lists.
- Constructors cannot be overridden.
3. Method Overriding
Method overriding happens when a child class provides its own implementation of a method that is already defined in its parent class. It allows the child class to change the inherited method's behaviour.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
Here, the Dog class inherits the Animal class using the extends keyword. Both classes contain the sound() method, but the Dog class provides its own implementation.
Animal a = new Animal();
Dog b = new Dog();
a.sound();
b.sound();
Output:
Animal makes a sound
Dog barks
Although the reference variable is of type Animal, the actual object is Dog. Java executes the overridden method based on the actual object at runtime. This is called runtime polymorphism or dynamic method dispatch.
Important points:
- Inheritance is required.
- Method names and parameters must match.
- The return type must be the same or covariant.
- The
@Overrideannotation helps identify overriding mistakes.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.