Polymorphism in Java

0 Comments
Source

Polymorphism mean the condition of occurring in several different forms
Polymorphism in Java has two types: Compile time polymorphism (static binding) and Runtime polymorphism (dynamic binding). Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism. Polymorphism represents a IS-A relationship

Consider a class of Vehicle and let bike be a subclass of Vehicl. So, a bike IS-A vehical.

Overloading:
Method overloading or static binding means that there are several method having same name but different number of parameters. At compile tme Java knows which method to call by their parameters

Overriding:
Method overriding or dynamic binding means that a Sub class change a specific method which is already defined in Super class or Parent class

class Vehicle{

public void move(){

    System.out.println(“Vehicle's method move trigger!!”);

}

}

class Bike extends Vehicle{

public void move(){

    System.out.println(“Bike's method move trigger!!”);

}

}

class Test{

public static void main(String[] args){

   Vehicle v=new MotorBike();

   v.move(); // prints MotorBike method

   v=new Vehicle();

   v.move(); // prints Vehicles method

}

}


Leave a Reply

Your email address will not be published.