Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays

 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didnt define method1 in either animal or pet so what would you expect?
 
dhana rangu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Gian franco

I got it now.Thanx for ur help

dhana
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic