Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

interface class instance or implementation class instance?

Hi,

When you want to create an instance of implementation class, do you assign it to the interface class or the implementation class?
Can't remember reasons why you do this way.
Can you explain or point to the web site?

What's the right way to create the instance of impletation class?


interface BankDao {

}

class BankDaoImpl {

}


clas TestBank{

//1)
BankDao bankDao = new BankDaoImpl();

//2)
BankDaoImpl bankDao = new BankDaoImpl();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dkim18
dkim18

ASKER

Thanks. Now it is coming back to me.
Code to an interface , not to the implementation
In that well-known quote, the term "interface" does not necessarily mean a Java interface. It can also mean an abstract class.
Right , interface in that quote means a higher level class . So you should always code to the highest level class possible , not to a specific subclass