aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "arrays" Watch "arrays" New topic
Author

arrays

dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65

In the above code i am getting an error " cannot resolve the symbol method1() " when i am trying to acees method1 in the line a[2].method1().plzzz help
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi dhana,
When you create the array of the animal class in the
first line of the main, all the elements are upcasted
to the animal class.
In order to use your method (method1) you should
explicitly downcast a[2] (i.e. the formerly upcasted
dog object) by doing:
((dog)a[2]).method1();
Gian Franco


"Eppur si muove!"
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
You didnt define method1 in either animal or pet so what would you expect?


SCJP 1.4<br />SCWCD 1.4
dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
hi Gian franco

I got it now.Thanx for ur help

dhana
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi dhana,
Coming back to your question. If polymorphism is desired the base class animal should provide the common interface to the derived classes. The subclasses should override these definitions to provide unique behavior for each specific type of animals. Not defining the methods in the base class or giving them a private access specifier inhibits polymorphism.
Gian Franco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays