Polymorphism

Exercises


  1. Starting from the classes Building, HighBuilding and Skyscraper from the last exercise (reusing classes), define a class Visits that stores an array of 10 buildings (representing a street). Define a method that enters all the buildings in the street using the method enter for each of the buildings, one after another. Fill the array with mixed objects from the classes Building, HighBuilding and Skyscraper. Make sure, that the output of your program visualizes the fact that different method implementations are used depending on the type of the actual object. If necessary, modify the building classes with further print statements.
  2. Define a Class Bank and a class Account.  (Account may be defined as an inner class, but we did not cover this in the lecture. So you can define it as a normal class)
  3. The next exercises are concerned with a simple implementation of money cards like telephone cards or credit cards. For this purpose define two interfaces:
  4. Define a third interface CardAll that includes all card operation (Use inheritance).

  5. Change the interface CardAll into an abstract class that implements the balance of the card and a basic solution for the methods fill and reduceBy leaving the rest of the methods abstract; In this implementation the PIN may be ignored; Choose the correct access specifier to make the balance accessible to subclasses but not to the public;
  6. Introduce a method getBankCard into the Bank class from exercise 2 that takes the name and the PIN of a person as a parameter and returns an object of type CardUse with the following properties:

    To accomplish this task, define a class BankCard that implements the interface CardUse. Test your card. (Again, you may define BankCard as an inner class).


Software Systems Institute

Home of the Java Course Claudia Niederée, nov 1998