Abstract Class




An Abstract Class is a class that is marked as abstract and has at least one abstract method (which may come from an implemented interface). An abstract method is a method with no body (no code). An abstract class is used in designing. Several classes may be built (extended) from one abstract class, where the abstract class contains all of the common data elements and common methods. The common methods that can be written are written in the abstract class, so that we don't write them over and over again in the concrete classes that extend our abstract class. An abstract class can not be instantiated (you can't new it) (you can't create an object from it). Often times an abstract class implements an interface, and the abstract methods are defined in the interface.

Examples:


// the following example might be used to create a draw program
// it consists of an interface called ShapeInterface
// which holds all of the common methods among different shapes

// we use an abstract class called AbstractShapeObject where we
// define common variables and write some of the common methods

// we then have actual classes for a Rectangle, Ellipse, Line, etc.

// most of the methods in this example are left empty to conserve space
// the idea is to understand why abstract classes are important			  
			  			      
        ShapeInterface          define common constants and methods
               |				 
               |			   
      AbstractShapeObject       define common instance variables
    |         |       |    |    define and write constructors
    |         |       |    |    and write what we can
    |         |       |    |
Rectangle  Ellipse  Line  etc.  define any unique instance variables
                                define and write constructors
                                write any unwritten methods from above
                                define and write any unique methods

We could define:

ArrayList<AbstractShapeObject> list = new ArrayList<AbstractShapeObject>();


We could then create specific shape objects and add them to our list:

AbstractShapeObject myShape;

myShape = new Line(x1,y1,x2,y2,color);
myShape.setMode("Line");
list.add(myShape);
 
myShape = new Rectangle(x1,y1,x2,y2,color);
myShape.calcXYWidthHeight(x1,y1,x2,y2);
myShape.setMode("Rectangle");
list.add(myShape);

// etc.


We could draw specific shape objects:

for (int j=0; j < myList.size(); j++)
{
    AbstractShapeObject myShape = myList.get(j);
    // polymorphism in action
    // a general object acting like a specific object 
    myShape.draw(gMemory);
}
		  

								

import java.awt.*; public interface ShapeInterface { // all variables (constants) are public final by default public int defaultWidth = 1; public int defaultHeight = 1; public int defaultThickness = 1; public Color defaultColor = Color.RED; // these methods are all public abstract by default public int getX(); public void setX(int x); public int getY(); public void setY(int y); public int getWidth(); public void setWidth(int width); public int getHeight(); public void setHeight(int height); public int getThickness(); public void setThickness(int thickness); public Color getColor(); public void setColor(Color color); public String getMode(); public void setMode(String mode); public boolean getKeep(); public void setKeep(boolean keep); public abstract void draw(Graphics window); }
import java.awt.Color; public abstract class AbstractShapeObject implements ShapeInterface { private int x; private int y; private int width; private int height; private Color color; private int thickness; private String mode; // Am I a Rectangle, Line, etc. private boolean keep; // do I keep the shape or erase it later public AbstractShapeObject() { this(0,0,0,0,defaultThickness,defaultColor); } public AbstractShapeObject(int x, int y) { this(x,y,defaultWidth,defaultHeight,defaultThickness,defaultColor); } public AbstractShapeObject(int x, int y, int width, int height) { this(x,y,width,height,defaultThickness,defaultColor); } public AbstractShapeObject(int x, int y, int width, int height, Color color) { // call this } public AbstractShapeObject(int x, int y, int width, int height, int thickness) { // call this } public AbstractShapeObject(int x, int y, int width, int height, int thickness, Color color) { this.x = x; this.y = y; this.width = width; this.height = height; this.color = color; this.thickness = thickness; mode = "None"; keep = true; } // write all of your getter and setter methods here !!! // do NOT write your draw methods here // you will write your draw methods in the specific classes public int getX() { return 0; } public void setX(int x) { } public int getY() { return 0; } public void setY(int y) { } public int getWidth() { return 0; } public void setWidth(int width) { } public int getHeight() { return 0; } public void setHeight(int height) { } public int getThickness() { return 0; } public void setThickness(int thickness) { } public Color getColor() { return Color.red; } public void setColor(Color color) { } public String getMode() { return ""; } public void setMode(String mode) { } public boolean getKeep() { return true; } public void setKeep(boolean keep) { } public void calcXYWidthHeight(int x1, int y1, int x2, int y2) { int width = Math.abs(x2-x1); int height = Math.abs(y2-y1); // now I need to find my upper left corner point // find the x1 value if (x2 <= x1) { x1 = x2; } // find the y1 value if (y2 <= y1) // they have dragged the mouse up { y1 = y2; } // now set the instance variables of the class setX(x1); setY(y1); setWidth(width); setHeight(height); } }
import java.awt.*; import java.awt.geom.*; public class Rectangle extends AbstractShapeObject { public Rectangle() { super(); } public Rectangle(int x, int y, int width, int height) { super(x,y,width,height); } public Rectangle(int x, int y, int width, int height, Color color) { } public Rectangle(int x, int y, int width, int height, int thickness) { } public Rectangle(int x, int y, int width, int height, int thickness, Color color) { } public void draw(Graphics window) { // first, set your draw color // second, draw the shape } // each toString() method must use this format public String toString() { return getMode() + "," + getX() + "," + getY() + "," + getWidth() + "," + getHeight() + "," + getColor().getRed() + "," + getColor().getGreen() + "," + getColor().getBlue(); } }
import java.awt.*; import java.awt.geom.*; public class Ellipse extends AbstractShapeObject { public Ellipse(int x, int y, int width, int height) { super(x,y,width,height); } public Ellipse(int x, int y, int width, int height, Color color) { } public Ellipse(int x, int y, int width, int height, int thickness) { } public Ellipse(int x, int y, int width, int height, int thickness, Color color) { } public void draw(Graphics window) { // first, set your draw color // second, draw the shape } // write the toString() method // use the same format that is used for the Rectangle and Line classes }
import java.awt.*; import java.awt.geom.*; public class Line extends AbstractShapeObject { int x2; int y2; public Line(int x1, int y1, int x2, int y2) { super(x1,y1); this.x2 = x2; this.y2 = y2; } public Line(int x1, int y1, int x2, int y2, Color color) { } public Line(int x1, int y1, int x2, int y2, int thickness) { } public Line(int x1, int y1, int x2, int y2, int thickness, Color color) { } public int getX1() { return getX(); } public void setX1(int x) { setX(x); } public int getY1() { return getY(); } public void setY1(int y) { setY(y); } public int getX2() { return 0; } public void setX2(int x) { } public int getY2() { return 0; } public void setY2(int y2) { } public void draw(Graphics window) { // first, set your draw color // second, draw the shape } // each toString() method must use this format public String toString() { return getMode() + "," + getX1() + "," + getY1() + "," + getX2() + "," + getY2() + "," + getColor().getRed() + "," + getColor().getGreen() + "," + getColor().getBlue(); } }