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

Week 15- OOP Chapter 11- Polymorphism-1

Polymorphism in OOP allows objects of different classes to respond to the same method in unique ways, enabling a single interface to represent various objects. In Java, there are two types of polymorphism: static (compile-time) achieved through method overloading, and dynamic (runtime) achieved through method overriding. Binding in Java can be static or dynamic, with static binding occurring at compile time and dynamic binding at runtime, particularly through method overriding.

Uploaded by

Mainard Lacsom
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 15- OOP Chapter 11- Polymorphism-1

Polymorphism in OOP allows objects of different classes to respond to the same method in unique ways, enabling a single interface to represent various objects. In Java, there are two types of polymorphism: static (compile-time) achieved through method overloading, and dynamic (runtime) achieved through method overriding. Binding in Java can be static or dynamic, with static binding occurring at compile time and dynamic binding at runtime, particularly through method overriding.

Uploaded by

Mainard Lacsom
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Polymorphis

m
OOP - Chapter 11
Polymorphism
•Polymorphism allows objects of different classes to be
used, while still maintaining their own unique behavior.
This means that objects of different classes can respond
to the same message or method in different ways.

•Polymorphism allows to use a single interface to


represent different objects that share a common
behavior.

•Poly means “many”


•Morph means “Take different forms”
Two types of polymorphism in
Java

Static polymorphism, also known as compile- Dynamic polymorphism, also known as


time polymorphism, refers to the ability of runtime polymorphism, refers to the ability of
the compiler to determine which method to objects to respond to the same message with
call at compile time based on the arguments the appropriate method based on their class
passed to the method. definition at runtime.
Static polymorphism is achieved through Dynamic polymorphism is achieved through
method overloading, where multiple method overriding, where a subclass provides
methods in the same class have the same a different implementation of a method that
name but different parameters. is already defined in its superclass.
Polymorphism – Method
Overriding
class Shape { public class Main {
public void draw() { public static void main(String[]
System.out.println("Drawing a args) {
shape"); Shape shape1 = new Shape();
} Shape shape2 = new Circle();
} Shape shape3 = new Square();

class Circle extends Shape { shape1.draw();


@Override shape2.draw();
public void draw() { shape3.draw();
System.out.println("Drawing a }
circle"); }
}
}

class Square extends Shape {


@Override
public void draw() {
System.out.println("Drawing a
square");
}
Binding in java
•Binding refers to the process of associating a
method or function call with its implementation
at runtime.
•The connecting (linking) between a
method call and method
body/definition.

•There are two types of binding in Java:


1. Static binding
2. Dynamic binding
Binding in
java
Static binding
• Occurs when the method to be called is determined at compile
time.

• This happens when the method being called is a static method,


private method, final method or a constructor.

• The compiler can determine which method to call based on the


method signature and the static type of the object on which
the method is being called.

• This binding is also known as early binding because it takes


place before the program actually runs.
Static binding
public class Addition {

void add(int x, int y) {


int sum = x + y;
System.out.println("Sum of two numbers: " +sum);
}

void add(int x, int y, int z) {


int sum = x + y + z;
System.out.println("Sum of three numbers: " +sum);
}

public static void main(String[] args) {


Addition a = new Addition();
a.add(10, 20); // Calling add() method with passing two argument
values.
a.add(20, 30, 40); // Calling add() method with passing three argument
values.
}
Dynamic binding
• Also known as late binding or runtime binding, occurs
when the method to be called is determined at runtime.

• This happens when the method being called is a non-static


method and the actual type of the object on which the
method is being called is not known until runtime.

• Dynamic binding is also called late binding or runtime


binding because binding occurs during runtime.

• An example of dynamic binding is method overriding.


Dynamic binding
public class Animal { public class DynamicBindingTest
{
void eat() { public static void main(String[]
System.out.println("Animals eat both args)
plants and flesh"); {
} Animal a = new Animal();
a.eat(); // It will call eat()
} method of Animal class because the
reference variable is pointing to
public class Lion extends Animal { object of animal class.

void eat() {
System.out.println("Lions eat flesh Animal a1 = new Lion();
because they are carnivore"); a1.eat(); // It will call eat()
} method of Lion class because the
} reference variable is pointing
towards the object of Loin class.
}
}

You might also like