Abstract classes and interfaces
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Abstract classes and interfaces PowerPoint PPT Presentation


  • 59 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Abstract classes and interfaces

Abstract classes and interfaces


Review inheritance

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

  • 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.


Abstract classes and interfaces

Shape

xCenter, yCenter;

color;

Frame for drawing

draw

animate

Circle

Snowman

Rectangle

radiusOfHead

radius

width

height


Abstract classes and interfaces

Shape

xCenter, yCenter;

color;

Frame for drawing

Circle

Snowman

Rectangle

radiusOfHead

radius

width

height

draw

animate

draw

animate

draw

animate


Abstract classes

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 classes1

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.


Abstract classes and interfaces

Shape

xCenter, yCenter;

color;

Frame for drawing

abstract draw

abstract animate

Circle

Snowman

Rectangle

radiusOfHead

radius

width

height

draw

animate

draw

animate

draw

animate


Abstract classes and interfaces

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

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


Interfaces1

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

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

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.


  • Login