• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can Objects Create From Interfaces?

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it work something similar to like this... ?
This is what I guess after reading the tutorial...




Thank you... Looking forward your replies...
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create an instance of an interface as an anonymous class, but that is rather different.
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much... Sir..
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic