Docstoc

lec5

Document Sample
lec5 Powered By Docstoc
					                                                                                                                                     The abstract Modifier
                                                                                                                    • The abstract class
                                                                                                                      – Cannot be instantiated
                                                                                                                      – Should be extended and implemented in
                                      Lecture 5                                                                         subclasses

                                                                                                                    • The abstract method
                                                                                                                      – Method signature without
           Abstract Classes & Interfaces                                                                                implementation
                                                                                                                                                                                                              Hide details



                                                                                                         1                                                                                                                      2




          GeometricObject
  -color: String
  -filled: boolean
                                      The color of the object (default: white).
                                      Indicates whether the object is filled with a color (default: false).
                                                                                                                                                    Abstract Classes
                                                                                                                                                  GeometricObject
  -dateCreated: java.util.Date        The date when the object was created.                                                               -color: String
  +GeometricObject()                  Creates a GeometricObject.                                                                          -filled: boolean
  +getColor(): String                 Returns the color.                                                                                  -dateCreated: java.util.Date
                                                                                                              The # sign indicates
  +setColor(color: String): void      Sets a new color.                                                       protected modifer           #GeometricObject()
                                                                                                                                          +getColor(): String
  +isFilled(): boolean                Returns the filled property.
                                                                                                                                          +setColor(color: String): void
  +setFilled(filled: boolean): void   Sets a new filled property.                                                                         +isFilled(): boolean
  +getDateCreated(): java.util.Date   Returns the dateCreated.                                                                            +setFilled(filled: boolean): void
  +toString(): String                 Returns a string representation of this object.                                                     +getDateCreated(): java.util.Date
                                                                                                                                          +toString(): String
                                                                                                                                          +getArea(): double
                                                                                                                                          +getPerimeter(): double
                    Circle                                           Rectangle
                                                                                                                                                                                                              GeometricObject
  -radius: double                                  -width: double
  +Circle()                                        -height: double
                                                                                                                              Circle                                                Rectangle
  +Circle(radius: double)                          +Rectangle()                                                -radius: double                                    -width: double                               Circle
  +getRadius(): double                             +Rectangle(width: double, height: double)                   +Circle()                                          -height: double

  +setRadius(radius: double): void                 +getWidth(): double                                         +Circle(radius: double)                            +Rectangle()

  +getArea(): double                               +setWidth(width: double): void                              +getRadius(): double                               +Rectangle(width: double, height: double)   Rectangle
                                                                                                               +setRadius(radius: double): void                   +getWidth(): double
  +getPerimeter(): double                          +getHeight(): double
                                                                                                               +getDiameter(): double                             +setWidth(width: double): void
  +getDiameter(): double                           +setHeight(height: double): void                                                                               +getHeight(): double
                                                   +getArea(): double                                                                                             +setHeight(height: double): void
                                                                                                         3                                                                                                                      4
                                                   +getPerimeter(): double




                                      NOTE                                                                                                                               NOTE
An abstract method cannot be contained in                                                                        An abstract class cannot be instantiated
a nonabstract class. If a subclass of an                                                                         using the new operator, but you can still
abstract superclass does not implement all                                                                       define its constructors, which are invoked
the abstract methods, the subclass must be                                                                       in the constructors of its subclasses.
declared abstract. In other words, if a
                                                                                                                 For instance, the constructors of
nonabstract subclass extended from an
                                                                                                                 GeometricObject are invoked in the Circle
abstract class, all the abstract methods
                                                                                                                 class and the Rectangle class.
must be implemented, even if they are not
used in the subclass.
                                                                                                         5                                                                                                                      6
                      NOTE                                             Example: Using the
 A class that contains abstract methods                              GeometricObject Class
 must be abstract. However, it is possible to              • Objective: This example creates two
 declare an abstract class that contains no                  geometric objects: a circle, and a rectangle,
 abstract methods. In this case, you cannot                  invokes the equalArea method to check if
 create instances of the class using the new                 the two objects have equal area, and
 operator. This class is used as a base class                invokes the displayGeometricObject
 for defining a new subclass.                                method to display the two objects.

                                                                              TestGeometricObject
                                                       7                                                   8




                   Interfaces                                  Interface is a Special Class
An interface is a classlike construct that contains only
constants and abstract methods. In many ways, an           An interface is treated like a special class in
interface is similar to an abstract class, but an          Java. Each interface is compiled into a
abstract class can contain variables and concrete          separate bytecode file, just like a regular class.
methods as well as constants and abstract methods.         Like an abstract class, you cannot create an
                                                           instance from an interface using the new
To distinguish an interface from a class, Java uses
the following syntax to declare an interface:
                                                           operator, but in most cases you can use an
                                                           interface more or less the same way you use
public interface InterfaceName {                           an abstract class. For example, you can use
  constant declarations;
  method signatures;
                                                           an interface as a data type for a variable, as
}                                                          the result of casting, and so on.
                                                       9                                                  10




             Define Interfaces                                    Example of an Interface
Suppose you want to design a generic method to find
the larger of two objects. The objects can be
students, dates, or circles. Since compare methods         // This interface is defined in
are different for different types of objects, you need
to define a generic compare method to determine            // java.lang package
the order of the two objects. Then you can tailor the      package java.lang;
method to compare students, dates, or circles.
For example, you can use student ID as the key for
comparing students, radius as the key for comparing        public interface Comparable {
circles, and volume as the key for comparing dates.          public int compareTo(Object o);
You can use an interface to define a generic               }
compareTo method, as follows:

                                                      11                                                  12
               String and Date Classes                                                                            Generic max Method
 Many classes (e.g., String and Date) in the Java                                              // Max.java: Find a maximum object
                                                                                               public class Max {
 library implement Comparable to define a natural                                                /** Return the maximum of two objects */
 order for the objects. If you examine the source code                                           public static Comparable max
 of these classes, you will see the keyword                                                          (Comparable o1, Comparable o2) {
                                                                                                   if (o1.compareTo(o2) > 0)
 implements used in the classes, as shown below:                                                     return o1;
                                                                                                   else
 public class String extends Object                   public class Date extends Object               return o2;} }
     implements Comparable {                              implements Comparable {                                 (a)                                                  (b)
   // class body omitted                                // class body omitted                  String s1 = "abcdef";                            Date d1 = new Date();
 }                                                    }                                        String s2 = "abcdee";                            Date d2 = new Date();
                                                                                               String s3 = (String)Max.max(s1, s2);             Date d3 = (Date)Max.max(d1, d2);

new   String() instanceof String
new   String() instanceof Comparable
                                                                                               The return value from the max method is of the
new   java.util.Date() instanceof java.util.Date
                                                                                               Comparable type. So, you need to cast it to String or
new   java.util.Date() instanceof Comparable                                                   Date explicitly.
                                                                                          13                                                       14




         Declaring Classes to Implement                                                                    Interfaces vs. Abstract
                  Comparable                                                                                             Classes
  Notation:                            GeometricObject                  «interface»             In an interface, the data must be constants; an abstract class
  The interface name and the       -                               java.lang.Comparable         can have all types of data.
  method names are italicized.
  The dashed lines and hollow                                +compareTo(o: Object): int
  triangles are used to point to
                                          Rectangle                                             Each method in an interface has only a signature without
                                   -
  the interface.                                                                                implementation; an abstract class can have concrete methods.
                                    ComparableRectangle
                                   -
                                                                                                             Variables         Constructors                             Methods
                                                                     ComparableRectangle
 You cannot use the max method to find the larger of two instances of
                                                                                                  Abstract   No restrictions   Constructors are invoked by              No restrictions.
 Rectangle, because Rectangle does not implement Comparable. However,                             class                        subclasses through constructor
 you can declare a new rectangle class that implements Comparable. The                                                         chaining. An abstract class cannot be
 instances of this new class are comparable. Let this new class be named                                                       instantiated using the new operator.
 ComparableRectangle.                                                                             Interface All variables      No constructors. An interface cannot     All methods must
   ComparableRectangle rectangle1 = new ComparableRectangle(4, 5);
                                                                                                            must be public     be instantiated using the new            be public abstract
   ComparableRectangle rectangle2 = new ComparableRectangle(3, 6);                                          static final       operator.                                instance methods
   System.out.println(Max.max(rectangle1, rectangle2));
                                                                                          15                                                                                               16




      Interfaces vs. Abstract Classes,                                                             Interfaces vs. Abstract Classes,
                    cont.                                                                                                         cont.
                                                                                               All classes share a single root, the Object class, but there is no single root
 All data fields are public final static and all methods are public                            for interfaces. Like a class, an interface also defines a type. A variable of an
 abstract in an interface. For this reason, these modifiers can be                             interface type can reference any instance of the class that implements the
 omitted, as shown below:                                                                      interface. If a class extends an interface, this interface plays the same role
                                                                                               as a superclass. You can use an interface as a data type and cast a
        pbi itraeT {
         ulc nefc 1                                           ulc nefc 1
                                                              pbi itraeT {                     variable of an interface type to its subclass, and vice versa.
          p b i s a i f n l i t K = 1 Equivalent
           ulc ttc ia n              ;                          n
                                                               itK=1;
                                                                                                        Interface1_2                               Interface2_2

            pbi asrc vi p)
             ulc btat od (;                                        od (;
                                                                  vi p)
        }                                                     }                                         Interface1_1           Interface1          Interface2_1


  A constant defined in an interface can be accessed using
  syntax InterfaceName.CONSTANT_NAME (e.g., T1.K).                                                       Object                 Class1                                       Class2


                                                                                               Suppose that c is an instance of Class2. c is also an instance of Object,
                                                                                               Class1, Interface1, Interface1_1, Interface1_2, Interface2_1, and
                                                                                          17   Interface2_2.                                                             18
    Whether to use an interface or a
                class?
In general, a strong is-a relationship that clearly describes a
parent-child relationship should be modeled using classes.
For example, a staff member is a person. So their relationship
should be modeled using class inheritance. A weak is-a
relationship, also known as an is-kind-of relationship,
indicates that an object possesses a certain property. A weak
is-a relationship can be modeled using interfaces. For
example, all strings are comparable, so the String class
implements the Comparable interface.
• You can also use interfaces to circumvent single inheritance
restriction if multiple inheritance is desired. In the case of
multiple inheritance, you have to design one as a superclass,
and others as interface.

                                                            19

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:0
posted:3/15/2012
language:
pages:4
Description: java programming language lecture