top of page

Inheritance

Inheritance can be defined as the process where one object acuires the properties of another.

IS A Relationship

 

 

Is-A- is a ways of saying : This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

With use of the " extends " keyword, the subclasses will be able to inherit all the properties of the superclass.

We can assure that Mammal is actually an Animal with the use of the

" instance of " operator.

Implements Keyword: 

 

The " Implements " keyword is used by classes to inherit from interfaces.

 

 

 

This shows that class Van HAS -A speed. By having a separate class for Speed we do not have to put the entire code that belongs to speed inside the Van class, which makes it possible to reuse the Speed class in multiple application.

Java only supports only single inheritance. This means that a class cannot extend more than one class. However a clas can implement one or more interfaces.

InstanceOf Operator:

You can make a logical test as to the type of particular object using the "instanceOf operator". This can save you from a runtime error owing to and improper cast. for example :

Subclass Constructors

From the Bicycle example where MountainBike is a subclass of Bicycle, the mountainbike (subclass) constructor and then adds initialization code of its own :

Object as superclass

 

The object class, in the java.lang package,sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the object class.

bottom of page