14. Polymorphism - CS2030S Programming Methodology II
Method overriding enables polymorphism, the fourth and the last pillar of OOP, and arguably the most powerful one. It allows us to change how existing code behaves, without changing a single line of the existing code (or even having access to the code). Consider the function say below: Note that this method receives an Object instance. Since both Point <: Object and Circle <: Object, we can do the following: When executed, say will first print Hi, I am (0.0, 0.0), followed by Hi, I am { center: (0.0, 0.0), radius: 4.0 }. We are invoking the overriding Point::toString in the first call, and Circle::toString in the second call. The same method invocation obj.toString() causes two different methods to be called in two separate invocations! In biology, polymorphism means that an organism can have many different forms. Here, the variable obj can have many forms as well. Which method is invoked is decided during run-time, depending on the run-time type of the obj. This is called dynamic bind
Unit 14: Polymorphism After reading this unit, students should understand dynamic binding and polymorphism be aware of the equals method and the need to override it to customize the equality test understand when narrowing type conversion and type casting are allowed Taking on Many Forms Method overriding enables polymorphism , the fourth and the last pillar of OOP, and arguably the most powerful one. It allows us to change how existing code behaves, without changing a single line of the existing code (or even having access to the code). Consider the function say below: 1 2 3 void say ( Object
Explore this link on the map →saved by
related reading
- 20. Casting - CS2030S Programming Methodology IInus-cs2030s.github.io
- 19. Wrapper Class - CS2030S Programming Methodology IInus-cs2030s.github.io
- 21. Variance - CS2030S Programming Methodology IInus-cs2030s.github.io
- 17. Abstract Class - CS2030S Programming Methodology IInus-cs2030s.github.io
- Lab 1: Simulation I - CS2030S Programming Methodology IInus-cs2030s.github.io
- 4. Encapsulation - CS2030S Programming Methodology IInus-cs2030s.github.io
- 2. Variable and Type - CS2030S Programming Methodology IInus-cs2030s.github.io
- Lab 2: Simulation 2 - CS2030S Programming Methodology IInus-cs2030s.github.io
- Dynamic dispatch - Wikipediaen.wikipedia.org
- LEAP 71 | PicoGK.org Blogpicogk.org
- Everything is Fertilenickcammarata.com
- 9. Classes — Python 3.14.6 documentationdocs.python.org