Object Oriented
Programming
Lecture 7
BSCS 2(Afternoon)
Spring 2024
[email protected]
Topics to be covered according to
outline
• Polymorphism
• Static Polymorphism
• Dynamic
Polymorphism
Polymorphism
• It is one of the core concepts of OOP
• Polymorphism describes situation in which something occur in
several different forms
• Java supports two types of Polymorphism
• Static or Compile time
• Dynamic or runtime
Polymorphism
• means "many forms". It comes from Greek word "poly"
(means many) and "morphos" (means form).
• For examples, in chemistry, carbon exhibits polymorphism
because it can be found in more than one form: graphite and
diamond. Each of the form has it own distinct properties.
Static Polymorphism
• It occurs when there is function overloading
• In overloading, functions with same name have different parameter list
• Parameter list may be different in terms of
• Data types
• myFunc1(int x, int y)
• myFunc1(double x, double y)
• myFunc1(int x, double y)
• Number of parameters.
• myFunc1(int x)
• myFunc1(int x, int y)
• This difference in parameter list allow compiler to decide which method
has to be called or to which method particular call should be bind.
• In other words, when an overloaded method is invoked, Java uses the
type and/or number of arguments as its guide to determine which version
of the overloaded method to actually call.
• This approach is called as static binding, early binding or static
polymorphism
Dynamic Polymorphism
• It occurs when there is inheritance and function overriding
• It occurs when a reference of parent class is used to refer to a
child class object.
• It does not allow compiler to determine which function will be
executed (function of parent class or function of a child class)
• Which function will be called is determined by JVM at runtime and
it depends on calling object.
• It is also known as late binding
A reference variable of Super class can
hold the reference/address of child class’s
object.
e.g.
r=b;
Here r is a reference of super class A and b
is reference of an object of child class B.
The output:
// Define superclass Shape
public class Shape {
private String color;
// Constructor
public Shape (String color) {
this.color = color;
}
public String toString() {
return "Shape of color=" + color ;
}
public double getArea() {
System.err.println("Shape unknown! Cannot compute area!");
return 0; // Need a return to compile the program
}
}
// Define Rectangle, subclass of Shape
public class Rectangle extends Shape {
private int length;
private int width;
// Constructor
public Rectangle(String color, int length, int width) {
super(color);
this.length = length;
this.width = width;
}
public String toString() {
return "Rectangleof length=" + length + " and width=" + width + ", subclass of "
+ super.toString();
}
public double getArea() {
return length*width;
}
}
// Define Triangle, subclass of Shape
public class Triangle extends Shape {
private int base;
private int height;
// Constructor
public Triangle(String color, int base, int height) {
super(color);
this.base = base;
this.height = height;
}
public String toString() {
return "Triangle of base=" + base + " and height=" + height + ",
subclass of " + super.toString();
}
public double getArea() {
return 0.5*base*height;
}
}
// A test driver program for Shape and its
subclasses
public class TestShape {
public static void main(String[] args) {
Shape s1 = new Rectangle("red", 4, 5);
System.out.println(s1);
System.out.println("Area is " + s1.getArea());
Shape s2 = new Triangle("blue", 4, 5);
System.out.println(s2);
System.out.println("Area is " + s2.getArea());
}
}
Polymorphism
• If a subclass instance is assign to a superclass reference,
• you can invoke the methods defined in the superclass only.
• You cannot invoke methods defined in the subclass.
• If the subclass overrides methods in the superclass,
• the subclass's version will be executed, instead of the superclass's
version.
Questions Are Welcome