Compiletime Polymorphism
Compiletime polymorphism:-
- Polymorphism means single name and multiple meanings.
- In this type of polymorphism, we can use a concept called as function overloading.
- Function overloading in which we have to provide multiple definition of a function having same name but different syntax means no. of parameters, sequence of parameters, type of parameters may vary to overload that function.
- We cannot consider the return value of that function while overloading a function.
- Function overloading in java is almost similar as a function overloading in C++.
- It is called as compiletime polymorphism, because, all of the function and its definition is bind at compiletime.
e.g.
class demo
{
public int i,j;
public int fun()
{
System.out.println(“inside a 1st fun”);
}
void fun(int x, int y)
{
System.out.println(“inside 2nd fun”);
}
public float fun(float f, int I, double d)
{
System.out.println(“inside 3rd fun”);
}
}
public class hello
{
public static void main(String args[])
{
demo d = new demo();
d.fun(); // inside 1st fun
d.fun(10,20); // inside 2nd fun
d.fun(10.1,20,3.14); // inside 3rd fun
}
}
Technical example of function overloading:-
int I = -15,j;
float x = -3.14,y;
long m = -123456,n;
j = abs(i); // 15
y = fabs(x); // 3.14
n = labs(m); // 123456
- All the above 3 functions return absolute value of input parameters.
- In this example working of all the 3 functions are same but it accepts different arguments
- Due to which programmer has to remember names of all those 3 functions.
- To avoid this drawback we can provide same name and different types of arguments by using a concept called as function overloading.
e.g.
int my_abs(int x)
{
System.out.println(“inside my_abs int”);
}
float my_abs(float x)
{
System.out.println(“inside my_abs float”);
}
long my_abs(long x)
{
System.out.println(“inside my_abs long”);
}
- In this example, we used same name for defining all the function but change is only in the type of input arguments.
- The return values are also changed but we can not consider the return valu for function overloading.
Dynamic Call Dispatching:-
- Dynamic call dispatching means writing a virtual function and call of that function and definition of that function is bind at runtime.
- But there is no concept of a virtual function in java but still java support runtime polymorphism by providing dynamic call dispatching method.
e.g.
class base
{
void fun()
{
System.out.println(“fun of base”);
}
}
class derived extends base
{
void fun()
{
System.out.println(“fun of derived”);
}
}
public class demo
{
public static void main(String args[])
{
base b1 = new base();
derived d1 = new derived();
base b2;
derived d2;
b2 = d1;
b2=b1;
b1.fun(); // fun of base
d1.fun(); // fun of derived
b2.fun(); // fun of derived
b2=d1;
b2.fun();
d2=b1; // error
}
}
Difference between overloading, overriding and redefinition:
Function overloading:-
- When we write a function with same name having different no of parameters or different sequence of parameters then that functions are called as overloaded functions.
- All the overloaded functions must be inside the same class.
- Function call and its binding is performed at compiletime.
- The name of each function is changed by the compiler by using a technique called as name mangling.
e.g.
class demo
{
int fun(int i)
{
System.out.println(“inside 1st fun of demo”);
}
int fun(int i, int j)
{
System.out.println(“inside 2nd fun of demo”);
}
}
- In this case, fun is an overloaded by considering no of parameters to that function.
Function overriding:-
- In this case, we have to define a function across the related classes.
- While writing such a function the syntax i.e. no of parameters and return value of the function must be same.
- The definition of method overriding is achieved by a reference in java or reference or pointer in C++.
- When we call this function, function calls and its binding is performed at runtime and is called as dynamic call dispatch in java.
- To achieve this technique we have to use virtual keyword in C++.
- But in java there is no such keyword is required.
e.g.
class base
{
int fun(int i)
{
System.out.println(“inside fun of base”);
}
}
class derived extends base
{
int fun(int i)
{
System.out.println(“inside fun of derived”);
}
}
- In this case, fun is overridden.
base b = new derived();
b.fun(10);
- fun() of derived class is called because base class reference is referring to derived class object.
Function redefinition:-
- when we define a function inside a base class with appropriate syntax and same name function is written in derived class by varying no of parameters and return value and that is called as function redefinition.
e.g.
class base
{
int fun(int i)
{
System.out.println(“inside fun of base”);
}
}
class derived extends base
{
int fun(int I, int j)
{
System.out.println(“inside fun of derived”);
}
}
For more reading about technology news in singapore and seo to online marketing do view more about other pages.