File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Use Of Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use Of Interface" Watch "Use Of Interface" New topic

Use Of Interface

Biswomohan Pattanaik

Joined: Feb 22, 2008
Posts: 10
I have a doubt
//Interface A

I have a doubt,In class C I am accessing the B's add()through 2 referance variables one is class type and another is interface type.ok
My doubt is what's the differance?
which is adviceable to use?

[ November 02, 2008: Message edited by: Maneesh Godbole ]
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 415

You will not see any difference in the output because you have assigned same object to both the references.

Regarding which one is good to use is the the first one

that is A a = new B();

if you have 2 implemntation and at run time you have to decide that which one to be used then 1st is more appropriate.

Pawan Chopra
Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

When interface has reference to a object then only methods declared in the interface can be called.

Here reference to object is via Class name and all the public methods of Class B are accessible.

SCJP 5.0, SCWCD 5, preparing for SCDJWS
vipin raimcs

Joined: Sep 26, 2008
Posts: 9
A a = new B(); is advisable to use, this is because of business purpose. Also creation of objects is left on some factory classes. So client code is totally unaware of the fact that what they are using. They just only know if A a.
I agree. Here's the link:
subject: Use Of Interface
It's not a secret anymore!