Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces 2015-12-211. About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation. 2015-12-212.

Similar presentations


Presentation on theme: "Interfaces 2015-12-211. About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation. 2015-12-212."— Presentation transcript:

1 Interfaces 2015-12-211

2 About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation. 2015-12-212

3 Abstract classes and methods The intent of base class is to create a common interface for all the classes derived from it. The only reason to establish this common interface is so that it can be expressed differently for each different subtype. It establishes a basic form, so that you can say what’s common for all the derived classes. Another way of saying this is to call the base class an abstract base class, or simply an abstract class. 2015-12-213

4 If you have an abstract class, objects of that specific class almost always have no meaning. You create an abstract class when you want to manipulate a set of classes through its common interface. Thus, abstract class is meant to express only the interface, and not a particular implementation, so creating an object of that class makes no sense, and you’ll probably want to prevent the user from doing it. 2015-12-214

5 Java provides a mechanism for doing this called the abstract method. This is a method that is incomplete; it has only a declaration and no method body. Here is the syntax for an abstract method declaration. 2015-12-215

6 A class containing abstract methods is called an abstract class. If a class contains one or more abstract methods, the class itself must be qualified as abstract. 2015-12-216

7 If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword. 2015-12-217

8 It’s possible to make a class abstract without including any abstract methods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstract methods, and yet you want to prevent any instances of that class 2015-12-218

9 Interfaces The interface keyword takes the concept of abstractness one step further. The abstract keyword allows you to create one or more undefined methods in a class—you provide part of the interface without providing a corresponding implementation. The implementation is provided by inheritors. 2015-12-219

10 The interface keyword produces a completely abstract class, one that provides no implementation at all. It allows the creator to determine method names, argument lists, and return types, but no method bodies. An interface provides only a form, but no implementation. 2015-12-2110

11 Interface Syntax To create an interface, use the interface keyword instead of the class keyword. As with a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name). If you leave off the public keyword, you get package access, so the interface is only usable within the same package. An interface can also contain fields, but these are implicitly static and final. 2015-12-2111

12 To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword, which says, "The interface is what it looks like, but now I’m going to say how it works." Other than that, it looks like inheritance. 2015-12-2112

13 Example 2015-12-2113

14 You can see from the Woodwind and Brass classes that once you’ve implemented an interface, that implementation becomes an ordinary class that can be extended in the regular way. You can choose to explicitly declare the methods in an interface as public, but they are public even if you don’t say it. So when you implement an interface, the methods from the interface must be defined as public. Otherwise, they would default to package access, and you’d be reducing the accessibility of a method during inheritance, which is not allowed by the Java compiler. 2015-12-2114

15 Complete decoupling Whenever a method works with a class instead of an interface, you are limited to using that class or its subclasses. If you would like to apply the method to a class that isn’t in that hierarchy, you’re out of luck. 2015-12-2115

16 An interface relaxes this constraint considerably. As a result, it allows you to write more reusable code. Decoupling interface from implementation allows an interface to be applied to multiple different implementations, and thus your code is more reusable. 2015-12-2116

17 “Multiple inheritance” in Java Because an interface has no implementation at all—that is, there is no storage associated with an interface—there’s nothing to prevent many interfaces from being combined. This is valuable because there are times when you need to say, "An x is an a and a b and a c." 2015-12-2117

18 2015-12-2118

19 In a derived class, you aren’t forced to have a base class that is either abstract or "concrete“ (one with no abstract methods). If you do inherit from a non-interface, you can inherit from only one. All the rest of the base elements must be interfaces. 2015-12-2119

20 Extending an interface with inheritance You can easily add new method declarations to an interface by using inheritance, and you can also combine several interfaces into a new interface with inheritance. In both cases you get a new interface. 2015-12-2120

21 The syntax works only when inheriting interfaces. Normally, you can useextends with only a single class, but extends can refer to multiple base interfaces when building a new interface. As you can see, the interface names are simply separated with commas. 2015-12-2121

22 Name collisions when combining Interfaces You can encounter a small pitfall when implementing multiple interfaces. Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well. Strive to avoid it 2015-12-2122

23 Adapting to an interface One of the most compelling reasons for interfaces is to allow multiple implementations for the same interface. In simple cases this is in the form of a method that accepts an interface, leaving it up to you to implement that interface and pass your object to the method. 2015-12-2123

24 A common use for interfaces is the aforementioned Strategy design pattern. You write a method that performs certain operations, and that method takes an interface that you also specify. 2015-12-2124

25 Fields in interfaces Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values. Before Java SE5, this was the only way to produce the same effect as an enum in C or C++. With Java SE5, you now have the much more powerful and flexible enum keyword, so it rarely makes sense to use interfaces for constants anymore. 2015-12-2125

26 Initializing fields in interfaces Fields defined in interfaces cannot be "blank finals," but they can be initialized with non-constant expressions. The fields, of course, are not part of the interface. The values are stored in the static storage area for that interface. 2015-12-2126

27 Nesting interfaces Interfaces may be nested within classes and within other interfaces. This reveals a number of interesting features. The syntax for nesting an interface within a class is reasonably obvious. Just like non- nested interfaces, these can have public or package-access visibility. Nestinglnterfaces shows the various ways that nested interfaces can be implemented. In particular, notice that when you implement an interface, you are not required to implement any interfaces nested within. 2015-12-2127

28 Interfaces and factories An interface is intended to be a gateway to multiple implementations, and a typical way to produce objects that fit the interface is the Factory Method design pattern. 2015-12-2128

29 Summary It is tempting to decide that interfaces are good, and therefore you should always choose interfaces over concrete classes. Of course, almost anytime you create a class, you could instead create an interface and a factory. However, any abstraction should be motivated by a real need.. Interfaces should be something you refactor to when necessary, rather than installing the extra level of indirection everywhere, along with the extra complexity. An appropriate guideline is to prefer classes to interfaces. 2015-12-2129


Download ppt "Interfaces 2015-12-211. About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation. 2015-12-212."

Similar presentations


Ads by Google