![]() ![]() Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. Implementing an interface allows a class to become more formal about the behavior it promises to provide. changeCadence, changeGear, speedUp, and applyBrakes GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways.// The compiler will now require that methods You can take advantage of the similarities and declare all the graphic objects to inherit from the same abstract parent object (for example, GraphicObject) as shown inĬlasses Rectangle, Line, Bezier, and Circle Inherit from GraphicObjectįirst, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. This is a perfect situation for an abstract superclass. All GraphicObjects must be able to draw or resize themselves they just differ in how they do it. Others require different implementations (for example, resize or draw). Some of these states and behaviors are the same for all graphic objects (for example: position, fill color, and moveTo). These objects all have certain states (for example: position, orientation, line color, fill color) and behaviors (for example: moveTo, rotate, resize, draw) in common. In an object-oriented drawing application, you can draw circles, rectangles, lines, Bezier curves, and many other graphic objects. Note that many software libraries use both abstract classes and interfaces the HashMap class implements several interfaces and also extends the abstract class AbstractMap. In addition, the Map interface has been enhanced with many default methods such as merge and forEach that older classes that have implemented this interface do not have to define. Serializable Objects), and has the functionality of a map. By reading this list of interfaces, you can infer that an instance of HashMap (regardless of the developer or company who implemented the class) can be cloned, is serializable (which means that it can be converted into a byte stream HashMap, which implements the interfaces Serializable, Cloneable, and Map. Its subclasses (which include HashMap, TreeMap, and ConcurrentHashMap) share many methods (including get, put, isEmpty, containsKey, and containsValue) that AbstractMap defines.Īn example of a class in the JDK that implements several interfaces is You want to take advantage of multiple inheritance of type.Īn example of an abstract class in the JDK isĪbstractMap, which is part of the Collections Framework.You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.For example, the interfacesĬloneable are implemented by many unrelated classes. You expect that unrelated classes would implement your interface.Consider using interfaces if any of these statements apply to your situation:.This enables you to define methods that can access and modify the state of the object to which they belong. You want to declare non-static or non-final fields.You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).You want to share code among several closely related classes.Consider using abstract classes if any of these statements apply to your situation:.Which should you use, abstract classes or interfaces? In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. (It can be used, but it is unnecessary.)Ībstract classes are similar to interfaces. ![]() Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |