0% found this document useful (0 votes)
66 views12 pages

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses polymorphism and method overloading and overriding in object-oriented programming. It defines method signatures as the method name and parameter types/order but not return type. Polymorphism allows one interface to have multiple forms via method overloading, where methods have the same name but different signatures, and method overriding, where sub-classes can override methods from the super-class but signatures and return types must be the same. Examples are provided to demonstrate method overloading and overriding.

Uploaded by

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

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses polymorphism and method overloading and overriding in object-oriented programming. It defines method signatures as the method name and parameter types/order but not return type. Polymorphism allows one interface to have multiple forms via method overloading, where methods have the same name but different signatures, and method overriding, where sub-classes can override methods from the super-class but signatures and return types must be the same. Examples are provided to demonstrate method overloading and overriding.

Uploaded by

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

Object-Oriented Programming (CS F213)

Module I: Object-Oriented and Java Basics


CS F213 RL1.4: Polymorphism

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 1.4 : Topics

Method Signatures
Polymorphism
Polymorphism Types

2 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


Method Signatures
A Method Signature is the method name and the number, type
and order of its parameters .
Return type of a method is not considered to be a part of the
method signature.

Methods Method Signatures


int doSomeThing(int a , float b)
{

doSomeThing(int,float)
}

float doSome(int a , int b, int c)


{ doSome(int,int,int)

}

3 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


Polymorphism
https://en.wikipedia.org/wiki/Polymorphism_(computer_science)

One Interface Having Multiple Forms


Three Flavors
1. Adhoc Polymorphism (Via Method Overloading)
2. Subtyping (Via Method Overriding)
3. Parametric Polymorphism (Via Generic Programming Will
be Covered Later)

4 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


Adhoc Polymorphism : Method
Overloading
Supported via Method Overloading
Two Methods are said to be overloaded if they have same name but
different signatures
Method signatures can be different either via having different number
of arguments to methods or via having different order of the
arguments
Overloaded method either may have same or different return types

int add(int a , int b) { .} add(int , int )

float add(float a, float b) { .} add(float , float )

double add(double a, double b) { .} add(double , double )

String add(String a, String b) { .} add(String , String )

5 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


Adhoc Polymorphism :
Method Overloading

Method Overloading Example

class MethodOverloadingExample
{
int doS(int a, float b) {} doS(int , float)

float doS(float a, int b) {} doS(float, int)

int doS(int a, float b, int c) {} doS(int , float, int)

float doS(int a, float b, double c){ } doS(int , float, double)


}
Wrong Method Declaration in
float doS(double a, float b) {} In Same Class Compile
Time Error
double doS(double x, float y) {}
(Not Overloaded Methods)
6 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Polymorphism:
Subtyping (Via Method Overriding)

This Type of Polymorphism is known as Runtime


Polymorphism (Dynamic Method Dispatch or Method
Overriding)
Two Methods are said to be overridden if and only if they have
name, same signatures and same return type
Exhibited only by sub-classes of a common super class
A sub class can override a method of a super class
class A
{
public void doS(int a, int b) { . }
}// End of class A
class B overrides the
class B extends A doS() method of super
{ class A.
public void doS(int a, int b) { . }
}// End of class B
7 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Polymorphism:
Subtyping (Via Method Overriding)

Method Overriding Example-1


class A
{
A a1 = new A();
public void print() a1.print();
{
System.out.println(Hello Class A);
}
// Whats the o/p?
}// End of class A
a1 = new B();
class B extends A
{
a1.print();
public void print()
{ // Whats the o/p?
System.out.println(Hello Class B);
}
}// End of class B
class B overrides the print() method of super class A.
8 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Polymorphism:
Subtyping (Via Method Overriding)

Method Overriding Example-2


class A
{ A
public void print()
{
System.out.println(Hello Class A);
}
}// End of class A
class B extends A
{
public void print() B C D
{
System.out.println(Hello Class B);
}
}// End of class B
class C extends A
{
Sub-classes B, C and
public void print()
{
D overrides the print()
System.out.println(Hello Class C);
} from the super class A
}// End of class C
class D extends A
{
A a1 = new A();
public void print()
{
a1.print();
System.out.println(Hello Class D);
}
}// End of class D a1 = new D();
a1.print();
9 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Polymorphism:
Subtyping (Via Method Overriding)

Overridden Methods Cannot Have Different Return


Types
class A
{
public int print() Wrong ..
{ Compile Time
System.out.println(Hello Class A); Error
return 0;
}
Overridden
}// End of class A
class B extends A
Methods Can
{ Not have
public void print() Different
{ Return Types
System.out.println(Hello Class B);
}
}// End of class B

10 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas


Polymorphism:
Subtyping (Via Method Overriding)

What is the problem with the following code?

class A
{
public int print(int x)
{
System.out.println(Hello Class A);
return 0;
} Method Overloading
}// End of class A
class B extends A
{
public void print()
{
System.out.println(Hello Class B);
} No Error Not Method
}// End of class B Overriding
11 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas
Thank You

12 Object-Oriented Programming (CS F213) Dr. Pankaj Vyas

You might also like