• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface Queries : Java

 
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
I had few clarifications.

1> Can a Interface be instantiated. ?If yes,why and if not ,why?

2> I have a sample Java code as below.




Help provided will be highly appreciated.

--
Deepak Lal
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


You have posted a fully compilable example. Why don't you actually compile it, and see for yourself?

Henry
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok Agreed Henry

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.?

Help provided will be highly appreciated.

--
Deepak Lal
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1> can you give me the answer for Question 1 ?



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?

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.?



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).

Henry
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
Could you Please Clarify the below statements and please correct if Im Wrong in understanding the concepts related to interfaces.


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?



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.





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: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?

Dont you think we need to make changes to both the interface and underlying subclass method implementation.

Consider for Example: if i had to add an extra method in Map interface,i had to also change the HashMap implementation this implies i need to make 2 changes against 1 change as you are trying to say.

Please clarify the above concepts.

Help provided will be highly appreciated.

--
Deepak Lal
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Not sure what you are saying... Are you saying that you were able to instantiate an interface directly, and it worked? And what do you mean subclass? The question is instantiating an interface -- not a class.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 mean in the future, you can make the change like this...

Map map = new LinkedHashMap();

And you won't have to change any of the original code that uses the map reference, as it doesn't know about the implementation -- it is only using the map interface.

Henry
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
Thanks for your Awesome and outstanding replies and valuable inputs




I have understood the feature of using the map reference as you suggested.
Map map = new HashMap();
(or)
Map map = new LinkedHashMap();




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?



Help provided will be highly appreciated.

--
Deepak Lal

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?




Basically... I was trying to say that you can't directly instantiate an interface. i.e. you can't do this...

Map map = new Map();

But.... if you could... where would Java find the implementation to execute when you call one of the interfaces methods?


I was trying to get you to come to the same conclusion, of why you can't directly instantiate an interface.

Henry
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Thanks Henry,I got to understand what you are saying.

hence Map m = new HashMap(); holds good where Map is the interface and HashMap is the underlying class which has the implementation which is required in our actual code.

The m object created above basically acts as a reference for the underlying class.

Thanks Once gain for your valuable time and input.



--
Deepak Lal
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Lal wrote:The m object created above basically acts as a reference for the underlying class.

m is NOT an Object. It is a reference which refere to Object of HashMap class.
 
Deepak Lal
Ranch Hand
Posts: 603
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vishal Pandya for correcting me.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic