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

Polymorphism Assignment

Polymorphism in Java allows performing the same action in different ways. There are two types: compile-time polymorphism achieved through method overloading, and runtime polymorphism through inheritance and method overriding. For example, a Bank class and subclasses SBI, ICICI, and AXIS that override the getRateOfInterest() method, allowing each bank to return its own interest rate. This demonstrates how the same method performs differently based on the object type at runtime.

Uploaded by

N-raj Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
413 views

Polymorphism Assignment

Polymorphism in Java allows performing the same action in different ways. There are two types: compile-time polymorphism achieved through method overloading, and runtime polymorphism through inheritance and method overriding. For example, a Bank class and subclasses SBI, ICICI, and AXIS that override the getRateOfInterest() method, allowing each bank to return its own interest rate. This demonstrates how the same method performs differently based on the object type at runtime.

Uploaded by

N-raj Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) What is Polymorphism? Explain with real time examples.

The word polymorphism means having many forms.

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 and runtime polymorphism

Compile-time polymorphism – method overloading.

Runtime polymorphism.

Example-

For example, you have a smartphone for communication. The communication mode you choose could
be anything. It can be a call,

A text message, a picture message, mail, etc. So, the goal is common that is communication, but their
approach is different.

This is called Polymorphism.

2) Why do we need Polymorphism?

- To perform a single action in different ways.

- In Inheritance, using thre concept of polymorphism , Parent and child class method can have its own
implementation.

3) How can we achieve polymorphism in java with Practical demo Example?

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;}

Public class Demo {

Public static void main(String[] args) {

// TODO Auto-generated method stub

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

4)Can We Overload Static Method in Java?


Yes, We can overload static method in java

5) Difference between Compile time and Run time Polymorphism.

A)Compile time polymorphism means binding is occuring at compile time

Run time polymorphism where at run time we came to know which method is going to invoke

B)Compile time polymorphism can be achieved through static binding

Run time polymorphism can be achieved through dynamic binding

C)Inheritance is not involved in Compile time polymorphism

Inheritance is involved in Run time polymorphism

D)Method overloading is an example of compile time polymorphism

Method overriding is an example of runtime polymorphism

6) What is Upcasting

Upcasting is the typecasting of a child object to a parent object. Upcasting can be done implicitly.
Upcasting gives us the

Flexibility to access the parent class members but it is not possible to access all the child class members
using this feature.

Instead of all the members, we can access some specified members of the child class. For instance, we
can access the overridden

Methods.

Ex-

Parent p=new Child();

7) Write a program to calculate area of 3 different shape using Compile time Polymorphism.

Class Area{
Public void areaOfShape( double base,double height ) {

Double Area = 0.5 *base*height;

System.out.println(“the area of triangle is :- “+ Area);

Public void areaOfShape( double r ) {

Double Area = Math.PI * r*r;

System.out.println(“the area of triangle is :- “+ Area);

Public void areaOfShape( double a,double b,double height ) {

Double Area = (a+b)/2 * height;

System.out.println(“the area of triangle is :- “+ Area);

Public class CompileTimePoly {

Public static void main(String[] args) {

// TODO Auto-generated method stub

Area a =new Area();

a.areaOfShape(2,4);
a.areaOfShape(5);

a.areaOfShape(2,4,5);

You might also like