NIMIEXP4JAVA
NIMIEXP4JAVA
Theory :
Polymorphism:
Polymorphism:
Polymorphism means "many forms." In Java, it allows one action to be
performed in dif erent ways.
Inheritance plays a key role in achieving polymorphism, where objects of different
classes can be treated as objects of a common superclass.
This allows for more flexible and extensible code. i.e Polymorphism is extensively
used in implementing inheritance.
Implementation of inheritance is performed at compile time. It is at compile time
that the child class inherits the public data members and methods of the base class.
Method Overloading
When there are multiple methods with the same name but different parameters then
these functions/methods are said to be overloaded.
Methods can be overloaded by changes in the number of arguments or/and a
change in the type of arguments.
Compile-Time Polymorphism
It is also known as static polymorphism. This type of polymorphism is achieved by
function overloading or operator overloading.
Runtime Polymorphism:
➢ It is also known as Dynamic Method Dispatch. It is a process in which a function
call to the overridden method is resolved at Runtime.
➢ This type of polymorphism is achieved by Method Overriding.
Method overriding, on the other hand, occurs when a derived class has a definition for
one of the member functions of the base class. That base function is said to be
overridden.
PROGRAM 1:Overloading by Different Number of Arguments
class SimpleMultiplication
{
int multiply(int a, int b)
{
return a*b;
}
int multiply(int a, int b, int c)
{
return a*b*c;
}
}
class Demo
{
public static void main(String args[])
{
SimpleMultiplication num = new
SimpleMultiplication();
System.out.println(num.multiply(10, 20));
System.out.println(num.multiply(10, 20, 30));
}
}
OUTPUT:
//Override
public double calculateSalary() {
return basicSalary + BONUS;
}
}
manager.displaySalary();
clerk.displaySalary();
}
}
OUTPUT: