|
Object-Oriented
Topics covered include :
InterfacesJava has 'single inheritance' only. This means that a child class inherits from only one parent class. Mostly this is all you need. But sometimes multiple inheritance would be convenient. Interfaces give Java some of the advantages of multiple inheritance without the disadvantages. The chapter includes some small examples, but you will have to wait until you start programming graphical user interfaces to see interfaces used realistically. Interfaces are crucial to GUI programming. Object oriented programming makes software behave like "real world" objects. This makes programs easier to think about and more reliable. In the real world, you often think about an object in several different ways. You can think of your car as a vehicle or as an item of taxable property. It would be convenient if software objects, also, could be thought of in several ways. But a Java object belongs to just one class. In Java, an interface is used to express an aspect of a class other than what it inherits from its parent.
A child class that extends a parent class can also implement an interface to gain some additional behavior. Here is what an interface definition looks like:
A method declaration is simply an access modifier, return type, and method signature followed by a semicolon. This looks somewhat like a class definition. But no objects can be instantiated from it. However, instances can be constructed from a class that implements an interface. A class definition implements an interface like this:
A class always extends just one parent but may implement more than one interface. Here is an example interface definition:
The constants don't have to be separate from the methods, but doing so makes the interface easier to read. The key word 'final' indicates that the fields are constants - their values are 'final' - they cannot be modified. A method in an interface cannot be made private. A method in an interface is public by default. Hence, in the example, methodB is public even though it does not say so.
Variables cannot be put in an interface. Only constants and methods.
Implementing an InterfaceA class definition must always extend one parent, but it can implement zero or more interfaces:
Here is a class definition that implements three interfaces :
The body of the class definition is the same as always. However, because it implements an interface, the body must have a definition (implementation) of each of the methods from the interface. The class definition can use access modifiers as usual. Now BigClass must provide a method definition (implementation) for every method in each of the interfaces it declares that it implements. Note that any number of classes can implement the same interfaces. Here is another class definition:
You might think that class SmallClass does not extend a base class, but it does. If no other class is extended, Object is the base class. Thus SmallClass extends class Object and implements InterfaceA. Constants should be unique to each interface. However, it is ok if two interfaces require the same method. A class that implements both interfaces only needs to provide one complete method definition to satisfy both interfaces.
An Example Design Involving InterfacesAs an example, we will create a database program for a store. The store sells:
Of these goods, toys and books are taxable, but food is not. There are many other things that are taxable, such as services or entertainment so we need to have the concept "taxable" as a separate concept, not part of the concept of Goods. Here is what the concept Taxable looks like:
When implemented in Java, these concepts will appear as classes and one interface. Careful consideration of our Object Oriented Design suggests the following.
The child class Food extends its parent class Goods. It uses the keyword super to call the parent class's constructor and the parent's display method.
Here is the interface Taxable:
The Taxable interface looks like this:
The keyword final makes field taxRate a constant, not a variable (variables are not allowed in interfaces.) In fact, the keyword final can be omitted because the identifier that follows will automatically be a constant because it is declared in an interface; however, for clarity it is best practice to include it. The "= value" cannot be omitted - because taxRate is a constant, it must be set to a value. The method declaration (in the second line) is public by default, but once again it is best practice to make that absolutely clear by including the keyword 'public'.
The Other ClassesHere is a partial definition of Toy. Recall that it:
The constant taxRate is used in the calculateTax() method just as if it had been defined in the Toy class itself. Further, it can be used in methods of the class other than those listed in the interface.
The calculateTax() method must be made public. There is one remaining class in our example, Book, which looks like this:
Here is a small test program :
The calculateTax() method is only used with objects whose class implements the interface taxable. Here is a diagram that shows the classes and their objects : In the picture, clouds represent classes. Solid arrows connect child classes to parent classes. The dotted rectangle represents the interface; a dotted arrow shows which classes implement it. Rectangles represent objects. Arrows with a square head connect an object to its class. Here is a modified testing program that uses an array.
Since each child class is a (type of) Goods, an array of type Goods can be used to hold the instances of the child classes of class Goods.
Interface as a TypeAn interface can be used as a data type for a reference variable. Since Toy and Book implement Taxable, they can both be used with a reference variable of type Taxable:
The compiler has been told in the interface that all Taxable objects will have a calculateTax() method, so that method can be used with the variables.
Type CastsWhen you use a variable of type Taxable you are asking to use the "taxable" aspect of the object. Many different kinds of object might be referred to by the variable. In a larger program there may be Taxable classes that are not Goods. The compiler can only use the methods it knows that the object must havethose in the interface. However, you can use a typecast to tell the compiler that in a particular statement in the program the variable will refer to an object of a specific class. For example:
This program is not very sensibly written, because variable item1 is of type Book and everything would work without the need for the type cast. But in programs with more complicated logic such class casts are sometimes needed. A class cast (also called 'typecast') is necessary to tell the compiler that the variable tax in the following case contains a Book object:
Here is an alternative test program, with different type casts.
The first type cast must cast variable tax to type Goods (or an ancestor). The second must cast variable toy to Taxable or to a class that implements interface Taxable. Here is another possible way to achieve the same result :
Extending an interfaceAn interface can extend another interface - but not extend a class! For example :
A complex hierarchy of interfaces can be constructed using this feature.
|