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?