All classes automatically inherit from the class Object
even if no extends clause is given. Look into your class documentation
for the class Object (in package java.lang).
Write a class InheritanceTest with no
methods (except main) and no data members. Check,
if you can use the method toString inherited
from the class Object. What is its output?
In the next exercises it is your task to model buildings. Each building
has a door as one of its components. Write a class Door
for this purpose:
In the class Door, model
the fact that a door has a color and three states, "open" , "closed",
and "locked". To avoid illegal state changes, make the state private.
Write a method (getState) that inspects
the state.
Write four methods (open, close, lock and
unlock) that change the state.
Initialize the state to "closed" in the constructor. Look for
an alternative place for this initialization.
Write a constructor that allows to pass the color of the door as a parameter.
Inspect the class Color in the class
library for this pupose or use a String to represent the color.
Define a Class Building for
building objects.
Write a method enter that
visualizes the process of entering the building (unlock door, open door,
enter, ...) by printing adequate messages, e.g. to show the state of the
door.
Write a corresponding method quit
that visualizes the process of leaving the house. Don't forget to close
and lock the door.
Test your class by defining an object of type Building
and visualizing the state changes when entering and leaving the building.
Extend exercise 3 by introducing a subclass HighBuilding.
An object of class HighBuilding contains an elevator and the height of
the building in addition to the components of Building.
Override the method enter
to reflect the use of the elevator.
Define a constructor that takes the height of the building (in meters)
as a parameter.
Define a subclass Skyscraper
of HighBuilding:
The number of floors is stored with each skyscraper.
What happens, if you don't define a constructor for class Skyscraper (Try
it)?
Write a constructor that takes the number of floors and the height as
a parameter.
Test the class by creating a skyscraper with 40 floors and using the inherited
method enter.