Abstract class as just described are powerful ways to define how new classes are constructed. Sometimes,however,you don't want to fully define a class. Instead you need to specify that classes must implement a certain set of methods. This way you can have vastly different classes created,but all define these methods and therefore can operate the same.
The can be done by defining an interface. Interfaces are made by using the interfacekeyword instead of class and specifying public methods,just as you would have for an abstract class. Other classes can now use the implementskeyword instead of extends and state that they are going to implement all methods that the interface specified.
The main advantage of an interface is that it allows you to define a protocol to be implemented for an object to have some behavior.For example,you could have a Comparable interface with a compare method for classes to implement,and every class that implements it would have a standardized method for comparison.
No comments:
Post a Comment