wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interface Object?" Watch "Interface Object?" New topic
Author

Interface Object?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9501
    
    2

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


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Charith Fernando
Ranch Hand

Joined: Sep 12, 2005
Posts: 67
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


Charith I Fernando<br />SCJP5, SCWCD, SCBCD, BSc(Hons) IS<br />+94 773 263 222 (mobile)
anil kuchana
Ranch Hand

Joined: Sep 08, 2000
Posts: 404
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


http://www.skillfox.com Home of Java Skills SCJP 5.0, SCBCD, SCEA mock exams
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9501
    
    2

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

Joined: Sep 23, 2000
Posts: 3324
    
    8
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface Object?