aspose file tools*
The moose likes Beginning Java and the fly likes Question on calling a method in an child class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on calling a method in an child class" Watch "Question on calling a method in an child class" New topic
Author

Question on calling a method in an child class

Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19


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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

This is caused by a bad indentation. See my comments in the code.



[My Blog]
All roads lead to JavaRanch
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on calling a method in an child class