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

Polymorphism in Java

Polymorphism in Java allows objects to take on multiple forms, enabling methods to behave differently based on their class type. It is categorized into compile-time polymorphism, achieved through method overloading, and runtime polymorphism, achieved through method overriding. Examples illustrate how method overloading can vary by the number of parameters, data types, or parameter order, while runtime polymorphism demonstrates how overridden methods are resolved at runtime.

Uploaded by

Vivek Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Polymorphism in Java

Polymorphism in Java allows objects to take on multiple forms, enabling methods to behave differently based on their class type. It is categorized into compile-time polymorphism, achieved through method overloading, and runtime polymorphism, achieved through method overriding. Examples illustrate how method overloading can vary by the number of parameters, data types, or parameter order, while runtime polymorphism demonstrates how overridden methods are resolved at runtime.

Uploaded by

Vivek Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

POLYMORPHISM

The word ‘polymorphism’ means ‘having many forms’. In Java, polymorphism


refers to the ability of a message to be displayed in more than one form. This
concept is a key feature of Object-Oriented Programming and it allows objects
to behave differently based on their specific class type.
Real-life Illustration of Polymorphism in Java: A person can have different
characteristics at the same time. Like a man at the same time is a father, a
husband, and an employee. So the same person possesses different behaviors in
different situations. This is called polymorphism.

Types of Java Polymorphism


In Java Polymorphism is mainly divided into two types:
 Compile-Time Polymorphism
 Runtime Polymorphism

Method Overloading in Java




In Java, Method Overloading allows different methods to have the same


name, but different signatures where the signature can differ by the
number of input parameters or type of input parameters, or a mixture of
both.

1
Method overloading in Java is also known as Compile-time
Polymorphism, Static Polymorphism, or Early binding. In Method
overloading compared to the parent argument, the child argument will
get the highest priority.
Example of Method Overloading
// Java program to demonstrate working of method
// overloading in Java

public class Sum {


// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }

// Overloaded sum(). This sum takes three int parameters


public int sum(int x, int y, int z)
{
return (x + y + z);
}

// Overloaded sum(). This sum takes two double


// parameters
public double sum(double x, double y)
{
return (x + y);
}

// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

Output
30
60
31.0

Different Ways of Method Overloading in Java


 Changing the Number of Parameters.
 Changing Data Types of the Arguments.
 Changing the Order of the Parameters of Methods

2
1. Changing the Number of Parameters
Method overloading can be achieved by changing the number of
parameters while passing to different methods.
Below is the implementation of the above method:
// Java Program to Illustrate Method Overloading
// By Changing the Number of Parameters

// Importing required classes


import java.io.*;

// Class 1
// Helper class
class Product {
// Method 1
// Multiplying two integer values
public int multiply(int a, int b)
{
int prod = a * b;
return prod;
}

// Method 2
// Multiplying three integer values
public int multiply(int a, int b, int c)
{
int prod = a * b * c;
return prod;
}
}

// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of above class inside main()
// method
Product ob = new Product();

// Calling method to Multiply 2 numbers


int prod1 = ob.multiply(1, 2);

// Printing Product of 2 numbers


System.out.println(
"Product of the two integer value :" + prod1);

// Calling method to multiply 3 numbers


int prod2 = ob.multiply(1, 2, 3);

// Printing product of 3 numbers

3
System.out.println(
"Product of the three integer value :" + prod2);
}
}

Output
Product of the two integer value :2
Product of the three integer value :6

2. Changing Data Types of the Arguments


In many cases, methods can be considered Overloaded if they have the
same name but have different parameter types, methods are considered
to be overloaded.
Below is the implementation of the above method:
// Java Program to Illustrate Method Overloading
// By Changing Data Types of the Parameters

// Importing required classes


import java.io.*;

// Class 1
// Helper class
class Product {
// Multiplying three integer values
public int Prod(int a, int b, int c)
{
int prod1 = a * b * c;
return prod1;
}

// Multiplying three double values.


public double Prod(double a, double b, double c)
{
double prod2 = a * b * c;
return prod2;
}
}

class GFG {
public static void main(String[] args)
{
Product obj = new Product();

int prod1 = obj.Prod(1, 2, 3);


System.out.println(
"Product of the three integer value :" + prod1);

double prod2 = obj.Prod(1.0, 2.0, 3.0);


System.out.println(

4
"Product of the three double value :" + prod2);
}
}

Output
Product of the three integer value :6
Product of the three double value :6.0

3. Changing the Order of the Parameters of Methods


Method overloading can also be implemented by rearranging the
parameters of two or more overloaded methods. For example, if the
parameters of method 1 are (String name, int roll_no) and the other
method is (int roll_no, String name) but both have the same name, then
these 2 methods are considered to be overloaded with different
sequences of parameters.
Below is the implementation of the above method:
// Java Program to Illustrate Method Overloading
// By changing the Order of the Parameters

// Importing required classes


import java.io.*;

// Class 1
// Helper class
class Student {
// Method 1
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " "
+ "Roll-No :" + roll_no);
}

// Method 2
public void StudentId(int roll_no, String name)
{
// Again printing name and id of person
System.out.println("Roll-No :" + roll_no + " "
+ "Name :" + name);
}
}

// Class 2
// Main class
class GFG {
// Main function
public static void main(String[] args)
{
// Creating object of above class
Student obj = new Student();

5
// Passing name and id
// Note: Reversing order
obj.StudentId("Spyd3r", 1);
obj.StudentId(2, "Kamlesh");
}
}

2. Runtime Polymorphism
Runtime Polymorphism in Java known as Dynamic Method Dispatch. It is a
process in which a function call to the overridden method is resolved at Runtime.
This type of polymorphism is achieved by Method Overriding. Method overriding,
on the other hand, occurs when a derived class has a definition for one of the
member functions of the base class. That base function is said to be overridden.
Method Overriding

Example: Below is the implementation of the Java Method Overriding


// Java program to demonstrate
// method overriding in java
class Parent {
// base class or superclass which is going to overriden
// below
void show() { System.out.println("Parent's show()"); }
}
// Inherited class
class Child extends Parent {
// This method overrides show() of Parent
@Override void show()
{
System.out.println("Child's show()");
}
}
// Driver class
class Geeks {
public static void main(String[] args)
{
// If a Parent type reference refers
// to a Parent object, then Parent's
// show is called
Parent obj1 = new Parent();
obj1.show();

// If a Parent type reference refers


// to a Child object Child's show()
// is called. This is called RUN TIME
// POLYMORPHISM.
Parent obj2 = new Child();
obj2.show ();
}

6
}

Example
// Java Program for Method Overriding

// Class 1
// Helper class
class Parent {

// Method of parent class


void Print() {
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() {
System.out.println("subclass2");
}
}

// Class 4
// Main class
class Geeks {

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

7
a = new subclass2();
a.Print();
}
}

Output
subclass1
subclass2
Example
// Example of Overriding in Java
class Animal {
// Base class
void move() { System.out.println(
"Animal is moving."); }
void eat() { System.out.println(
"Animal is eating."); }
}

class Dog extends Animal {


@Override void move()
{ // move method from Base class is overriden in this
// method
System.out.println("Dog is running.");
}
void bark() { System.out.println("Dog is barking."); }
}

public class Geeks {


public static void main(String[] args)
{
Dog d = new Dog();
d.move(); // Output: Dog is running.
d.eat(); // Output: Animal is eating.
d.bark(); // Output: Dog is barking.
}
}

Output
Dog is running.
Animal is eating.
Dog is barking.
Explnation: The Animal class defines base functionalities like move() and eat().t he Dog
class inherits from Animal and overrides the move() method to provide a specific

8
behavior Dog is running. Both classes can access their own methods. When creating a Dog
object, calling move() executes the overridden method.

You might also like