Pre-assessment notes
Polymorphism
The answers to this one really showed up that I need to find a way to get
more on OO principles into the Inf1OP class... which was on my todo list
anyway.
Here's someone with the right top-level view:
You've actually encountered two different kinds of polymorphism in the
first year, and both of them occur in Java. The one you see where Java uses
generics, e.g. ArrayList<String>, ArrayList<Integer>, is called
parametric polymorphism, and Haskell also uses this extensively. (In fact,
Java didn't have this kind in the beginning, and our very own Phil Wadler
was instrumental in getting it added.) However, the main one is what's
usually called object oriented polymorphism, or sometimes ad hoc
polymorphism. It's when the same client code can work with objects that
have many forms, because they share a common interface which is all the
client code needs to know. For example, if a client is written in terms of
Animal and only needs to access methods that are understood by any Animal,
then the same client code can work with any object which is an instance of
a subclass of Animal:
- Having a common interface with many different forms (eg Animal -> Cat, Dog, Moose etc).
- Polymorphism is when in programming you abstract away from the
individual classes and can treat various different objects the same way due
to a shared interface of a group of classes.
And this still applies if subclasses implement methods differently, though
note that it's still polymorphism if they don't (e.g., if one subclass of
Animal just added an attribute and/or method that the client code never
mentioned):
- In inheritance, the same method (inherited from a super-class) can be
implemented by different sub-classes in different way.
- This is when a super class or interface type declared variable is used
to call the actual implementation of a method of the particular class's
instance it is referring to at runtime.
- It is OOP technique when classes have different functionality but they share the same interface.
- Where several objects override a superclass's method with different behaviours.
Several people mentioned method overloading, but that isn't really a kind
of polymorphism, so I'd say these are wrong, though well done for
remembering this feature of Java:
- Using the same name for a function(or constructor) but with different arguments.
- Exhibiting many forms of implementation, like a function showing overloading.
This, coming from someone who didn't seem to be taking the preassessment
totally seriously, was my favourite.
- You can turn your code to sheep
This page is maintained by
Perdita Stevens (
perdita@inf.ed.ac.uk
)