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

Can Objects Create From Interfaces?

Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Hey, I am bit confused...
My teacher says, you can't create object from interfaces...
I don't know it is true or not.. ?
But I found this..



My problem is.. According to the API, Connection is interface..
Then
does DriverManager.getConnection("jdbc:mysql://localhost/JDBC","root","ijse"); return Connection interface ?

Please some body.. clear this out..
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

It means you cant directly create an Object with "new" keyword of an "Interface" or an "Abstract class".
But there are Factory Methods which returns the Object of Interface or Abstract Class.

I hope you understood.


OCPJP
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Hi,

I found this , here "http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html"


When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.


Here, Where I can't understand ..

Connection is an interface according to the API
Connection <identifier> means connection reference data type

what actually DriverManager.getConnection() method returns ? interface or object ?

I think it is interface...

If it is interface, I will be really confused...

Thank you...
Please help me, I am beginner, I can't understand these concepts very clearly...
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Does it work something similar to like this... ?
This is what I guess after reading the tutorial...




Thank you... Looking forward your replies...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
You can create an instance of an interface as an anonymous class, but that is rather different.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Campbell Ritchie wrote:You can create an instance of an interface as an anonymous class, but that is rather different.


Could you please explain actually what happened above code ? Is it all right with my thinking process ? Any suggestions ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
That is how you can create an object as an instance of an interface, but not how you can connect to a database. For database connections, the class is supplied ready‑made by the database supplier, and you need to follow the instructions in their tutorial or handbook.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Thank you very much... Sir..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
You’re welcome
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

I know it's just semantics but, like Fred R said recently, and I paraphrase: we developers like be precise in our language. Instances of an anonymous class are NOT instances of an interface; they are instances of an unnamed class that implements the interface. There is no such thing as an "instance of an interface" -- there are only instances of classes that implement them. In other words, there are only instances of interface implementations.

Like I said, it's really just a matter of semantics. I use the phrase "an instance of someInterface" all the time because it's a convenient shortcut even though I know it's technically inaccurate.

Campbell was right in implying that the teacher misspoke by saying that "You can't create object from interfaces" because you obviously can use an interface to define and instantiate an anonymous inner class.

Try this out:


The last System.out statement in main is meant to confuse you even more


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4462
    
    6

Although now that I think about it, the statement "You can't create objects from an interface" is technically correct. See InstantiationException. You can create objects using just an interface to define an anonymous class but instances are still created from the anonymous class, not from the interface. Again, it's just a matter of semantics and how anal you want to be about the subtleties.
Ranji Sura
Ranch Hand

Joined: Oct 28, 2012
Posts: 84
Junilu Lacar wrote:Although now that I think about it, the statement "You can't create objects from an interface" is technically correct. See InstantiationException. You can create objects using just an interface to define an anonymous class but instances are still created from the anonymous class, not from the interface. Again, it's just a matter of semantics and how anal you want to be about the subtleties.


Thank you sir...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

As you say, it's important to use the correct words. Here's an example:



Notice that we're creating an object, an Integer object, "from" that interface. Did the original poster mean that?

Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

Connection is an interface indeed of which an object can not be created directly...
When you write: Connection conn=DriverManager.getConnection....
[I will not go into details but you should think llike the following:]
"conn" will store an object of type Connection (that does not necessarily means Connection object, the object might be of a subclass or implemented class...
somewhere the Connection interface was implemented and an object was created of that class. The static method getConnection() of DriverManager class just returns that object only.

All the important info has been already given by others in the same thread already.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 356

I think you're confused with the word "interface."


Interface in Java is set up to create methods that are to be used by classes that implement it. It's just a set of methods, that's all.


List is an interface, and you can do List L = new LinkedList(); or just do LinkedList L = new LinkedList();


Other "interfaces" would like you mentioned JDBC, a way to connect to a DB. That's a different type of "interface."


"In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software. This allows a component, whether a piece of hardware such as a graphics card or a piece of software such as an Internet browser, to function independently while using interfaces to communicate with other components via an input/output system and an associated protocol."

http://en.wikipedia.org/wiki/Interface_(computing)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can Objects Create From Interfaces?