aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Referring to objects by their interface vice their implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Referring to objects by their interface vice their implementation" Watch "Referring to objects by their interface vice their implementation" New topic
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Referring to objects by their interface vice their implementation
 
Similar Threads
Head First Java HashSet
Generic data structures and wildcard types
Set holding two dates
If set is interface then how it is instantiated:
Design for Collection, or its methods?