• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheritance, extended class.  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to complete my exercise but I have issues in it. I would be very grateful if you help me.

Here is my abstract Boat class.





Here is my extended class SailBoat.



and here is my driver class.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you have an actual question ?
TellTheDetails
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the question / problem is?

Welcome to the Ranch!
 
Ranch Hand
Posts: 198
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Suood wrote:
boatList[0].setMastHeight(12); // <<=== something is woring here in this line.



The issue is... setMastHeight(12) method is not a method of Boat class, and your instance is of a Boat class. Hence the issue.
If you want to set setMastHeight, you need to use the instace of SailBoat class.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Consider adding a setMastHeight method to the Boat class. If Boat is abstract, the method can be abstract, too.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhakar Reddy Bokka wrote:and your instance is of a Boat class.

To be more precise the reference variable is of type Boat and that is what is used to determine what methods can be called.
The instance is actually a SailBoat object.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Consider adding a setMastHeight method to the Boat class. If Boat is abstract, the method can be abstract, too.

But not all boats have masts, so that would not be a good design.

If you want to add a SailBoat instance to the array, you need to create it separately and then add it
e.g.

Alternatively you could have a constructor that sets all the variables as you appear to have with your MotorBoat class.

Or a third alternative is to use the Builder pattern.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!