File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Question on calling a method in an child class

Toshiro Hitsuguya

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

Joined: Jul 08, 2003
Posts: 24199

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é

Joined: Nov 24, 2005
Posts: 14688

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: 684
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:
subject: Question on calling a method in an child class
It's not a secret anymore!