Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question on calling a method in an child class

 
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
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is caused by a bad indentation. See my comments in the code.

 
Ranch Hand
Posts: 686
  • 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.
 
It was the best of times. It was the worst of times. It was a tiny ad.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic