Polymorphism: Department of Computer Science & Engineering Tripura University
Polymorphism: Department of Computer Science & Engineering Tripura University
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.
@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.
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.