| Author |
Referring to objects by their interface vice their implementation
|
John Pritchard
Ranch Hand
Joined: Nov 15, 2002
Posts: 49
|
|
Hello All- I'm on that close brink of the light bulb above my head shinning brightly (it's only sparking at the moment). I'm having a little difficulty grasping the differances of having a variable that refers to an object by of an interface type the object implements vice the Class of object itself. In other words. If I have: does this mean I only have the methods and variables defined in the interface Set provied in the object s? vice which would give me all the methods provided by the Class I conceptually understand the programming practice of referring to an object by it's interface type and thus allowing one to change the implementation later without problem. What are the implications of this approach? Thanks, John [ November 29, 2002: Message edited by: John Pritchard ]
|
John Pritchard<br />If a JTree falls in the woods, is it Observable
|
 |
John Pritchard
Ranch Hand
Joined: Nov 15, 2002
Posts: 49
|
|
er, as I thought about this more, since the class implements the interface, all the vars and methods are provided by it so scratch quetsion #1. Still trying to understand what the difference provdes you though. -John
|
 |
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
|
|
|
The compiler will not allow to invoke any method not defined or inherited in the type of the variable. If the class adds methods that are not defined in the interface, it is not possible to invoke them through a reference to the interface. But you can cast it if needed.
|
SCJP2. Please Indent your code using UBB Code
|
 |
John Pritchard
Ranch Hand
Joined: Nov 15, 2002
Posts: 49
|
|
Jose- Thanks for the reply. Let's se if I have this correct. By defining an object of it's interface type I gain the advantage of being able to change the implementation class later if I need to (without having to make a lot of other modifications). I class defined by it's interface type will only have vars/methods in inteface avaiable to it. If I however need specific behavior defined in the implementation class, I can always cast the the object to it's implementation class. If I got this correct, thanks for the help. - John
|
 |
 |
|
|
subject: Referring to objects by their interface vice their implementation
|
|
|