Follow The Author

Saturday, 14 June 2014

“is a” and “has a” classes relationships

No comments :
     “is a” and “has a” classes relationships
There are two ways we can do code reuse either by implementation of inheritance (IS-A relationship), or object composition (HAS-A relationship).
·        IS-A Relationship:- The concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example House is a Building. But Building is not a House.
It is key point to note that you can easily identify the IS-A relationship. Wherever you see an extends keyword or implements keyword in a class declaration, then this class is said to have IS-A relationship.

·        HAS-A Relationship:- HAS-A simply mean use of instance variables that are references to other objects. For example: Suzuki has Engine, or House has Bathroom.

No comments :