• 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

interface and concrete classes

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a reference of an interface assigned to a concrete class which implements that interface can I call methods that are defined in the implementing class but not declared in the interface ?

for example :

my interface is


my abstract class is


my concrete class is


my test class is


I cannot call method2 on the obj as it is not defiened in the abstract class though its there in TestConcrete.
If I make the refernce type as interface Testif still I cannot call method2 even though the reference points to TestConcrete.

As far as I understand the method calls are made on the type of the object the reference points to and not on the class type of the reference. So why are these calls not possible ? Why can I call only those methods that are declared in the interface / abstrace class ?
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I understand, the compiler knows the object only as the label you gave it when you named it, which was TestAbs in the case of obj. What constructer you used is all well and good, but to the compiler it IS a TestAbs. The solution is easy; just add a typecast, i.e.



-Joe
[ May 27, 2004: Message edited by: Joseph George ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I've a reference of type A, then I can only use that reference to invoke methods defined as available to things of type A.

As mentioned, I could also use that reference to create a new reference of a different type by using a type cast. Then, the same rules apply to my new reference.
 
Kaustubh Patil
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe and Dirk. I did try that after I posted this query and it worked !!!
 
I promise I will be the best, most loyal friend ever! All for 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