Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface Object?

 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Can we create an object of an interface??
For example, interface MyInterface{}
saying MyInterface mine = new MyInterface()......?

This may sound silly but can anyone give me reasons as to why we cant do like this?? (Afterall WHY is the question that is never answerable). So anyone why?

Regards,
Jothi Shankar Kumar. S
 
Charith Fernando
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we simply cant make an instance of an interface because we dont have an implementation of it... interfaces only carries the method definitions of the processes... it doesnt have the functionality of how to do it...

simply an interface gives a highlight of what it can do but doesnt implement how it is been done.. so making an object of an interface is worthless..

btw there is one particular place where you will see code which looks like it creates an instance of an interface.. that is where you create annonymous inner classes. but at that point you implement the class and create the instance at the same time which at the end gives you a proper implementation...

an Object/instance is worthless without a proper implementation
 
anil kuchana
Ranch Hand
Posts: 404
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface is just a contract of what should be done but it never says anything of how to do it..
it cannot be instantiated directly but a class that implements an interface can be instantiated
 
Joe Harry
Ranch Hand
Posts: 10085
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Above,

But I have read that we can create a refernce to an Interface???How is this helpfful? As said an interface dosnt have any implementations of its methods, so in this case how the reference variable is going to help?? Can anyone help me on this?

Regards,
Jothi Shankar Kumar. S
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Above,

But I have read that we can create a refernce to an Interface???How is this helpfful? As said an interface dosnt have any implementations of its methods, so in this case how the reference variable is going to help?? Can anyone help me on this?

Regards,
Jothi Shankar Kumar. S


A reference (to anything) never does anything anyway. It is merely a reference...an address of an object that can do something. When you call a method using a reference, the call is actually directed to an object pointed to by that reference. BTW, that is also the reason why you get a NullPointerException when a reference doesn't point to anything and you still call a method on it. There is nothing to execute the method!

So a reference to an interface allows you to call methods declared in that interface. But the actual execution is done by an object that implements that interface and is pointed to by the reference.

A reference to an interface is a very powerful concept. It allows you to change the actual object class without affecting the caller.

Since this is such a basic thing in OO programming, I suggest you read any good book on java programming. Thinking in Java is one such book that will clear all your fundamentals. You can download it for free.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,


Jothi Shankar Kumar Sankararaj posted October 28, 2006 09:05 PM

But I have read that we can create a refernce to an Interface???


Throw that book away.




The bold line shows a reference of an interface type.
What you can't do is something like
// new Swimmable(); //no!


Interface references can be helpful if you only want the objects to perform methods that are in the interface.
All objects of classes implementing Swimmable for example are guarantied to be able to swim().
If you have objects of subclasses of Fish (or Fish themselves), they all know to swim ("filho de peixe sabe nadar").
Or you can have other classes that implement Swimmable e.g. Boat that are no Fish.

Then you can store them all in an ArrayList<Swimmable> or an array of type Swimmable[] and call the swim() method on all of the elements. Very soon the ocean gets pretty crowded!


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic