Implementing Inheritance ,Is-A and Has-A Relationships in Java

Hi Freinds, Today  i came with one of the OOP  principles by which an object acquires the properties of another object. That is the concept of inheritance supports hierarchical classification. Without inheritance, we need to define all the characteristics of each object explicitly. For example, a TabbyCat is  atype of cat, which, in turn belongs to the mammal class, which comes under the larger class, animal.

In java inheritance is implemented in the code by using the extends keyword and modifying the  inherited members.The concept of inheritance is closely related to accessibility, implying that a private class member of a superclass cannot be inherited by a subclass. However, if a superclass member is accessible by the subclass, it implies that the accessible member is inherited.

We also need to focus on the key reasons of using inheritance, that is, promoting the re-usability of code. See this below example


  1. package com.vbbs;
  2. //declaring the superclass
  3. class Animal {
  4. public void catchPrey() {
  5. System.out.println(“Using Superclass”);
  6. }
  7. }

  1. The Animal superclass is declared, which defines the catchPrey() method. The catchprey() method is marked public so that it can be accessed by a subclass.

Now we create a subclass Cat, which extends the Animal class and defines a new method displayEyes().

The Cat Subclass:


  1. package com.vbbs;
  2. //declaring the subclass
  3. class Cat extends Animal
  4. {
  5. //declaring  a method of the subclass
  6. public void displayEyes() {
  7. System.out.println(“Cat has brown eyes”);
  8. }
  9. }

Here The Cat class inherits all the class members of its superclass Animal.one more subclass Tiger we can create now.

The Tiger Subclass:


  1. package com.vbbs;
  2. //declaring the subclass
  3. class Tiger extends Animal
  4. {
  5. //declaring  a method of the subclass
  6. public void displayEyes() {
  7. System.out.println(“Tiger has dangerous eyes”);
  8. }
  9. }

Now, let’s create the TestAnimal  class, which creates an instance of the subclass Cat.The bellow shows how to create instances of the subclasses:

The TestAnimal Class:


  1. package com.vbbs;
  2. //declaring the subclass
  3. public class TestAnimal
  4. {
  5. public static void main(String[] args) {
  6. //creating instance of the Cat class
  7. Cat c = new Cat();
  8. //accessing the methods of the Cat
  9. c.displayEyes();
  10. // accessing the methods of the superclass of the Cat
  11. subclass c.catchPrey();
  12. // creating instance of the Tiger class
  13. Tiger t = new Tiger();
  14. //accessing the methods of the Tiger class
  15. t.displayEyes();
  16. //accessing the methods of the superclass
  17. Tigerd ubclass t.catchPrey();
  18. }
  19. }

Here the TestAnimal class and accesses the methods of the superclass and subclass by creating an instance of the subclasses Cat and Tiger. The TestAnimal  class invokes the methodlass as already declared in the superclass Animal and, in turn, hides the implementation of the subclass and superclass methods.

 

Inheritance Hierarchy

The Animal superclass extends the Object class from the java.lang.package. Every class in java is  implicitly a subclass of the Object class.

A subclass can have either an IS-A or a HAS-A relationship with its superclass. Now, we will discuss the IS-A and HAS-A relationships.

The IS-A Relationship

The IS-A relationship corresponds to the concept of inheritance. This can be explained with an example. Suppose class A is a subclass of class B. In this case, we say that an IS-A relationship exists between class A and class B. we can say this class A is a specialization of class B or  class B is a generalization of class A.

We take one example to explain this an apple is a fruit, a house is a building, economics is a subject and Java is a Technology. These all are depict the IS-A relationship, because an apple is a type of fruit and has most of the characteristics of a fruit.The same kind of relationship is depicted in the other examples.


  • class subject {
  • //class implementation
  • }
  • class Economics extends Subject {
  • }

Here the Economics class extends the Subject class.This code depicts the IS-A relationship between the Economics and Subject classes

The Subject class is a superclass of the Economics class.

The Economics class is a subclass of the Subject class.

The HAS-A Relationship:

The HAS-A relationship is a relationship where one object is a member of another object. In other words, the HAs-A relationship is based on containment rather than on inheritance. This kind of relationship is also known as composite-type relationship.

For ex: we can say that an apple has a taste, a house has a livingroom, Economics has a concept, and java has a class. These all are show the HAS-A relationship, implying that though an apple is a fruit, it also has a taste. In simple words, we can say that if class A has a reference to an instance of class B, then class A is said to have a HAS-A relationship with class B.


  1. public class Subject {
  2. ………….
  3. }
  4. public class Economics extends Subject {
  5. private concept c;
  6. }

In the above, the Subject and Economics classes are in IS-A relationship, whereas the Economics and Concept classes have a HAS-A relationship.

 

The HAS-A Relationship between the Economics and Concept classes.

Leave a Reply

Your email address will not be published. Required fields are marked *