oops2

Object Oriented Programming Language-2



Polymorphism
  • one entity showing diffrent entity behaviours or forms.one task is performed in diffrent ways is known as polymorphism.
  • The word “poly” means many and “morphs” means forms.
  • Polymorphism allows us to perform a single action in different ways.
  •  In other words, polymorphism allows you to define one interface and have multiple implementations.
Types of polymorphism
  1. compile Time Polymorphism:
  2.  Run Time Polymorphism:
Compile Time Polymorphism:
  • called to overloaded() is resolved at compile time,according to argument type is known as "compile Time_Polymorphism".
  • It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
Example:
package com.org.oar;
public class calculator
{
int addition(int operand1,int operand2)
{
return operand1+operand2;
}
int addition(int operand1,int operand2,int operand3)
{
return operand1+operand2+operand3;
}
public class compileTimePolymorphism
{
psvm(------------------------)
{
Calculator cl=new Calculator();
c1.addition(10,20);
c1.addition(10,20,30);
}
}
output:
30
60

RunTime_Polymorphism
  • Called to "overridden()is resolved at compile time,according to instant type" is known as "Run time 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.
Advantage of RuntimeOfPolymorphism
  1. Reusability
  2. Loose Coupling
  • Almost all polymorphism running at RuntimePolymorphism.
  • same method name,same parameters,same returntype in its derived class overrides the method in base class.
Example:
public class multiplication
{
int calculator(int operand1,int operand2)
{
return operand1*operand2;
}
}
public class division
{
int calculator(int operand1,int operand2)
{
return operand1/operand2;
}
}
public class RuntimePolymorphism
{
psvm(-----------------------)
{
multiplication m1=new multiplication();
m1.calculate(10,20);
multiplication m1=new division();
m1.calculate(20,10);
}
}
ouput:
200
2


ABSTRACTION
Advantages of abstraction:
1.Reduce the complexity of "viewing the things"
2.Avoids "code-duplication" & increases "reusability"
3.Increase security of application.
4."Readability"

Program as important details provided to user:
1.Abstraction is process by "hiding irrelevent data"&"exposing the essential  feauture of object"
2.Abstraction is achieved by using abstract class&interface.