Title: Polymorphism: Shapes (JPHS)
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER > Section: Learning Units - Bb

Presentations and Videos

- Polymorphism: Shapes (JPHS) lecture video (Time: 00:00)
- Polymorphism: Shapes (JPHS) document

- Polymorphic (Late Binding) definition - when overriden methods are involved, the decision of which of several methods to call is made at Run-Time and the Java environment knows what kind of object it is really working with. For normal methods (i.e. non-static methods), Java always makes its decisions in a polymorphic manner.
- Early Binding -This technique is used for static methods in Java. In the case of overriden static methods, the compiler will make the decision as to which method to call.
  - The preferred way to call static methods is by specifying the name of the class rather than the object. Using this approach eliminates the need for Early Binding.
- Example: Shape and its derived classes: Rectangle, Square, Circle, Triangle, RegularPoygon
- DemoShapes.java
  - Note the polymorphic decisions made on getArea, getPerimeter
  - Note the approaches used at the end on the static getCount method. Using the class name is preferred.
- Abstract class - a base class that we don't want constructed (like Shape). Often abstract classes have abstract methods (but not necessarily). The Shape example is reworked to make Shape abstract.Interfaces specify a list of methods which a class needs to implement. The Shape example is also recast to use a Shape interface.Interfaces specify a list of methods which a class needs to implement. The Shape example is also recast to use a Shape interface.
  - Classes which extend an abstract class and implement any required abstract methods are constructable.
- Interfaces specify a list of methods which a class needs to implement. The Shape example is also recast to use a Shape interface.