1> can you give me the answer for Question 1 ?
2> My next question is why does Interface i = new B() actually work.? what is the concept behind this.can you explain why it works.?
Henry's Statement : Well, let's say you can instantiate an interface directly... What should java do when you try to actually call one of the methods? Where will Java fine the actual implementation to execute?
Basically, B IS-A I. This is because any object can be treated via it's interface. The more classic case, which you probably used before is....
Map map = new HashMap();
Here the HashMap class is referenced by it's interface. In the future, if you want to change the implementation, you only need to change it in one location (for all the places that use the map reference).
My Reply: Yes,Interface gets instantiated.a reference is created and using that reference we can call the sub classes method.so it implies Java calls the methods in subclass using the interface reference.Java will find the actual implementation of the methods in Subclasses whereas the method definition is in the interface. --->Please correct me if my understanding is Wrong.
My Reply:I have a clarification in the statement which you are making " In the future, if you want to change the implementation, you only need to change it in one location (for all the places that use the map reference)", So what does one location refer to here and which is that one location?
I have understood the feature of using the map reference as you suggested.
Map map = new HashMap();
(or)
Map map = new LinkedHashMap();
Well, let's say you can instantiate an interface directly... What should java do when you try to actually call one of the methods? Where will Java fine the actual implementation to execute?
Deepak Lal wrote:
Could you please clarify what does the "below statement made by you" mean.
Well, let's say you can instantiate an interface directly... What should java do when you try to actually call one of the methods? Where will Java fine the actual implementation to execute?
m is NOT an Object. It is a reference which refere to Object of HashMap class.Deepak Lal wrote:The m object created above basically acts as a reference for the underlying class.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |