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..
does DriverManager.getConnection("jdbc:mysql://localhost/JDBC","root","ijse"); return Connection interface ?
Please some body.. clear this out..
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...
Please help me, I am beginner, I can't understand these concepts very clearly...
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 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...
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.
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."