Polymorphism In Java-10 Program

--help cse
5 min readMar 20, 2022

Polymorphism In Java

Polymorphism in Java is a concept by which we can perform a single action in 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 and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.

If you overload a static method in Java, it is the example of compile time polymorphism. Here, we will focus on runtime polymorphism in java.

Method Overloading: When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in the number of arguments or/and a change in the type of arguments.

Types of polymorphism

In Java polymorphism is mainly divided into two types:

Compile-time Polymorphism

Runtime Polymorphism

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.

upcasting in Runtime Polymorphism:the reference variable of Parent class refers to the object of Child class, it is known as upcasting.

Program 1:Rutime example with upcasting

FileName:Bike.java

CODE:

class Bike{

void run(){System.out.println(“running”);}

}

class Splendor extends Bike{

void run(){System.out.println(“running safely with 60km”);}

public static void main(String args[]){

Bike b = new Splendor();//upcasting

b.run();

}

}

OUTPUT:

running safely with 60km

Program 2:Simple polymorphism through using different method.

Filename:Bank.java

CODE:

class Bank{

float getRateOfInterest(){return 0;}

}

class SBI extends Bank{

float getRateOfInterest(){return 8.4f;}

}

class ICICI extends Bank{

float getRateOfInterest(){return 7.3f;}

}

class AXIS extends Bank{

float getRateOfInterest(){return 9.7f;}

}

class TestPolymorphism{

public static void main(String args[]){

Bank b;

b=new SBI();

System.out.println(“SBI Rate of Interest: “+b.getRateOfInterest());

b=new ICICI();

System.out.println(“ICICI Rate of Interest: “+b.getRateOfInterest());

b=new AXIS();

System.out.println(“AXIS Rate of Interest: “+b.getRateOfInterest());

}

}

OUTPUT:

SBI Rate of Interest: 8.4

ICICI Rate of Interest: 7.3

AXIS Rate of Interest: 9.7

Program 3:Simple Poly morphism example

Filename:Shape.java

CODE:

class Shape{

void draw(){System.out.println(“drawing…”);}

}

class Rectangle extends Shape{

void draw(){System.out.println(“drawing rectangle…”);}

}

class Circle extends Shape{

void draw(){System.out.println(“drawing circle…”);}

}

class Triangle extends Shape{

void draw(){System.out.println(“drawing triangle…”);}

}

class TestPolymorphism2{

public static void main(String args[]){

Shape s;

s=new Rectangle();

s.draw();

s=new Circle();

s.draw();

s=new Triangle();

s.draw();

}

}

OUTPUT:

drawing rectangle...drawing circle...drawing triangle...

Program 4: Java Runtime Polymorphism Example: Animal

Filename:Animal.java

CODE:

class Animal{

void eat(){System.out.println(“eating…”);}

}

class Dog extends Animal{

void eat(){System.out.println(“eating bread…”);}

}

class Cat extends Animal{

void eat(){System.out.println(“eating rat…”);}

}

class Lion extends Animal{

void eat(){System.out.println(“eating meat…”);}

}

class Animalpoly{

public static void main(String[] args){

Animal a;

a=new Dog();

a.eat();

a=new Cat();

a.eat();

a=new Lion();

a.eat();

}}

OUTPUT:

eating bread...eating rat...eating meat...

Program 5: Java Runtime Polymorphism with Data Member

Filename:Bike.java

CODE:

class Bike{

int speedlimit=90;

}

class Honda3 extends Bike{

int speedlimit=150;

public static void main(String args[]){

Bike obj=new Honda3();

System.out.println(obj.speedlimit);//90

}

OUTPUT:

90

Program 6: Java Runtime Polymorphism with Multilevel Inheritance

CODE:

class Animal{

void eat(){System.out.println(“eating”);}

}

class Dog extends Animal{

void eat(){System.out.println(“eating fruits”);}

}

class BabyDog extends Dog{

void eat(){System.out.println(“drinking milk”);}

public static void main(String args[]){

Animal a1,a2,a3;

a1=new Animal();

a2=new Dog();

a3=new BabyDog();

a1.eat();

a2.eat();

a3.eat();

}

}

OUTPUT:

eatingeating fruitsdrinking Milk

Program 7:

CODE:

Filename:Babydog1.cpp

class Animal{

void eat(){System.out.println(“animal is eating…”);}

}

class Dog extends Animal{

void eat(){System.out.println(“dog is eating…”);}

}

class BabyDog1 extends Dog{

public static void main(String args[]){

Animal a=new BabyDog1();

a.eat();

}}

OUTPUT:

Dog is eating

Since, BabyDog is not overriding the eat() method, so eat() method of Dog class is invoked.

static binding

When type of the object is determined at compiled time(by the compiler), it is known as static binding.

If there is any private, final or static method in a class, there is static bindin

Program 8: static binding in poly morphism example

CODE:

Filename:Dog.cpp

class Dog{

private void eat(){System.out.println(“dog is eating…”);}

public static void main(String args[]){

Dog d1=new Dog();

d1.eat();

}

}

OUTPUT:

dog is eating…

Dynamic binding

When type of the object is determined at run-time, it is known as dynamic binding.

Program 9: Example of dynamic binding

CODE:

Filename:Animal.cpp

class Animal{

void eat(){System.out.println(“animal is eating…”);}

}

class Dog extends Animal{

void eat(){System.out.println(“dog is eating…”);}

public static void main(String args[]){

Animal a=new Dog();

a.eat();

}

}

OUTPUT:

Output:dog is eating...

Type 2: Compile-time polymorphism

It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.

Program 10: Method overloading By using Different Types of Arguments

CODE:

Filename:MAIN.cpp

// Java Program for

// Class 1

// Helper class

class Helper {

// Method with 2 integer parameters

static int Multiply(int a, int b)

{

// Returns product of integer numbers

return a * b;

}

// Method 2

// With same name but with 2 double parameters

static double Multiply(double a, double b)

{

// Returns product of double numbers

return a * b;

}

}

// Class 2

// Main class

class MAIN {

// Main driver method

public static void main(String[] args)

{

// Calling method by passing

// input as in arguments

System.out.println(Helper.Multiply(2, 4));

System.out.println(Helper.Multiply(5.5, 6.3));

}

}

OUTPUT:

834.65

Here,in function one we are passing two argument so it invoke first method.

And in second we are passing folat numbers so second methos is invoked.

Program 11:Method overloading in polymorphism using different numbers of arguments.

CODE:

Filename:MAIN.cpp

// Java program for Method Overloading

// by Using Different Numbers of Arguments

// Class 1

// Helper class

class Helper {

// Method 1

// Multiplication of 2 numbers

static int Multiply(int a, int b)

{

// Return product

return a * b;

}

// Method 2

// // Multiplication of 3 numbers

static int Multiply(int a, int b, int c)

{

// Return product

return a * b * c;

}

}

// Class 2

// Main class

class MAIN {

// Main driver method

public static void main(String[] args)

{

// Calling method by passing

// input as in arguments

System.out.println(Helper.Multiply(2, 4));

System.out.println(Helper.Multiply(2, 7, 3));

}

}

OUTPUT:

8

42

Note*:it is similar example if we pass 2 argument then first method is invoked and if we pass 3 arguments pass then second arguments is executed.

Program 12: Method Overriding through a runtime poly.

CODE:

Filename:MAIN.cpp

// Java Program for Method Overriding

// Class 1

// Helper class

class Parent {

// Method of parent class

void Print()

{

// Print statement

System.out.println(“parent class”);

}

}

// Class 2

// Helper class

class subclass1 extends Parent {

// Method

void Print() { System.out.println(“subclass1”); }

}

// Class 3

// Helper class

class subclass2 extends Parent {

// Method

void Print()

{

// Print statement

System.out.println(“subclass2”);

}

}

// Class 4

// Main class

class MAIN {

// Main driver method

public static void main(String[] args)

{

// Creating object of class 1

Parent a;

// Now we will be calling print methods

// inside main() method

a = new subclass1();

a.Print();

a = new subclass2();

a.Print();

}

}

OUTPUT:

subclass1subclass2

Note*: Here in this program, When an object of child class is created, then the method inside the child class is called. This is because The method in the parent class is overridden by the child class. Since The method is overridden, This method has more priority than the parent method inside the child class. So, the body inside the child class is executed.

Thank you .please practise more and more .If you any doubt contact me.

--

--