• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Question on calling a method in an child class

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get off me! Here, read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic