A key element in Object Oriented Programming (OOP) is the ability to derive new classes from existing classes by adding new methods and redefining existing methods. The new class can inherit many of its attributes and behaviors from the existing class. This process of deriving new classes from existing classes is called inheritance, which was introduced in Lesson A11, Inheritance.
In Java, all classes belong to one big hierarchy derived from the most basic class, called Object. This class provides a few features common to all objects; more importantly, it makes sure that any object is an instance of the Object class, which is useful for implementing structures that can deal with any type of object. If we start a class from “scratch,” the class automatically extends Object. For example:
public class SeaCreature{
...
}
is equivalent to:
public class SeaCreature extends Object{
...
}
when new classes are derived from SeaCreature
, a class hierarchy is created. For example:
public class Fish extends SeaCreature{
...
}
public class Mermaid extends SeaCreature{
...
}
This results in the hierarchy shown below.