Sunday, May 3, 2015

Singly rooted heirarchy in Java

Let us explore the IS-A relationship.

A Driver IS-A Person
(Meaning the Driver class extends the Person class)

A Cat IS-A(n) Animal

A Square IS-A Shape

Extending a class means providing some specific functionality in the new class.

This IS-A relationship between classes is called inheritance.

Not every person is a driver. The Driver class denotes a specific category of people who have a valid driving license.  

A driver can do whatever a person can do but the opposite is not true.

In Object-Oriented Programming it can be said that:

A Driver object can do whatever a Person object can do. IT CAN ALSO DO ADDITIONAL THINGS (read driving). A Driver is-a-kind-of Person.

Read the first statement again. We see that both drivers and persons are treated as objects in OOP programming.

In Java, everything is an object (except primitives)

The Driver class extends the Person class which in turn extends the Object class (predefined in Java). So a Person (and therefore a Driver) IS-A(n) Object.

 Each class can extend only one class in Java. There are several hierarchies of  classes but there is a single class at the root of each and every hierarchy which is the Object class.



This finishes the discussion of singly-rooted hierarchy in Java.