Using the Subclass in Place of the Superclass

Learn how to use the subclass in place of the superclass.

We'll cover the following

Polymorphism

Since the superclass is more general and the subclass is more specialized, objects of a subclass can be used in places where an object of the superclass type is required. This is called polymorphism.

The concepts of general and specialized types can be seen in statements like “this type is of that type”: “alarm clock is a clock”, “student is a person”, “cat is an animal”, etc. Accordingly, an alarm clock can be used where a clock is needed, a student can be used where a person is needed, and a cat can be used where an animal is needed.

When a subclass object is being used as a superclass object, it does not lose its own specialized type. This is similar to the examples in real life: Using an alarm clock simply as a clock does not change the fact that it is an alarm clock; it would still behave like an alarm clock.

Get hands-on with 1000+ tech skills courses.