Polymorphism

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

SIT Internal

Polymorphism
SIT Internal

Learning outcomes
➢ What is polymorphism?

➢ Motivation and examples

➢ Types of polymorphism
❑ Method Overloading
❑ Method Overriding
SIT Internal

Poly morphism
many form
SIT Internal

What is Polymorphism

Ability of objects to take on different forms or exhibit


different behaviors based on the context

What does it mean?


SIT Internal

What is Polymorphism?

A way of allowing similar operations to be grouped


together under the same name
SIT Internal

Let’s take another example

Dog Woof
Speak

Cat ?
SIT Internal

Let’s take another example

Dog Speak Woof

Cat Speak Meow

Cow Speak Moo


SIT Internal

Polymorphism

Method Overloading Method Overriding


the same name but different allows a subclass to provide its own
parameters to be defined in a class implementation of a method defined in
its superclass
SIT Internal

Method Overloading
Allows class to have more than one method having the same name, if their
argument lists are different.

public int calculateSum(int a, int b) {


// Method implementation
}

public double calculateAverage(int[] numbers) {


// Method implementation
}
SIT Internal

Method Overloading
class Calculator {
public int add(int a, int b) {
return a + b;
Calculator calculator = new Calculator();
}
int sum1 = calculator.add(5, 10);
public int add(int a, int b, int c) int sum2 = calculator.add(1, 2, 3);
{
return a + b + c;
}
}
SIT Internal

Method Overloading: Constructors

class Vehicle {
private String make;
private String model;
private int year; class Main {
public static void main(String[] args) {
public Vehicle(String m, String mod, int year) { Vehicle vehicle1 = new Vehicle("Toyota", "Camry“,
make = m;
model = mod; 2020);
year = y; Vehicle vehicle2 = new Vehicle("Honda", "Civic");
}
}
public Vehicle(String m, String mod) {
}
make = m;
model = mod;
}
}
SIT Internal

Method Overriding
subclass overrides the method inherited from the superclass and
provides its specific implementation.

• Argument list: The argument list of overriding method must be same


as that of the method in parent class.
• The data types of the arguments and their sequence should be
maintained as it is in the overriding method
SIT Internal

Code Example
class Animal {
public void speak() {
System.out.println("Animal makes a sound"); class Main {
} public static void main(String[] args) {
} Animal animalDog = new Dog();
class Dog extends Animal { Animal animalCat = new Cat();
public void speak() { animalDog.speak();
System.out.println("Dog barks");
animalCat.speak();
}
}
}
class Cat extends Animal { }
public void speak() {
System.out.println("Cat meows");
}
}
SIT Internal

Method Overriding
subclass overrides the method inherited from the superclass and
provides its specific implementation.

Access Modifier: The Access Modifier of the overriding


method (method of subclass) cannot be more restrictive than
the overridden method of parent class.
SIT Internal

Code Example

class Animal {
protected void speak() {
System.out.println("Animal makes a sound"); class Main {
} public static void main(String[]
} args) {
Animal animal = new Dog();
class Dog extends Animal { animal.speak();
public void speak() { }
System.out.println("Dog barks"); }
}
}
SIT Internal

Code Example

class Animal {
protected void speak() {
System.out.println("Animal makes a sound"); class Main {
} public static void main(String[]
} args) {
Animal animal = new Dog();
class Dog extends Animal { animal.speak();
private void speak() { }
System.out.println("Dog barks"); }
}
}
SIT Internal

Summary
• Polymorphism: actual type of an object can be different from the type it
is declared as.

• Method overloading deals with multiple methods with the same name in
the same class, but with different signatures

• Method overriding deals with two methods, one in a parent class and
one in a child class, that have the same signature

You might also like