Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is pure OOP, so please take much time to think about it, until you really have understood this.</p> <p>Answering the questions:</p> <blockquote> <p>I'm having trouble because I think that since in the Boat class, Color is private so an instance of Kayak doesn't have a color associated with it. Is this true?</p> </blockquote> <p>No, this is wrong. According to the UML, a boat has a color. This "property" can be seen by the getter <code>getColor</code> and the setter <code>setColor</code>. The private field is only an implementation detail, that does not matter. As Kayak extends (derives from) Boat, it can be said, that a Kayak <em>is a</em> Boat. This is called sub-typing. Additionally, Kayak inherits the code from Boat, thus also inheriting the property color. So, a Kayak also has a color.</p> <blockquote> <p>Also, where the the fact that the Boat class is abstract come into play, how does it affect Kayak?</p> </blockquote> <p>It does not really matter, whether the super class is abstract or not, when extending it. What matters is the following: If your super class is abstract <em>and</em> contains abstract methods and your sub class is not abstract, then your sub class must provide implementations for the abstract methods.</p> <p>There are no abstract methods so far in your Boat class. So for the extension (Kayak extends Boat) it does not matter, that Boat is abstract. In fact, that matters when considering the example expressions.</p> <blockquote> <p>Finally, does an instance of the Boat class have a numSeats associated with it?</p> </blockquote> <p>This question contains a mistake: There can not be instancs of the Boat class. Simply because the Boat class is abstract. You cannot create instances of abstract classes!</p> <p>The Kayak class is not abstract. Thus you can create instances from it. Kayak instances have a number of seats. But, as you could have other classes extending Boat, that do not have number of seats, you cannot say, that all boats will have a number of seats.</p> <p>By the way: Are you able to answer the question, which expressions are valid? Could you post your answers and reasons?</p> <blockquote> <p>I want to say the first two will not work because Boat is abstract. The third and fourth will work because a Kayak IS A Boat, so it can be set as a Boat class with Kayak parameters, and obviously a Kayak is a Kayak. The last one won't work as there is no such constructor.</p> </blockquote> <p>This answer is correct, but we should work on your wording.</p> <blockquote> <p>so it can be set as a Boat class with Kayak parameters</p> </blockquote> <p>A Kayak cannot "be set as a Boat class". Let's consider the expression</p> <pre><code>Boat boat3 = new Kayak(Color.yellow, 30.2, 1, 1); </code></pre> <p>You are creating a Kayak instance, calling the Kayak constructor that takes a Color, a double, and two integers (by the way: this is the only constructor). Then you are assigning the reference of this new Kayak instance to the variable boat3, which is of type Boat. Note, that I speak of type here, and not of class. As we mentioned before, a Kayak is a Boat, this assignment is correct.</p> <p>This OOP concept is called subtype polymorphism: A variable of type Boat could reference any Boat, so also subtypes of it. (In this case, it must be a sub type, as Boat is abstract)</p> <p>Just to clarify the wordings: Subclassing just means the technical process of extending a class. That also means inheriting the code of the super class. Subtyping means the is-a-relationship (a Kayak is a Boat), and is the key to subtype polymorphism. In Java, subclassing is always also subtyping.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload