Re: Constructor vs Abstract
What makes an interface special as a type is that it has no implementation. Classes (whether abstract or not) do have implementation.
The implementation of a class lies in its instance variables and methods. All classes have constructors - it may be that a class has only the default constructor, but it always has a constructor. The job of a constructor is to make sure that the state of an instance is properly set up with all of its instance variables properly initialised.
Note that all three sentences in the previous paragraph apply to abstract classes just as they do to non abstract classes. And that the lack of constructors for interfaces is a reflection of the fact that interfaces do not have state or any implementation.
-----
You can use the "new" keyword with interfaces and abstract classes provided you follow the class with a block that provides the implementation:
Code:
MouseListener mouseListener = new MouseListener() {
@Override
public void mouseMoved(MouseEvent me) {}
// etc for the other methods that a MouseListener
// must have
};
Re: Constructor vs Abstract
Thanks a ton!!! tht was hugely helpful.
Re: Constructor vs Abstract