You are on page 1of 4

 

  Polymorphism 
 
 Polymorphism in Java is a concept by which we can perform a single action in
different ways 
 There are two types of polymorphism in Java 
 Compile-time polymorphism   
 Runtime polymorphism 
 
Compile-time polymorphism: 
 
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading or compile time polymorphism. 
 
class Multiplication 

//method multiply has two arguments 
int multiply(int number1, int number2) 

return number1 * number2; 

//method multiply has there arguments 
int multiply(int number1, int number2, int number3) 

return number1 * number2 * number3; 

//method multiply has two arguments of data type double 
double multiply(double number1, double number2) 

return number1 * number2; 


 
//Class OverloadingPolymorphism is sub class , 
public class OverloadingPolymorphism 

//Main method ,the execution starts here 
public static void main(String[] args) 

Multiplication multiplication = new Multiplication(); 
System.out.println(multiplication.multiply(2,2)); 
System.out.println(multiplication.multiply(2,2,2)); 
System.out.println(multiplication.multiply(2.1,2.2)); 


 
 
Runtime polymorphism: 
 
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding or runtime polymorphism in Java. 
 
class Pen  

//method write 
void write() 

System.out.println("Pen is not writing"); 


 
//class OverridingPolymorphism is sub class 
class OverridingPolymorphism extends Pen  

 
//method Write override the super class method  
void write() 

super.write(); 
System.out.println("Pen is writing"); 

 
//Main method ,the execution starts here 
public static void main(String[] args) 

 OverridingPolymorphism overridingPolymorphism = new
OverridingPolymorphism(); 
     overridingPolymorphism.write(); 


 
 
 
 
Interface: 
 The interface in Java is a mechanism to achieve abstraction. 
 An interface in Java is a blueprint of a class. 
 It has static constants and abstract methods. 
 By interface, we can support the functionality of multiple inheritance. 
//inteface  
interface College 

void run(); 

class InterfaceClass implements College 

//method run  
public void run() 

System.out.println("College is opened"); 

//Main method ,the execution starts here 
public static void main(String []args) 

 InterfaceClass interfaceClass = new InterfaceClass(); 
 interfaceClass.run(); 


 
Multiple inheritance in Java by interface: 
 
If a class implements multiple interfaces, or an interface extends multiple interfaces, it
is known as multiple inheritance. 
 
interface Sun 

void shine(); 

interface Moon 

void move(); 

class MultipleInheritance implements Sun,Moon 

public void shine() 

System.out.println("It's day"); 

public void move() 

System.out.println("It's night"); 

public static void main(String []args) 

MultipleInheritance multipleInheritance = new MultipleInheritance(); 
multipleInheritance.shine(); 
multipleInheritance.move();  


 
 

You might also like