130 likes | 311 Views
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.
E N D
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
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.
Shape xCenter, yCenter; color; Frame for drawing draw animate Circle Snowman Rectangle radiusOfHead radius width height
Shape xCenter, yCenter; color; Frame for drawing Circle Snowman Rectangle radiusOfHead radius width height draw animate draw animate draw animate
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.
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.
Shape xCenter, yCenter; color; Frame for drawing abstract draw abstract animate Circle Snowman Rectangle radiusOfHead radius width height draw animate draw animate draw animate
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++); } }
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
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.
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()) … }
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.