Understanding Java Polymorphism Examples

0% found this document useful (0 votes)
223 views5 pages
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…

Uploaded by

N-raj Yadav
  • Polymorphism in Java
  • Static Methods and Additional Concepts
  • Compile Time Polymorphism Examples

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();

[Link](“SBI Rate of Interest: “+[Link]());

B=new ICICI();

[Link](“ICICI Rate of Interest: “+[Link]());

B=new AXIS();

[Link](“AXIS Rate of Interest: “+[Link]());

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;

[Link](“the area of triangle is :- “+ Area);

Public void areaOfShape( double r ) {

Double Area = [Link] * r*r;

[Link](“the area of triangle is :- “+ Area);

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

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

[Link](“the area of triangle is :- “+ Area);

Public class CompileTimePoly {

Public static void main(String[] args) {

// TODO Auto-generated method stub

Area a =new Area();

[Link](2,4);
[Link](5);

[Link](2,4,5);

Common questions

Powered by AI

Method overloading in Java allows multiple methods in the same class to have the same name with different parameter lists. It is a feature of compile-time polymorphism where the appropriate method is picked based on the method signature during compilation. For instance, in the class 'Area', the method 'areaOfShape' is overloaded to calculate areas of various shapes by differing parameter types, such as one version calculates the area of a triangle using base and height, another for a circle using radius, and another for a trapezoid using base, top, and height .

Yes, static methods can be overloaded in Java. This relates to compile-time polymorphism, where multiple static methods can have the same name but different parameter lists within the same class, with the method to be called determined at compile time. Unlike dynamic polymorphism, static method calls are resolved using static binding .

Compile-time polymorphism occurs when method signatures are resolved at compile time using method overloading, where different methods have the same name but differ in parameter types or numbers. Runtime polymorphism, achieved through method overriding, resolves method calls at runtime using dynamic binding, allowing different implementations based on the actual object type. Compile-time polymorphism doesn't involve inheritance, whereas runtime polymorphism does .

Polymorphism provides flexibility in Java library development by allowing libraries to define generic interfaces or abstract classes that can be implemented or extended by user-defined classes. This enables users to design objects that can interact seamlessly with library components, promoting extensibility. User-defined classes can have customized functionality while adhering to the library's interface, facilitating stronger decoupling and easier updates or integration within the larger software ecosystem .

Polymorphism contributes to abstraction by allowing objects to be manipulated through a common interface rather than their specific implementations, focusing on what operations can be performed rather than how they are implemented. It enhances encapsulation by enabling objects to hide complex details behind interchangeable operations, promoting modularity and flexibility in a program design. Together, they lay the groundwork for reusable code and easier system maintenance .

Upcasting in Java is used to treat an object of a child class as an object of its parent class, which allows for flexibility in accessing the parent class members and methods. This is beneficial for runtime polymorphism as it enables invoking overridden methods. However, upcasting limits access to the child class-specific members that are not overridden from the parent, which may restrict using all child class features .

Consider a scenario involving an application that calculates interest rates for different types of bank accounts. The parent class 'Bank' contains a method 'getRateOfInterest()'. Subclasses 'SBI', 'ICICI', and 'AXIS' each override this method to return specific interest rates. An instance of 'Bank' referencing to 'SBI', dynamically invokes the 'SBI's implementation of 'getRateOfInterest()' at runtime. This exemplifies runtime polymorphism, demonstrating flexibility and dynamic method resolution, essential for large systems requiring extensibility and maintenance ease .

In the practical example, three overloaded methods named 'areaOfShape' are defined to calculate areas of different shapes based on varying parameters—such as for a triangle, circle, and trapezoid. The correct method is selected at compile-time based on the arguments provided, demonstrating compile-time polymorphism where multiple methods share the same name but differ in parameter number or type. This helps in achieving method overloading in Java .

Polymorphism in Java is implemented using inheritance, where a parent class reference is used to refer to a child class object. For example, the class 'Bank' has a method 'getRateOfInterest()', and subclasses 'SBI', 'ICICI', and 'AXIS' each provide their own implementation of this method. By using inheritance, you can dynamically decide which 'getRateOfInterest()' method to call at runtime this feature is known as runtime polymorphism, or method overriding .

Polymorphism allows a smartphone to perform the single action of communication in multiple ways, such as through calls, text messages, picture messages, or emails. This enhances functionality by providing users with various options for communication while maintaining the same underlying objective, similar to how an object can be treated in different forms in Java through polymorphism .

1)
What is Polymorphism? Explain with real time examples.
The word polymorphism means having many forms.
Polymorphism in Java
Class ICICI extends Bank{  
Float getRateOfInterest(){return 7.3f;}  
}  
Class AXIS extends Bank{  
Float getRateOfInterest(
Yes, We can overload static method in java
5) Difference between Compile time and Run time Polymorphism.
A)Compile time polym
Public void areaOfShape( double base,double height ) {
Double Area = 0.5 *base*height;
System.out.println(“the area of triang
a.areaOfShape(5);
a.areaOfShape(2,4,5);
}
}

You might also like