• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getAction

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wondering why i cant invoke my getAction?

Complier says
PSt5.java:60: error: cannot find symbol
owner.pets[ 4 ].getAction();
^
symbol: method getAction()
location: class Pet
1 error





 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the error message is telling you, the class Pet does not have a getAction() method. All the compiler knows is that each element of that array is a reference to a Pet. It doesn't know that the actual object it's pointing to is a Goldfish. You have to either cast it to Goldfish, or pull the getAction() method up to the Pet class.

And if you end up casting, that means you need to know and care which specific classes are in the array, which means you probably have a design flaw, and it shouldn't be an array of Pet, and you shouldn't be mixing Goldfish and other Pets in it.

On the other hand, if you define the method in Pet--and you should only do so if it makes sense for all Pets to have a getAction() method--then you can mix different kinds of Pets in the array and you only ever have to know or care that each one is a Pet, without worrying about which specific kind of Pet.
 
John Valiant
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, i cant put it to Pet as its an abstract class. So i have to cast it..what do you mean by it?How?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Valiant wrote:ok, i cant put it to Pet as its an abstract class.



You can still define the method in Pet. You can make it abstract or you can make it concrete. Abstract classes can have concrete methods.


So i have to cast it..what do you mean by it?How?



I don't think casting is the right approach here. If you want to learn about casting, google for something like java casting tutorial.
reply
    Bookmark Topic Watch Topic
  • New Topic