| Interface Inheritance | |||||||||||
| Intro to Interface Inheritance | Examples of Interface Inheritance | ||||||||||
| Useful Interface Inheritance | |||||||||||
| Home | |||||||||||
| Some useful interfaces found in the Java API:
o List - implemented by ArrayList o Runnable - implemented by Applets for threading purposes o ActionListener - implemented by GUI event-handlers An abstract class in Java is similar to an interface in that it cannot be instantiated and provides a set of methods that an inheriting class can implement. Methods in an abstract class declared as abstract must be implemented by an inheriting class, much like any method declared in an interface. Because abstract methods contain no implementation, a class that contains an abstract method is considered incomplete, and can therefore not be instantiated. In other words, any class containing an abstract method becomes abstract itself, and must be declared so. Even if an abstract class contains no abstract methods, it still cannot be instantiated. Similarly, any subclass that does not implement all abstract methods is also abstract. Remember that an interface is basically an abstract class whose instance variables are public, static and final, and whose methods are declared public and abstract. An abstract class has all of the inherent properties of a class, and can therefore define private and protected methods, as well as static methods. Unfortunately, inheriting multiple classes in Java is impermissible. In general, an abstract class is used over an interface if certain class methods require a default implementation. This lends itself to the fact that abstract classes are used most often to define a set of common states and behaviors so that inheriting classes are a type of that abstract class. For example, a Pitbull class that implements an abstract Dog class makes sense because a pitbull is a dog. Interfaces, on the other hand, tend to describe the abilities of an inheriting class, such as a Tshirt class that implements a Washeable interface. An unrelated concrete class, such as Bedsheet, can also implement the Washeable interface because it too can be washed, even though is not a type of clothing. |
|||||||||||