Imagine you start creating a class and, while implementing or testing it, you find out that this particular class can be used instead as a general base that other classes can be derived from. An interface is a special class whose purpose is to serve as a template that actual classes can be based on. An interface is primarily created like a class: it has a name, a body and can have members. To create an interface, instead of the class keyword, you use the interface keyword. Here is an example: interface Dimension { } As done when creating classes, you can specify the access level of an interface. Here is an example: public interface Size { } As done for a class, the members of an interface are listed in its body. In an interface, you cannot declare fields like those we have used in other classes, except constants. This means that an interface mostly contains methods and usually only methods (you can also create constants). The most important rule of an interface is that, if you declare a method in it, you cannot define that method. In other words, you can provide the signature of a method (the void or return value, the name of the method, the parentheses, and the arguments, if any, followed by a semi-colon). Here is an example of an interface that has two methods: public interface DigitalCamera { String getMake(); String getModel(); } An interface is used to lay a foundation for other interfaces or other classes. For this reason, it is the prime candidate for class derivation. To derive a class from an interface, type the name of the class, followed by the implements keyword, followed by the name of the interface, and followed by the body of the new class. Here is an example of a class named PointAndShoot that implements an interface named DigitalCamera: public class PointAndShoot implements DigitalCamera { } When defining a class that implements an interface, you can add or not add one or more new methods, etc. Here is an example: interface DigitalCamera { String getMake(); void setMake(String make); String getModel(); void setModel(String model); double getMegapixels(); void setMegaPixels(double megapixels); double getPrice(); void setPrice(double value); } class PointAndShoot implements Size, DigitalCamera { void describe() { } } The primary rule of interfaces is t that, if you derive a class from an interface, you must implement all methods that were created in the interface. Once the class is ready, you can then use it as you see fit. Here is an example: interface DigitalCamera { String getMake(); void setMake(String make); String getModel(); void setModel(String model); double getMegapixels(); void setMegapixels(double megapixels); double getPrice(); void setPrice(double value); } class PointAndShoot implements DigitalCamera { String mk; String mdl; double mp; double prc; public PointAndShoot() { } public String getMake() { return mk; } public void setMake(String make) { mk = make; } public String getModel() { return mdl; } public void setModel(String model) { mdl = model; } public double getMegapixels() { return mp; } public void setMegapixels(double megapixels) { mp = megapixels; } public double getPrice() { return prc; } public void setPrice(double value) { prc = value; } public void describe() { System.out.println("Digital Camera"); System.out.printf("Make: %s\n", mk); System.out.printf("Model: %s\n", mdl); System.out.printf("Megapixels: %.2f\n", mp); System.out.printf("Price: %.2f", prc); } } This would produce: Digital Camera Make: General Electric Model: A830 Megapixels: 8.00 Price: 69.95
You can create an abstract that is based on an interface. To do this, precede the name of the new class with the abstract keyword. Here is an example: interface DigitalCamera { } abstract class StoreItem implements DigitalCamera { } We saw that if you create a class that implements an interface, you must define all methods declared in the interface. There is an exception to this rule. If you create an abstract class that implements an interface, since you will not implement the methods of the interface in the abstract class, you do not have to implement any method in the derived class. Here is an example: interface DigitalCamera { String getMake(); void setMake(String make); String getModel(); void setModel(String model); double getMegapixels(); void setMegapixels(double megapixels); } abstract class StoreItem implements DigitalCamera { String itemNumber; double getPrice(); void setPrice(double value); } As seen for abstract classes, if you want to use the new abstract class, you must derive a new class from it. When creating a class that is based on an abstract that itself is based on an interface, you must implement all methods of the interface and all methods of the abstract. Here is an example: interface DigitalCamera { String getMake(); void setMake(String make); String getModel(); void setModel(String model); double getMegapixels(); void setMegapixels(double megapixels); } abstract class StoreItem implements DigitalCamera { String itemNumber; abstract double getPrice(); abstract void setPrice(double value); } class DSLR extends StoreItem { String mk; String mdl; double mp; double prc; public DSLR() { } public String getMake() { return mk; } public void setMake(String make) { mk = make; } public String getModel() { return mdl; } public void setModel(String model) { mdl = model; } public double getMegapixels() { return mp; } public void setMegapixels(double megapixels) { mp = megapixels; } public double getPrice() { return prc; } public void setPrice(double value) { prc = value; } } Of course, in the new class, you can add new fields and/or methods if you judge them necessary. After creating the class, you can instantiate it and access its public or protected members. Here is an example: interface DigitalCamera { String getMake(); void setMake(String make); String getModel(); void setModel(String model); double getMegapixels(); void setMegapixels(double megapixels); } abstract class StoreItem implements DigitalCamera { String itemNumber; abstract double getPrice(); abstract void setPrice(double value); } class DSLR extends StoreItem { private String mk; private String mdl; private double mp; private double prc; public DSLR() { } public String getMake() { return mk; } public void setMake(String make) { mk = make; } public String getModel() { return mdl; } public void setModel(String model) { mdl = model; } public double getMegapixels() { return mp; } public void setMegapixels(double megapixels) { mp = megapixels; } public double getPrice() { return prc; } public void setPrice(double value) { prc = value; } public String maximumResolution; public void describe() { System.out.println("Digital Camera"); System.out.printf("Item #: %s\n", itemNumber); System.out.printf("Make: %s\n", mk); System.out.printf("Model: %s\n", mdl); System.out.printf("Megapixels: %.2f\n", mp); System.out.printf("Max Res.: %s\n", maximumResolution); System.out.printf("Price: %.2f", prc); } } public class Exercise { public static void main(String[] args) { DSLR camera = new DSLR(); camera.itemNumber = "C29084-16"; camera.setMake("Canon"); camera.setModel("EOS 450D"); camera.setMegapixels(12.2); camera.maximumResolution = "4272 x 2848"; camera.setPrice(798.95); camera.describe(); } } This would produce: Digital Camera Item #: C29084-16 Make: Canon Model: EOS 450D Megapixels: 12.20 Max Res.: 4272 x 2848 Price: 798.95
Just as you can derive a class from an interface, you can create an interface that itself is based on another interface. To create such an interface, use the following formula: modifier interface InterfaceName extends InterfaceName { } As you can see from this formula, you use the extends keyword instead of implements when creating an interface that extends an existing interface. Here is an example: interface Size { } interface DigitalCamera { } interface Camcorder extends Size { } The Java language does not allow multiple inheritance, which is the ability to create a class based on more than one class (allowed in C++). Multiple inheritance is allowed only if the bases are interfaces. To create a multiple inheritance, separate the names of interfaces with a comma. Here is an example: interface Size { } interface DigitalCamera { } class PointAndShoot implements Size, DigitalCamera { } In the previous lesson, we learned how to derive one class from another, using the extends keyword. Here is an example we use: public class Sphere extends Circle { } In the same way, you can create a class that is based on another class and implements an interface. To create the class, start with the formula of deriving the class, then, use the formula of implementing an interface. |
|
|||||||||||||
|