0% found this document useful (0 votes)
14 views20 pages

Polymorph Is M

This document discusses polymorphism and its two main types in Java: method overloading and method overriding. Method overloading involves defining multiple methods with the same name but different parameters, allowing for compile-time polymorphism. Method overriding defines a method in a subclass with the same name and parameters as a method in the parent class, enabling runtime polymorphism where the method invoked depends on the object type. The document provides examples of both overloading and overriding to demonstrate their usage and rules in Java.

Uploaded by

Rituraj Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

Polymorph Is M

This document discusses polymorphism and its two main types in Java: method overloading and method overriding. Method overloading involves defining multiple methods with the same name but different parameters, allowing for compile-time polymorphism. Method overriding defines a method in a subclass with the same name and parameters as a method in the parent class, enabling runtime polymorphism where the method invoked depends on the object type. The document provides examples of both overloading and overriding to demonstrate their usage and rules in Java.

Uploaded by

Rituraj Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

POLYMORPHISM

Introduction

• Polymorphism is the ability of an object to take on many forms.


• In other words, polymorphism allows us to define one interface
and have multiple implementations.
• Polymorphism just means that, basically, once we have got a child
class, we can use objects of that child class wherever we would
use objects of the parent class. Java will automatically invoke the
right methods.
Introduction
So polymorphism can be elaborated as:
• It is a feature that allows one interface to be used for a general class
of actions.
• An operation may exhibit different behaviour in different instances.

• The behaviour depends on the types of data used in the operation.


• It plays an important role in allowing objects having different internal
structures to share the same external interface.
• Polymorphism is extensively used in implementing inheritance.
Introduction
class Fruit {
public void show() {
System.out.println("Fruit");
}
}

class Banana extends Fruit {


//method overriden
public void show() {
System.out.println("Banana");
}
public void makeBananaTree() {
System.out.println("Making a tree");
}
}

public class Application {


public static void main(String[] args) {
Fruit banana = new Banana();
banana.show();
// The following WILL NOT work;
// Variables of type Fruit know only about Fruit methods.
banana.makeBananaTree();
}
} OUTPUT:
Banana
Introduction
class Shape { void draw() { } }
class Circle extends Shape {
private int x, y, r;
Circle(int x, int y, int r) { this.x = x; this.y = y; this.r = r; }
void draw() { System.out.println("Drawing circle (" + x + ", "+ y + ", " + r + ")"); }
}
 
class Rectangle extends Shape {
private int x, y, w, h;
Rectangle(int x, int y, int w, int h) { this.x = x; this.y = y; this.w = w; this.h = h; }
  void draw() { System.out.println("Drawing rectangle (" + x + ", "+ y + ", " + w + "," + h + ")"); }
}

class ShapesMain {
public static void main(String[] args) {
Shape[] shapes = { new Circle(10, 20, 30),
new Rectangle(20, 30, 40, 50) };
for (int i = 0; i < shapes.length; i++)
shapes[i].draw();
}
}
Types of Polymorphism in JAVA

Polymorphism

Method Method
Overloading Overriding
Method Overloading (Compile-time
Polymorphism or Static Polymorphism)

• In Java, it is possible to define two or more methods of same

name in a class, provided that there argument list or parameters

are different. This concept is known as Method Overloading.

Argument lists could differ in –

• Number of parameters.

• Data type of parameters.

• Sequence of Data type of parameters.


Method Overloading (Compile-time
Polymorphism or Static Polymorphism)
Example 1: Overloading – Different Number of parameters in argument list
When methods name are same but number of arguments are different.
class DisplayOverloading {
public void disp(char c) {
System.out.println(c);
}
public void disp(char c, int num) {
System.out.println(c + " "+num);
}
}
class Sample {
public static void main(String args[]) {
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
Output:
obj.disp('a',10);
a
} a 10
}
Method Overloading (Compile-time
Polymorphism or Static Polymorphism)
Example 2: Overloading – Difference in data type of arguments
In this example, method disp() is overloaded based on the data type, one with char
argument and another with int argument.

class DisplayOverloading2{
public void disp(char c) {
System.out.println(c);
}
public void disp(int c) {
System.out.println(c);
}
}
 
class Sample2{
public static void main(String args[]) {
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5); Output:
} a
} 5
Method Overloading (Compile-time
Polymorphism or Static Polymorphism)
Example3: Overloading – Sequence of data type of arguments
Here method disp() is overloaded based on sequence of data type of arguments

class DisplayOverloading3 {
public void disp(char c, int num) {
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c) {
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3{
public static void main(String args[]) {
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
Output:
}
I’m the first definition of method disp
} I’m the second definition of method disp
 
Lets see few Valid/invalid cases of method
overloading
Case 1:
int mymethod(int a, int b, float c)
int mymethod(int var1, int var2, float var3)
Result: Compile time error. Argument lists are exactly same. Both methods are having
same number, data types and same sequence of data types in arguments.
Case 2:
int mymethod(int a, int b)
int mymethod(float var1, float var2)
Result: Perfectly fine. Valid case for overloading. Here data types of arguments are
different.
Case 3:
int mymethod(int a, int b)
int mymethod(int num)
Result: Perfectly fine. Valid case for overloading. Here number of arguments are
different.
Lets see few Valid/invalid cases of method
overloading
Case 4:

float mymethod(int a, float b)

float mymethod(float var1, int var2)

Result: Perfectly fine. Valid case for overloading. Sequence of the data types are
different, first method is having (int, float) and second is having (float, int).
Case 5:

int mymethod(int a, int b)

float mymethod(int var1, int var2)

Result: Compile time error. Argument lists are exactly same. Even though return type of
methods are different, it is not a valid case. Since return type of method doesn’t matter
while overloading a method.
Rules for Method Overloading

1. Overloaded method should always be the part of the same class


(can also take place in sub class), with same name but different
parameters.
2. Constructor in Java can be overloaded
3. Overloaded methods must have a different argument list.

4. The parameters may differ in their type or number, or in both.


5. They may have the same or different return types.
Guess the answers before checking it at the
end of programs
Question 1 – return type, method name and argument list same.
class Demo {
public int myMethod(int num1, int num2){
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2) {
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample4 { Answer: It will throw a
public static void main(String args[]) { compilation error: More than
Demo obj1= new Demo();
obj1.myMethod(10,10); one method with same name
obj1.myMethod(20,12);
and argument list cannot be
}
} defined in a same class.
Guess the answers before checking it at the
end of programs
Question 2 – return type is different. Method name & argument list same.
class Demo2 {
public double myMethod(int num1, int num2) {
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2) {
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
} Answer: It will throw a
class Sample5 { compilation error: More than one
public static void main(String args[]) method with same name and
{ argument list cannot be given in a
Demo2 obj2= new Demo2(); class even though their return
obj2.myMethod(10,10); type is different. Method return
obj2.myMethod(20,12);
type doesn’t matter in case of
}
overloading.
}
Method Overriding (Runtime Polymorphism or
Dynamic Polymorphism )
• Declaring a method in subclass which is already present in

parent class is known as method overriding.

• The benefit of overriding is: ability to define a behaviour that's

specific to the subclass type which means a subclass can

implement a parent class method based on its requirement.

• In object-oriented terms, overriding means to override the

functionality of an existing method.


Method Overriding (Runtime Polymorphism or
Dynamic Polymorphism )
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
}
Output:
} Animals can move
Dogs can walk and run
Method Overriding (Runtime Polymorphism or
Dynamic Polymorphism )
Consider the following example :
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
public void bark(){
System.out.println("Dogs can bark");
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
b.bark();
}
}
Method Overriding (Runtime Polymorphism or
Dynamic Polymorphism )
Consider the following example :
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{ This would produce the following result:
public void move(){ TestDog.java:30: cannot find symbol
System.out.println("Dogs can walk symbol : method bark()
and run");
} location: class Animal
public void bark(){ b.bark();
System.out.println("Dogs can bark"); ^
} This program will throw a compile time error since b's
} reference type Animal doesn't have a bark method.
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
b.bark();
}
}
Rules for Method Overriding

1. Applies only to inherited methods


2. Object type (NOT reference variable type) determines which
overridden method will be used at runtime
3. Overriding method can have different return type

4. Static and final methods cannot be overridden


5. Constructors cannot be overridden

6. It is also known as Runtime polymorphism.

You might also like