Monthly Archives: January 2023

5.6 Interfaces An interface defines a contract for services that classes can implement. Objects of such classes guarantee that this contract will be honored. Before diving into interfaces, an overview of the inheritance relationship between classes can be useful. The extends clause in a class definition only allows linear inheritance between classes—that is, a subclass can only extend one superclass. A superclass reference can refer to objects of its own type and of its subclasses strictly according to the linear inheritance hierarchy. Note that this inheritance relationship between classes comprises both inheritance of type (i.e., a subclass inherits the type of its superclass and can act as such) and inheritance of implementation (i.e., a subclass inherits methods and fields from its superclass). Since this relationship is linear, it rules out multiple inheritance of implementation, in which a subclass can inherit implementation directly from more than one direct superclass. As we…

Read more

Hiding Static Methods Only instance methods in an object can be overridden. However, a static method in a subclass can hide a static method from the superclass. Hiding a static method is analogous to overriding an instance method except for one important aspect: Calls to static methods are bound at compile time as opposed to runtime for calls to overridden instance methods, and therefore do not exhibit polymorphic behavior exhibited by calls to overridden instance methods (p. 278). When hiding a static method, the compiler will flag an error if the signatures are the same, but the other requirements regarding the return type, throws clause, and accessibility are not met. If the signatures are different, the method name is overloaded, not hidden. A static method in the subclass can only hide a static method in the superclass. Analogous to an overridden instance method, a hidden superclass static method is not…

Read more

2/2