• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on calling a method in an child class

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am trying to call the moveInDirection method in this other class:




Whenever, I try to call the method in my bouncing ball class, the constructor says "invalid method declaration; return type required". How can I fix it?

Thank You
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That call to moveInDirection() appears just after the closing brace of the draw() method, meaning it's not inside any method. A normal line of code, like a method call, can only appear inside a method in Java. Swap it with the line before it, and all will be well.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is caused by a bad indentation. See my comments in the code.

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two points...

1.perhaps I'm just stating the obvious, but this is a compile problem right? To state "Whenever I try to call" makes it sound like runtime.

2. Correct me if I'm wrong, but BouncingBall is a child of abstract class BouncingDevice. Isn't the child supposed to inherit the method from the parent, not call a method in the parent? And doesn't that make the "fixes" wrong? We should be fixing a bad declaration, not putting a call in the correct location.


Ok I have mistated point 2. The compiler sees this as an attempt to ovverride the method in the parent, and the programmer may not want to ovveride the method. But it still feels wrong to call a method in an abstract class, and doubly wrong to call a method in a parent from a child. It seems we should be referencing the method as being in BouncingBall, not from Bouncing ball. e.g. from some other class we would have...

BouncingBall bb = new BouncingBall().

...

bb.moveInDirection( dx, dy)

with no reference at all to moveInDirection with the class BouncingBall.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic