2

According to my textbook, the ability of a class to extend another class is known as inheritance. A subclass inherits all the fields and methods of its superclass, along with the type of its superclass. According to page 294 of my textbook, "Java Methods: Object-Oriented Programming and Data Structures",

Inheritance represents the IS-A relationship between types of objects. A superclass defines more general features of the objects of its subclass; a subclass defines additional, more specific features (fields and methods) and may override (redefine) some of the methods of the superclass.

After reading this passage, I have concluded that a class John IS-A Person, if John extends Person The textbook does not define the HAS-A relationship, but it is reasonable for me to conclude, if John extends Person, that Person HAS-A John. This is logically consistent, and explains inheritance in a simple manner.

My instructor, however, assigned us a quiz over Class Hierarchies and Interfaces. The following questions are from the quiz.

Consider the following definitions:

public class Brush implements Drawable{...}

public class Canvas extends JPanel
{
 private Brush currentBrush;
 ...
}

public class Painter extends Person
{
 private Canvas canvas;
 ...
}

True or False? Based on the code shown, the following describes a relationship between the objects:

A Canvas HAS-A Brush

The answer provided by the instructor was True

A Person HAS-A Canvas

The answer provided by the instructor was False

A Painter HAS-A Brush

The answer provided by the instructor was False

The provided answers make sense in a general sense, but do not satisfy the logical conclusions I reached through reading the book and several other online sources. Who here is confused?

John Henry
  • 33
  • 6
  • 2
    You are confused. A **has-a** relationship implies that one class contains fields of another. For example, a `Person` **has-a** `birthDate`. Note that a `Date` doesn't usually have a `Person`. – Elliott Frisch Dec 17 '15 at 01:21
  • 2
    *has-a* is not the inverse of an *is-a* relationship, it is a different type of relationships. For example, a car *is a* means of transportation (inheritance), but it *has a* wheel and brakes (composition). – Robin Krahl Dec 17 '15 at 01:30
  • Look at [Composition](https://en.wikipedia.org/wiki/Object_composition#Details). – user1803551 Dec 17 '15 at 01:33
  • @ElliottFrisch Thank you. That makes great sense. Have a nice day! – John Henry Dec 17 '15 at 02:00
  • @RobinKrahl Thanks you for this definition. This makes much more sense now that I think about it. – John Henry Dec 17 '15 at 02:01
  • @user1803551 Thank you for this link. The differences between inheritance and composition really make the issue extremely clear to me, now that I look back at it. – John Henry Dec 17 '15 at 02:02
  • You're on a roll but don't stop there. Next is [subtyping vs subclassing](http://stackoverflow.com/questions/7072290). – reaanb Dec 17 '15 at 08:38

0 Answers0