November 23, 2014
Hot Topics:

Encapsulation vs. Inheritance

  • August 4, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

A Code Example

Take a look at a specific code example that illustrates the problem that exists between encapsulation and inheritance. Consider the following class called Mammal presented in Listing 3.

// Class Mammal
public class Mammal {
   private String color;
   public void growHair(){
      System.out.println("Hair Growing");
   }
}

Listing 3

In this class, you have a private attribute called color and a public method called growHair(). To use the Mammal class, you create an application called Packaging (the choice of this name will become clear later). This class is presented in Listing 4.

//Class Packaging
public class Packaging {
   public static void main (String args[]){
      Mammal mammal = new Mammal();
      mammal.growHair();
      mammal.color = "blue";
   }
}

Listing 4

When you run this application, you get the output in Figure 4.



Click here for a larger image.

Figure 4

You can now test your encapsulation principle by attempting to have the application access the private attribute color as seen in Listing 5.

//Class Packaging
public class Packaging {
   public static void main (String args[]){
      Mammal mammal = new Mammal();
      mammal.growHair();
      mammal.color = "blue";    // try to access private attribute
   }
}

Listing 5

As you can see in Figure 5, the compiler just won't accept this.



Click here for a larger image.

Figure 5

This was all expected; however, try to add inheritance to the mix by adding a class called Dog, as seen in Listing 6.

//Class Dog
public class Dog extends Mammal{
   private int barkFrequency;
   public void bark(){
      System.out.println("Dog Barking");
   }
}

Listing 6

Now, you can start doing some interesting things. First, you can try to access Mammal's attribute color from the Dog class—see Listing 7.

//Class Dog
public class Dog extends Mammal{
   private int barkFrequency;
   public void bark(){
      System.out.println("Dog Barking"); 
      color = "blue";
   }
}

Listing 7

When you run this, the compiler complains again as seen in Figure 6. Yet, this may seem incorrect. Because Dog Is-A Mammal, doesn't the Dog contain all the attributes of Mammal? In short, is it not true that Dog inherits all the attributes and methods of Mammal?



Click here for a larger image.

Figure 6

This is an interesting design dilemma for object-oriented compiler builders. Here is the problem.

  • Inheritance dictates that a child class (subclass) inherits all the attributes and methods from a parent class (superclass).
  • Encapsulation dictates that one class must not have access to the (private) data of another class.

The way these rules are written, it seems that they are mutually exclusive. How can this conundrum be addressed?

Because encapsulation is the primary object-oriented mandate, you must make all attributes private. Making the attributes public is not an option. Thus, it appears that the use of inheritance may be severely limited. If a subclass does not have access to the attributes of its parent, this situation presents a sticky design problem.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel