Thursday, February 7, 2013

Abstract

Summary of Presentation:
Abstract classes are never instantiated, yet they can have constructors.
Good abstract example to go off of:

public class abstract Shape
{ private int x, y;
   public Shape(int xx, int yy)
       { x = xx;
          y = yy;
        }
  public abstract double getArea();

public class Circle extends Shape
{ private double radius;
   public Circle(int x, int y, double r)
        { super(x, y); radius = r}

  public double getArea
  { return Math.PI*r*r;}

  }

Overriding:

The word final signifies that a variable's value may never change.
Example:


public class abstract Shape
{ private int x, y;
   public Shape(int xx, int yy)
       { x = xx;
          y = yy;
        }
  public int getX()
  { return x;}
  public abstract double getArea();

public class Circle extends Shape
{ private double radius;
   public Circle(int x, int y, double r)
        { super(x, y); radius = r}
         public int getX()
         { return y;}

  public double getArea
  { return Math.PI*r*r;}

  }

This is bad because you have just overridden the getX() method. On the other hand, if you have a final before the method: public final int getX(), then you won't override the method.
If this is put into the code, the code won't compile because it can't override the method.

No comments:

Post a Comment