Subclass, Subtype, and Substitutability
The term subtype is used to describe the relationship between
types that explicitly recognizes the principle of substitution. A type B is considered to be a subtype of A if an instances of B can legally be assigned to a variable declared as of type A.
The term subclass refers to inheritance mechanism made by
extends keyword.
Not all subclasses are subtypes. Subtypes can also be formed
using interface, linking types that have no inheritance relationship.
Subclass
• Methods allows to reuse a sequence of statements
• Inheritance allows to reuse classes by deriving a new class from an existing one
• The existing class is called the parent class, or superclass, or base class
• The derived class is called the child class or subclass.
• As the name implies, the child inherits characteristics of the
parent(i.e the child class inherits the methods and data defined for the parent class
Subtype
• Inheritance relationships are often shown graphically in a
class diagram, with the arrow pointing to the parent class
Substitutability (Deriving Subclasses)
In Java, we use the reserved word extends to establish an
inheritance relationship
class Animal
{
// class contents
int weight;
public void int getWeight() {...}
}
class Bird extends Animal
{
// class contents
public void fly() {...};
}