It's UWAweek 17 (1st semester, week 8)

help2005

This forum is provided to promote discussion amongst students enrolled in CITS2005 Object Oriented Programming.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying selected article
Showing 1 of 89 articles.
Currently 1 other person reading this forum.


 UWA week 16 (1st semester, week 7) ↓
SVG not supported

Login to reply

👍?
helpful
3:45pm Tue 16th Apr, Andrew G.

ANONYMOUS wrote:

class BankAccount6 {
  String ownerName;
  int balance;
  
  BankAccount6(String ownerName, int balance) {
    this.ownerName = ownerName;
    this.balance = balance;
  }
  
  BankAccount6(String ownerName) {
    this.ownerName = ownerName;
    this.balance = 0;
  }
}

Hey! I'm wondering if the above example can be described as a form of polymorphism?

This is an example of method (in this case specifically constructor) overloading.

There is no universally accepted definition of polymorphism, but overloading is generally considered an example of "ad hoc polymorphism", where the behaviour of a method depends on the types of its arguments.

The general concept of polymorphism simply requires that the same "thing" (method, typically) takes on multiple different "forms" (in the case of methods, different implementations) in different contexts. Overloading is an example of this because for the same method name we get a different implementation based on the types of the arguments.

Java implements a number of different kinds of polymorphism, but so far the only one we have seen in class is "subtype polymorphism". Due to Java's inheritance rules, we can guarantee that, if we have a class Child extends Parent, then Child must have all the same public members that Parent does. So any valid code that is written to work with a Parent object is also valid if we substitute a Child in its place. Due to dynamic dispatch, the same code may call different methods based on the real type of the object it has been given. Therefore the "form" of the methods called has changed, and so the code is polymorphic.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy