Results 1 to 2 of 2
Thread: Reference Types - Interfaces?
- 10-12-2013, 07:52 PM #1Senior Member
- Join Date
- Jul 2011
- Rep Power
Reference Types - Interfaces?
In this tutorial, a reference type is given as
"If you declare a variable using the name of a class for its type, that variable can contain a reference to an object of that class. Such a variable does not contain an actual object, but rather a reference to the class instance, or object, the variable refers to. Because using a class name as a type declares a reference to an object, such types are called reference types."
OK I got it.
But then they say, "Java also allows the use of an interface name to specify a reference type."
What I have understood from this is that a reference to an object of the class which implements the interface can be stored in a reference variable declared with the name of the interface implemented by its class for its type.
But then I was thinking, why would I even do that? When I can simply create the reference variable with the name of the class for its type, why would I do that with an interface being implemented by its class.
Last edited by JosAH; 10-12-2013 at 08:25 PM. Reason: removed all font, size, boldace, italics tags; it was a mess ...Don't forget to smile :-)
- 10-14-2013, 01:09 AM #2
Re: Reference Types - Interfaces?
Why? Maybe you want to be able to assign objects of different classes that implement the interface to that variable. Or maybe you don't even know what class the object you're referring to actually is. Sometimes factory methods return instances of private classes that implement known interfaces.Get in the habit of using standard Java naming conventions!
- By kathmandu in forum New To JavaReplies: 3Last Post: 07-24-2013, 05:03 PM
- By blobbyx22 in forum New To JavaReplies: 4Last Post: 11-27-2011, 10:35 AM
- By guilty in forum Advanced JavaReplies: 1Last Post: 11-05-2010, 07:16 PM
- By sireesha in forum New To JavaReplies: 5Last Post: 01-16-2008, 05:52 PM
- By Kavana Krishnappa in forum New To JavaReplies: 7Last Post: 12-11-2007, 04:28 PM