Title: Inheritance/Polymorphism Summary
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Inheritance/Polymorphism Summary

Inheritance: Introduction

- Inheritance provides a way to eliminate duplication of code when supporting multiple variations of the same code
- Inheritance provides a way to conveniently make use of Building Blocks of code.
- Inheritance implies an "IS_A" relationship
- Examples: World of Living things(Life/Animals/Mammals/etc.), Game of Chess, Textbook Employee/SalariedEmployee
- Constructing Inherited classes using super(Using the textbook Employee, SalariedEmployee, etc example). Note the house construction model for how this operates.
- Calling methods in the parent class. For example super.toString().
- Note that sometimes the relationship between 2 classes should be "HAS A" instead of "IS A". If you get this wrong, the compiler won't know the difference.

Inheritance Odds and Ends

- Review of the usage of super in the previous video with Employee/SalariedEmployee/etc.
- Using the keyword "this" to have one constructor call another constructor to eliminate duplicated code
- IsADemo.java textbook example ... Note that SalariedEmployee "IS A" Employee
- EnhancedStringTokenizer.java to demonstrate adding features to an existing class.
- Overridden methods cannot become less accessable
- Overloading versus Overriding
- Protected access level

Inheritance More Examples

- Example: TitledEmployee extends SalariedEmployee
- Example: Square extends Rectangle - Note the problems that need to be solved with setWidth and setHeight.
- Note that boundingRectangle and intersectingRectangle still get defined as Rectangle even though we may passing Squares.

Inheritance Object Class

- Object class - the mother of all classes
- The Object class contains a number of methods including: equals, toString and getClass.
- The behavior of the default equals and toString methods in the Object class.
- Overriding toString and equals in Person.java to provide useful toString and equals methods.
- Overloading "equals" versus overriding "equals". Note the compiler will pick the version that is the closest fit. However, for the long haul in Java, we want to override the "equals" method.
- Writing a proper "equals" method: boolean equals(Object obj)
  - Check for obj being null
  - Check to make sure that obj is a compatible class that can be appropriately cast
    - Using the getClass approach - recommended by the textbook
    - Using "instanceof"
- Keyword "final"
  - Making a variable constant: final int num_rows =10;
  - Making a class so it can't be extended: final class MyFinalClass
  - Making a method so it can be overriden: final void finalMethod()

Polymorphism: Shapes

- 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.
  - 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.

More Polymorphic Examples

- Example: Shape example again, but this time we create a simple drawing program for ovals and rectangles. We make the Shape class abstract.
- Example: MakeMusic.java an example using an abstract class. Note that we can specify an array of abstract classes even though we cannot construct the abstract class MakeMusic
- Example: MakeMusic.java recast to use interfaces. Note that we now have a Singer class derived from Person which also implements the MakeMusic interface. Note that we can specify an array of objects which implement MakeMusic

Polymorphism Book Examples

- Textbook Example: LateBindingDemo.java uses Sale.java and Discount.java and demonstrates polymorphism(i.e. Late binding).
- Textbook Example: StaticMethodsDemo.java uses Sale.java and Discount.java demonstrates early binding used with static methods(i.e. the Compiler makes the decisions).
- Textbook Example: CopyingDemo.java - demonstrates 2 approaches to making a copy of an array. The better approach illustrates the usage of a clone routine.
- Textbook Example: reworking the Employee class to be an abstract class with an abstract method called getPay.

Inheritance/polymorphism Chess Game

- Example Dumb Chess game: A more realistic example of polymorphsim. In this example, a generic piece class and a generic board class is constructed. Then the generic classes get extended with more specific classes like Rook, Queen, Bishop, etc. There are no new concepts here, but the example provides a better picture of how class inheritance and polymorphism can provide a more intuitive solution.

Last Modified: June 1, 2014 1:11 PM