0% found this document useful (0 votes)
967 views

Polymorphism: Department of Computer Science & Engineering Tripura University

Polymorphism in Java allows performing the same action in different ways. There are two types of polymorphism - compile time polymorphism using method overloading, and runtime polymorphism using method overriding. Runtime polymorphism involves calling an overridden method based on the object being referred to by the reference variable. Abstract classes can contain abstract and non-abstract methods, while interfaces contain only abstract methods, and both cannot be instantiated.

Uploaded by

Abhijit Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
967 views

Polymorphism: Department of Computer Science & Engineering Tripura University

Polymorphism in Java allows performing the same action in different ways. There are two types of polymorphism - compile time polymorphism using method overloading, and runtime polymorphism using method overriding. Runtime polymorphism involves calling an overridden method based on the object being referred to by the reference variable. Abstract classes can contain abstract and non-abstract methods, while interfaces contain only abstract methods, and both cannot be instantiated.

Uploaded by

Abhijit Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Polymorphism

DEPARTMENT OF COMPUTER SCIENCE &


ENGINEERING
TRIPURA UNIVERSITY
Polymorphism
Polymorphism in java is a concept by which we can
perform a single action by different ways.
Polymorphism is derived from 2 greek words: poly
and morphs. The word "poly" means many and
"morphs" means forms. So polymorphism means
many forms.
There are two types of polymorphism in java:
 compile time polymorphism
 runtime polymorphism
We can perform polymorphism in java by method
overloading and method overriding.
Runtime Polymorphism in Java
Runtime polymorphism or Dynamic Method
Dispatch is a process in which a call to an overridden
method is resolved at runtime rather than compile-
time.
In this process, an overridden method is called through
the reference variable of a super-class. The
determination of the method to be called is based on
the object being referred to by the reference variable.
Up-casting
When reference variable of Parent class refers to the object of
Child class, it is known as upcasting. For example:

class A{}  
class B extends A{}  
A a = new B();//upcasting  
Example of Java Runtime Polymorphism
class Bike{  
  void run(){System.out.println("running");}  
}  
class Splender extends Bike{  
  void run(){System.out.println("running safely with 60km");}  
  
  public static void main(String args[]){  
    Bike b = new Splender();//upcasting  
    b.run();  
  }  

Annotations
Java Annotation is a tag that represents the metadata i.e. attached
with class, interface, methods or fields to indicate some additional
information which can be used by java compiler and JVM.
Annotations in java are used to provide additional information.

@Override : annotation assures that the subclass method is


overriding the parent class method. If it is not so, compile time
error occurs.

@SuppressWarnings

@Deprecated
class Animal{  
void eatSomething(){System.out.println("eating something");}  
}  
  
class Dog extends Animal{  
@Override  
void eatsomething()
{System.out.println("eating foods");}//should be eatSomething  
}  
  
class TestAnnotation1{  
public static void main(String args[]){  
Animal a=new Dog();  
a.eatSomething();  
}}   Output: Compile Time Error
Abstract class
A class that is declared with abstract keyword, is known
as abstract class in java. It can have abstract and non-
abstract methods (method with body).
It needs to be extended and its method implemented. It
cannot be instantiated.
Abstract method
A method that is declared as abstract and does not have
implementation is known as abstract method.

Example abstract method

abstract void printStatus();//no body and abstract 
Example
abstract class Bank{    
abstract int getRateOfInterest();    
}    
class SBI extends Bank{    
int getRateOfInterest(){return 7;}    
}    
class PNB extends Bank{    
int getRateOfInterest(){return 8;}    
}    
    
class TestBank{    
public static void main(String args[]){    
Bank b;  
b=new SBI();  
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
b=new PNB();  
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");    
}}  
Interface
An interface in java is a blueprint of a class. It has
static constants and abstract methods only.
The interface in java is a mechanism to achieve fully
abstraction. There can be only abstract methods in the
java interface not method body.
It is used to achieve fully abstraction and multiple
inheritance in Java.
Java Interface also represents IS-A relationship.
It cannot be instantiated just like abstract class.
Why use Java interface?
There are mainly three reasons to use interface. They are
given below.
It is used to achieve fully abstraction.
By interface, we can support the functionality of
multiple inheritance.
It can be used to achieve loose coupling.
 Example 
interface printable{  
void print();  
}  
  
class A6 implements printable{  
public void print(){System.out.println("Hello");}  
  
public static void main(String args[]){  
A6 obj = new A6();  
obj.print();  
 }  

Multiple inheritance in Java by interface
interface Printable{  
void print();  
}    
interface Showable{  
void show();  
}  
class A7 implements Printable,Showable{   
public void print(){System.out.println("Hello");}  
public void show(){System.out.println("Welcome");}  
public static void main(String args[]){  
A7 obj = new A7();  
obj.print();  
obj.show();  
 }  
}  
Diff.

You might also like