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?
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();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Code to an interface , not to the implementationIn 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
ASKER