aspose file tools*
The moose likes Beginning Java and the fly likes Interface Queries : Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface Queries : Java" Watch "Interface Queries : Java" New topic
Author

Interface Queries : Java

Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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


When The Going Gets Tougher,The Tougher gets Going
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40



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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Lal
Ranch Hand

Joined: Jul 01, 2008
Posts: 507

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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

Joined: Jul 01, 2008
Posts: 507

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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

Joined: Jul 01, 2008
Posts: 507

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
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40

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

Joined: Jul 01, 2008
Posts: 507

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
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
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

Joined: Jul 01, 2008
Posts: 507

Thanks Vishal Pandya for correcting me.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Queries : Java