Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 4

Inheritance: Build objects in layers

  • Print
  • Feedback

Page 10 of 10

class Point
{
   private double x, y;
   Point (double x, double y)
   {
      this.x = x;
      this.y = y;
   }
   double getX ()
   {
      return x;
   }
   double getY ()
   {
      return y; 
   }
}
class Circle
{
   private Point p;
   private double radius;
   Circle (double x, double y, double radius)
   {
      p = new Point (x, y);
      this.radius = radius;
   }
   double getX ()
   {
      return p.getX ();
   }
   double getY ()
   {
      return p.getY ();
   }
   double getRadius ()
   {
      return radius;
   }
}


From an external interface (not to be confused with the Java concept of interfaces) perspective, a Circle object created from this class is equivalent to an object created from the Circle class in this article's first code fragment. It would seem that it doesn't matter whether or not inheritance is used. But it does: Take a good look at Circle. Notice the getX() and getY() methods. Those methods duplicate their counterparts in the Point class and do not promote code reuse. By focusing exclusively on composition and ignoring inheritance, you've added redundant code to Circle. On a larger scale, this sort of redundant code could become a maintenance nightmare.

Review

Objects have layers, and those layers result from inheritance. This article explored the object-oriented programming principle of inheritance and introduced you to single and multiple inheritance. You learned how to extend classes (which Java refers to as implementation inheritance), picked up some inheritance terminology, and examined those rules related to calling superclass constructors, overriding methods, and casting subclass object references to superclass references (and vice versa). Finally, you learned how inheritance and composition are related from an object-oriented design perspective.

Every subclass object is also a superclass object. What about those objects whose classes don't use extends to inherit capabilities from a superclass? Are such objects also subclass objects of some superclass? The answer is yes. In the absence of extends, a class inherits from the root of all classes. And what class might that be? Tune in next month for the answer.

About the author

Jeff Friesen has been involved with computers for the past 20 years. He holds a degree in computer science and has worked with many computer languages. Jeff has also taught introductory Java programming at the college level. In addition to writing for JavaWorld, he wrote his own Java book for beginners -- Java 2 by Example (QUE, 2000) -- and helped write a second Java book, Special Edition Using Java 2 Platform (QUE, 2001). Jeff goes by the nickname Java Jeff (or JavaJeff). To see what he's working on, check out his Website at http://www.javajeff.com.
  • Print
  • Feedback

Resources