I have a question about interface. For example I have Interface A with two functions: getName() and getAddress(). Than interface B with function getSalary(). Interface B is instance of interface A. Than class Professor that is subclass or instance of interface B. I did the UML diagram on TogetherJ 4.02. My question is does is legal for class C implement the functions from interface A ((getName() or getAddress()). Thanks a lot
Joined: Jan 07, 2002
First of all, an interface does not contain any functions. It merely declares what methods a class which implements it must provide. This is an important concept. Interfaces are just about describing behavior, not actually implementing that behavior. Interfaces are similar to classes in some ways, but very different in others. Interfaces cannot be instantiated, so there is no concept of an interface "subclassing" another one. However, there is a similar concept : extending declarations. So an interface can extend another interface, but not subclass it. In your example, we say interface B extends interface A. Finally, if class Professor implements interface B it is mandatory for class Professor to provide declarations for every method that interface B delclares (if you intend to instantiate it.) Since B extends A, B inherits those declarations from A, so B contains all it's own method delcaration, PLUS A's. Thus Professor is obligated to provide implementation for all of the declared methods in both A and B, otherwise you must declare it to be abstract.