Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic question regarding Interfaces and classes which implement them 1

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

Let's say I have Interface IA and a class A which implements this Interface.
In many vendors code I see:
IA myA = new A() in addition many get methods return IA instead of A like this IA myA = myObject.getA()...

My question is this: What is the benefit from assigning the created Object into the Interface and not into the implemented class. Clearly the new A() ans well as the getA() creates an instance in memory, so why use this method? Is it just for convenience reasons or is there an efficiency consideration I am not aware of... ?

Roy
 
First of all, I'd never use a code like that.

Anyway, interfaces are just to offer some functionality and hide the implementation details, there's no efficience gaining under there.

Cheers,

Dian
 
In the future, please do not post standard Java questions in the J2EE forum - this type of question should be posted in forum269 .

Take JDBC and the core class java.sql.Connection. This is an interface. DB vendors must implement their own version of this class in order to communicate with their particular database.
The joy of interfaces is that you can write :

java.sql.Connection = DriverManager.getConnection();

which returns an implementation of the java.sql.Connection - a vendor specific class that has the full functionality specified in the interface. But it does not matter which vendor you use, or which concrete class is returned - your code can always be the same.

If you need to know more, I'd read up on Objet Orientated programming.

--------------------------------------------------
Free Database Connection Pooling Software
 
Hey sedj,

First, I apologize for submitting it at the J2EE forum, it was an honest mistake. secondly, I know how Interfaces wrork, no need explain this...
Let me clarify my question. When I said "Vendors" I didn't mean JDBC vendors I ment 3rd parties code. For example, I am using SAP API and they are using this convention a lot. If you'll read the jakarta projects code you'll see the same This is why I posted this question, since I was curious to know if there was a certain consideration I wasn't aware of...

 
JDBC vendors are 3rd parties (or can be / usually are) ...
JDBC is just a good example of why interfaces work / are a good idea.

The SAP API (never personally used it) probably use interfaces because they can define an interface that the user 'uses' - which will never change. But if SAP want to change the implementation of their API - how it all works under the covers, they can do so without actually affecting how the user uses the API.

--------------------------------------------------
Free Database Connection Pooling Software
 
But they can do it as well if I am using the implements class no? I mean, what is the difference between using the interface method which uses the underline implementation than using the implemented class method which uses the same implementation? Or am I missing something here...?

 
Or do you mean that if in the future a different class will represent a different implementation of that Interface it will be transparent to my code and won't affect it...
 
But on the other hand it won't affect it even if I will use the implemented class...
 
OK.

Lets imagine you are writing a 3rd party API.
You have a class called "SomeInterface" which is an interface.
One of the methods in that class which must be implemented is "interfaceMethodA()".

Now the implentation class may call several methods to satisfy that interfaced method, lets say method A, B and C.

Now if you found a major bug, or wanted to change in some way how that interfceMethodA() actually was handled, having an interface means you could actually change the implementation code without ever affecting, or the user knowing, you had changed anything.

Its just basic OO design patterns.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top