html5-img
1 / 13

Abstract classes and interfaces

Abstract classes and interfaces. Review: inheritance. Inheritance is another way that OOP supports reuse Used when multiple classes have many things in common “Abstract” the common properties and methods into a reusable parent (or super) class.

nate
Télécharger la présentation

Abstract classes and interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Abstract classes and interfaces

  2. Review: inheritance • Inheritance is another way that OOP supports reuse • Used when multiple classes have many things in common • “Abstract” the common properties and methods into a reusable parent (or super) class. • Only those things that are distinct need to implemented individually

  3. Polymorphism • Polymorphism is the capability of a method to do different things based on the object that it is acting upon. • Overloaded methods • methods with the same name signature but either a different number of parameters or different types in the parameter list. • Overridden methods • methods that are inherited but redefined within a subclass. They have the same signature and the subclass definition is used.

  4. Shape xCenter, yCenter; color; Frame for drawing draw animate Circle Snowman Rectangle radiusOfHead radius width height

  5. Shape xCenter, yCenter; color; Frame for drawing Circle Snowman Rectangle radiusOfHead radius width height draw animate draw animate draw animate

  6. Abstract classes • Super classes contain needed functionality. • Sometimes, it makes sense to permit a super class to be instantiated into objects. • Often, however, it does not • What do the draw() and animate() methods of a “shape” do? • If we omit these methods from the Shape class, subclasses may not implement them. • If we create these methods for the Shape class (with empty bodies) these empty methods are inherited, with the same possible problem.

  7. Abstract classes • When we declare a class “abstract” we tell Java that • The class can’t be instantiated • There are required methods (also abstract) that must be implemented in any subclass of the class • This permits us to require functionality that may be different for each subclass of the super class.

  8. Shape xCenter, yCenter; color; Frame for drawing abstract draw abstract animate Circle Snowman Rectangle radiusOfHead radius width height draw animate draw animate draw animate

  9. public abstract class Shape { intxCenter; intyCenter; Color color; JFramemyFrame; Graphics g; public Shape(intinX, intinY, Color c, JFrame frame){ xCenter=inX; yCenter=inY; color=c; myFrame=frame; g=myFrame.getGraphics(); g.setColor(color); } public abstract void draw(); public abstract void animate(); public void setColor(Color colorIn){ color=colorIn; } protected void wasteTime(){ for(double n=0; n<999999999 n++); } }

  10. Interfaces • Some situations require that classes which solve similar problems have the same interface (set of function signatures) • There may not be any common properties or functionality to abstract into a super class • We can still enforce a common “look and feel” for these classes by creating an Interface class

  11. Interfaces • Example: • You might want a program objects that are animated. • Such objects should be able to draw, erase, and animate themselves. • If you define an Animation Interface, then all objects that “implement” this interface must implement these three methods • An Interface is similar to an abstract class with no properties and all abstract methods.

  12. Example public interface Animation { public void draw(); //this method is called to draw the object public void erase(); //this method is called to erase the object public void animate(); //this object is called to animate the object } public class Circle implements Animation{ properties… methods (must include draw(), erase(), animate()) … }

  13. Abstract classes vs Interfaces • Use abstract classes • When there are common properties • When there is common functionality (such as the wasteTime() method) that can be inherited • Use Interface classes • When there is no commonality BUT • You want to enforce a common set of methods (and their signatures) AND • You believe that you will be adding additional algorithms with similar requirements in the future.

More Related