Spring Note 3

UserDao userdao = new UserDaoImpl();

UserDao is an interface class, UserDaoImpl is its implementation class. This code can also be replaced by UserDaoImpl userdaoimpl = new UserDaoImpl(), but why we don’t do so?

This is about the polymorphism. Knowing an interface can be implemented many times, imaging another implementation is provided and will be used, we name it UserDaoMySQLImpl. If I want to call this class in service layer, I have to rewrite as userDaoMySQLImpl userdaomysqlimpl = new UserDaoMySQLImpl(). If I use the code UserDao userdao = new UserDaoImpl(), I only have to change the new UserDaoImpl() to new UserDaoMySQLImpl().

After comparing these two codes, I can easily find the use of interface and can feel the sense of polymorphism.