This works for class and subclass but not for interface?? Please clear my doubt.
Thanks in advance
Anupam
Vijay Kokatnur
Greenhorn
Joined: Aug 10, 2006
Posts: 2
posted
0
Originally posted by Anupam Mittal:
public interface FirstInterface { String getName(); }
}
Got it? Your interface doesn't have helloIndia method. It gives you compilation error!!! [ September 11, 2006: Message edited by: Vijay Kokatnur ]
Anupam Mittal
Greenhorn
Joined: Aug 25, 2006
Posts: 8
posted
0
Yes we will get compilation error but i was just trying to figure out the reasons why it is not allowed...but i am not able to ascertain the reasons... can somebody help??
Thanks
Anupam
Robert Hill
Ranch Hand
Joined: Feb 24, 2006
Posts: 94
posted
0
FirstInterface a=new StaticTest();
You have an FirstInterface reference. When you do this you only have access to the defined interface methods. There are benefits to creating a reference like this, but like in everything else there are negative tradeoffs.
Try this and see if you can access any method in StaticTest:
Object o = new StaticTest(); [ September 11, 2006: Message edited by: Robert Hill ]
Anupam Mittal
Greenhorn
Joined: Aug 25, 2006
Posts: 8
posted
0
Guys,
I dont have any problem with getName()it is executing fine and since both interface and class in same pacakage public or default modifer does not matter.
Problem is helloIndia() method i was in impression that it shd get executed. See the follwoing lines
So a is the reference of type FirstInterface type and since StaticTest class implements FirstInterface variable a should be able to hold the reference of StaticTest class but when i try to make a call to method helloIndia() which is written in StaticTest my program is not compiling... trying to figure out why???
please help
Thanks
Anupam
Robert Hill
Ranch Hand
Joined: Feb 24, 2006
Posts: 94
posted
0
I already answered it, perhaps it is too vague, so I will try again.
The only methods a reference can see are those of its own type and parents. FirstInterface, in this case can access its own method and the methods of Object. That is it. FirstInterface knows nothing about any classes that could implement it, and why should it? How many classes could potentially implement it?
What you are doing is called polymorphism, I suggest you read more on it, and its implementation in Java. [ September 11, 2006: Message edited by: Robert Hill ]
Anupam Mittal
Greenhorn
Joined: Aug 25, 2006
Posts: 8
posted
0
Got it guys... :-) I appreciate Robert for his help. This clears one of my fundamental doubt. Thanks
Anupam
Burkhard Hassel
Ranch Hand
Joined: Aug 25, 2006
Posts: 1274
posted
0
Hi, cowboys!
Anupam assumed:
This works for class and subclass but not for interface??
No doesn't. Won't work for class, won't work for interface.
Consider this for classes:
Now, if you have somewhere
A polyVar = new B();
Then object type of polyVar is B, but the methods you can invoke depends only on the reference type, A in this example. Only Methods of class A (and superclasses) can be called on polyVar.
polyVar.aMethod(); is fine but polyVar.bMethod(); does not compile.
Yours, Bu.
ps: don't mix up polyVar with Bolivar since the south american nation is called Bolivia and not Polyvia