Polymorphism
Method Overloading
Method Overriding
Constructor
Overloading
Method overloading
:
When a class having
more than one methods with same name but with different parameterization, is called method
overloading. When the method or methods are called, java segregate/select
the right method my its parameter. The
parameter could be : int , String, boolean, float , double etc.
For example:
Create a class and create method with same name but with
different parameters as below:
public class Employee {
public void methodA()
{
System.out.println("This is methodA without parameter.");
}
public void methodA(int i)
{
System.out.println("This is methodA with parameter int i.");
}
public void methodA(String name , int i)
{
System.out.println("This is methodA with String and int parameters.");
}
}
Let's call above method/s
in below main method :
public class Run {
public static void main(String[] args) {
Employee obj = new Employee();
obj.methodA(); // This is method without
parameter.
}
}
Output:
This is methodA without
parameter.
public class Run {
public static void main(String[] args) {
Employee obj = new Employee();
obj.methodA(5); // This is method with
int parameter.
}
}
Output:
This is methodA with
parameter int i.
public class Run {
public static void main(String[] args) {
Employee obj = new Employee();
obj.methodA(); // This is method without
parameter.
obj.methodA(5); // This is method with int
parameter.
obj.methodA("Rose", 1); // This is method with String and int
parameter.
}
}
Output:
This is
methodA without parameter.
This is
methodA with parameter int i.
This is methodA with String
and int parameters.
Method Overriding
When two or more classes having parent/child relationship, are having method with same name and same parameterization(
or no parameterization), is called method overriding . In such situation, when the method is called,
the child method gets precedence.
For example:
Parent class
public class Employee {
public void methodA()
{
System.out.println("This is method in parent class.");
}
}
Child class
public class Dept extends Employee {
public void methodA()
{
System.out.println("This is method in child class.");
}
}
Running above codes:
public class Run {
public static void main(String[] args) {
Dept obj = new Dept();
obj.methodA();
}
}
As you can see, both parent
class and child class are having same method. In such situation, when we call
the method , we get output from the child class as shown below:
Output:
This is method in child class.
Constructor
Overloading
When you have a class with
more than one constructors in it with different parameterizations , it is
called constructor overloading . When you call the constructor, depending upon
the parameter you pass, the respective constructor is called. The
parameter could be : int , String, boolean, float , double etc.
For example, below class is
having three constructors with same name but different parameterization.
public class Employee {
Employee()
{
System.out.println("This is constructor without parameterization.");
}
Employee(int i) {
System.out.println("This is constructor with int parameter.");
}
Employee(int i , String name) {
System.out.println("This is constructor with int and String parameters.");
}
}
Let's call the constructors as below :
public class Run {
public static void main(String[] args) {
Employee obj = new Employee(); //constructor without parameterization
}
}
Output:
This is constructor without
parameterization.
public class Run {
public static void main(String[] args) {
Employee obj = new Employee(2, "Smith"); //constructor with int and String parameters.
}
}
Output:
This is
constructor with int and String parameters.
public class Run {
public static void main(String[] args) {
Employee obj = new Employee(2, "Smith"); //constructor with int and String parameters.
Employee obj1 = new Employee(10); //constructor with int parameter.
Employee obj2 = new Employee(); // constructor without parameterization.
}
}
Output:
This is
constructor with int and String parameters.
This is
constructor with int parameter.
This is constructor without
parameterization.
No comments:
Post a Comment