Inheritance
In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:
​
-
subclass (child) - the class that inherits from another class
-
superclass (parent) - the class being inherited from
To inherit from a class, use the extends keyword.
In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass):
Polymorphism
Polymorphism means "many forms".
Polymorphism refers to the ability of a class to provide different implementations of a method, depending on the type of object that is passed to the method. Polymorphism in Java allows us to perform the same action in many different ways.
For example, think of a superclass called Animal that has a method called animalSound(). Subclasses of Animals could be Pigs, Cats, Dogs, Birds - And they also have their own implementation of an animal sound (the pig oinks, and the cat meows, etc.):
CLICK THE IMAGE TO SEE THE CODE
​