Advanced Programming
CO2039
Chapter 3: Object Oriented Programming
Revision - Polymorphism
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA
TP.HCM, 03/01/2025
CONTENT
01 INTRODUCTION TO POLYMORPHISM
02 KEY CONCEPTS OF POLYMORPHISM
03 CONCLUSION
2
INTRODUCTION TO
01 POLYMORPHISM
3
What is Polymorphism?
In Java, polymorphism refers to the fact that you can have multiple
methods with the same name in the same class.
Two kinds of polymorphism:
● Overloading: two or more methods with different signatures.
● Overriding: replacing an inherited method with another having the
same signature.
4
Polymorphism overview
Two methods have to differ in their names or in the number or types of
their parameters.
Example:
● foo(int i) and foo(int i, int j) are different
● foo(int i) and foo(int k) are the same
● foo(int i, double d) and foo(double d, int i) are
different
5
Overloading vs. Overriding
6
int main()
#include <iostream> {
A a; B b;
class A a.n = 1; b.n = 2;
{ a.f2(); b.f2();
public: std::cout<<a.n <<" " << b.n;
int n;
int f() {n++; return n;} return 0;
int f2() {f(); return n;} }
};
class B: public A
{
public:
int f(){n--; return n;}
};
class A public class Main
{ {
public int n; public static void main(String[] args) {
public int f() {n++; return n;} A a = new A(); B b = new B();
public int f2() {f(); return n;} a.n = 1; b.n = 2; a.f2(); b.f2();
System.out.println(a.n);
}; System.out.println(b.n);
}
class B extends A }
{
public int f(){n--; return n;}
};
#include <iostream> int main()
{
class A A a; B b;
{ a=b;
public: a.n=1;
int n; a.f2();
virtual int f() {n++; return n;} std::cout<<a.n;
int f2() {f(); return n;}
return 0;
}; }
class B: public A
{
public:
virtual int f(){n--; return n;}
};
class A public class Main
{ {
public int n; public static void main(String[] args) {
public int f() {n++; return n;} A a = new A(); B b = new B(); a = b;
public int f2() {f(); return n;} a.n = 1; a.f2();
System.out.println(a.n);
}; //System.out.println(b.n);
}
class B extends A }
{
public int f(){n--; return n;}
};
1. Why the result is 0 in java and why the result is 2 in
c++
2. How can c++ behaves like java
10
KEY CONCEPTS OF
02 POLYMORPHISM
11
Binding
Dynamic Binding vs. Static Binding:
● Static Binding: Determined at compile-time.
● Dynamic Binding: Determined at runtime.
Polymorphism requires dynamic binding.
12
Binding in OOP languages
In C++:
● Static binding (default): the function to be called is determined
during compilation.
● Dynamic binding (using virtual): enables polymorphism by
resolving function calls at runtime.
In Java:
● Java uses dynamic binding by default for method overriding.
● The method invoked is determined at runtime based on the actual
object, not the reference type.
13
Hands-on exercise
class A {
void f() { cout << "A::f"; }
void f2() { f(); }
1. What is the output?
};
2. Is polymorphism used in this code?
class B: public A {
void f() { cout << "B::f"; } If it isn't, how can we implement it?
};
3. Can you rewrite the code in Python
int main() { and Java?
A* a = new B();
a->f2(); // Output: ???
return 0;
}
14
Key differences: C++ vs. Java
Features C++ Java
Default binding Static binding Dynamic binding
Enabling dynamic Use virtual keyword Always enabled for
binding overrides
Polymorphism Requires explicit Default behaviour
virtual
Behind the scenes Requires explicit Abstracted as object
pointers references
15
Why use pointers for dynamic binding in C++?
Reason:
● In C++, the vtable (virtual table) is used to enable dynamic method
resolution.
● When using pointers or references, the program accesses the vtable of the
actual object to determine the appropriate method.
Static Objects (No Pointer):
● Vtable lookup is skipped, leading to static binding.
Dynamic Objects (Pointer/Reference):
● Vtable is consulted, allowing polymorphism to work.
16
03 CONCLUSION
17
Conclusion
● Polymorphism:
○ A key OOP feature that enables objects to behave differently
based on their actual type.
● Dynamic Binding:
○ Required for polymorphism.
○ Default in pure OOP languages like Java.
● C++ vs. Java:
○ C++: Static binding by default; use virtual for dynamic
binding.
○ Java: Always dynamic binding.
18
Thank you for your
attention!
https://www.cse.hcmut.edu.vn
ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA