Declaring variable using class vs. interface

If MyClass implements MyInterface, then what is the difference between declaring a variable:
  private MyInterface x;
  //vs.
  private MyClass x;

Open in new window


For example:
public interface MyInterface {
  ...
}

public class MyClass implements MyInterface {
  ...
}

public class AnotherClass {

  private MyInterface x;
  //vs.
  private MyClass x;

}

Open in new window

deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
If you declre variable as interface - you can only use the methods defined by the interface. Compler does not know that itis an instance of the class and it will complain if you use methods or variables of the class which are not part of the interface
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
On the other hand sometimes you may have vraibales of different classes implementing one and the same interface and it may be only important for you in certain program that these variables implement the methods of interface. In suh situation it may be handy to declare variabkle as of interface type rather than as class type
0
for_yanCommented:
Interface and Class are tow ways of specifying type of the variables.
Each of these ways is useful in specific situation in the program
0
for_yanCommented:

read this trail fro more details and uses of different ways of specifying type ov varaibles in java
http://stackoverflow.com/questions/3383726/java-declaring-from-interface-type-instead-of-class
0
gordon_vt02Commented:
It is generally considered better practice to declare your variables using the Interface they implement rather than the concrete class -- unless you rely on methods of a particular implementation.  When exposing public methods of your class, you should almost always accept Interface parameters and return an Interface so you, and users of that method, are free to change the actual implementation without breaking the API and any code using it.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.