COP3252 Advanced Java Programming - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

COP3252 Advanced Java Programming

Description:

The main purpose of an abstract method is to be overridden in derived classes ... Suppose a method findArea() is called through one of the objects (s1, s2, s3) ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 16
Provided by: UoD
Category:

less

Transcript and Presenter's Notes

Title: COP3252 Advanced Java Programming


1
COP3252Advanced Java Programming
  • 08-Apr-08
  • Lecture Set 20
  • Abstract classes, Polymorphism, instanceof,
    interfaces

2
Abstract Classes
  • Superclasses are more general and subclasses are
    more specific.
  • Sometimes a base class is so general that it
    doesn't make sense to actually instantiate it
    (i.e. create an object from it).
  • Such a class is primarily a grouping place for
    common data and behaviors of subclasses -- an
    abstract class.
  • To make a class abstract, use the keyword
    abstract (which is a modifier)
  • public abstract class Shape
  • Now that Shape is abstract, this would be
    illegal
  • Shape s new Shape()
  • Specifically, it's new Shape() that is illegal

3
Abstract Methods
  • Methods can be abstract as well
  • An abstract method is a method signature without
    a definition
  • Abstract methods can only be created inside
    abstract classes
  • The main purpose of an abstract method is to be
    overridden in derived classes (with the same
    signature)
  • Example
  • public abstract class Shape // Shape is an
    abstract class
  • public abstract double findArea() // findArea
    is an abstract method
  • // other methods and data

4
Polymorphism
  • If a piece of code is designed to work with an
    object of type X, it will also work with an
    object of a class type that is derived from X
    (any subclass of X).
  • This is known as polymorphism
  • Implemented by the Java interpreter through a
    mechanism called dynamic binding

5
Polymorphism Examples
  • Suppose these is a base class called Shape
  • Suppose Rectangle, Triangle and Circle are all
    subclasses of Shape.
  • It is legal to attach derived objects to the base
    reference variables
  • Shape s1 new Circle()
  • Shape s2 new Rectangle()
  • Shape s3 new Triangle()

6
Polymorphism Example Cont
  • Suppose a method findArea() is called through one
    of the objects (s1, s2, s3) shown in the previous
    slide.
  • The method must exist in the Shape class, but can
    be overridden in the subclasses.
  • Through dynamic binding, the method that runs
    will be based on the attached objects type (one
    of Shapes subclasses) as a priority over the
    reference variable type (Shape)
  • s1.findArea() //runs findArea() method of Circle
  • s2.findArea() //runs findArea() method of
    Rectangle
  • s3.findArea() //runs findArea() method of
    Triangle
  • //If subclasses have no findArea method, it will
    run the findArea
  • //method of class Shape.

7
Polymorphism
  • If a method expects a parameter of type X, it is
    legal to pass in an object of a type derived from
    X (any subclass of X).
  • //Sample method
  • public int draw (Shape s)
  • //something .
  • //Sample calls
  • Shape s1 new Shape()
  • Shape s2 new Circle()
  • Shape s3 new Rectangle()
  • draw(s1) //normal usage
  • draw(s2) //passing in Circle object
  • draw(s3) //passing in Rectangle object

8
Polymorphism
  • Polymorphism is generally used to store many
    related items, but with slightly different types
    (subclasses of the same superclass) in one
    storage container (for example an array) and
    then do common operations on them through
    overridden functions.
  • Example Assume the setup in the previous
    example, base class Shape and derived classes
    Rectangle, Circle, Triangle.
  • Suppose the base class has a findArea() method
    (likely abstract, as we would not know how to
    compute the area for a generic shape), and each
    derived class has its own findArea() method.
  • Reminder Abstract method a method that does
    not provide an implementation. Subclasses of the
    class usually then overload the method and
    provide an implementation.

9
Polymorphism Example
  • // create an array of Shape reference variables
  • Shape list new Shapesize
  • list0 new Circle() // attach a Circle to
    first array slot
  • list1 new Rectangle() // attach a Rectangle
    to second slot
  • list2 new Triangle() // attach a Triangle to
    third slot
  • // .... continue in a like manner, attaching a
    variety of
  • // shapes to the array. Note that there could be
    MANY
  • // subcategories of shapes and MANY array
    elements.
  • //Notice that we are using the polymorphism
    feature
  • for (int i 0 i lt list.length i)
    System.out.println("The area of shape " i "
    " listi.findArea())

10
instanceof
  • Operator to check to see if the first operand (an
    object) is an instance of a second operand (a
    class) and returns a response of type boolean.
  • Example
  • Shape s1
  • Circle c1
  • if (s1 instance of Circle)
  • c1 (Circle)s1 //Cast to Circle

11
Interfaces
  • Java does not allow multiple inheritance
  • A subclass can only be derived from one base
    class with the keyword extends
  • In Java, the interface can obtain a similar
    effect to multiple inheritance
  • Interface - A construct that contains only
    constants and abstract methods
  • Similar to abstract class, but different since an
    abstract class can also contain regular variables
    and methods
  • Cannot instantiate (like an abstract class)

12
Interfaces Format for declaring
  • modifier interface Name
  • // constant declarations
  • // abstract method signatures
  • // keyword "abstract" not needed
  • // ALL methods in an interface are abstract

13
Interfaces implements
  • Keyword implements used to state that a class
    will use a certain interface.
  • In this example, Comparable is the interface.
  • Class ComparableCircle inherits data from the
    Comparable interface, and would then need to
    implement the methods (to be able to use them).
  • Class ComparableCircle extends Circle implements
    Comparable
  • // ....

14
Inheritance Other rules
  • Only single inheritance for classes, with extends
  • Interfaces can inherit other interfaces (even
    multiple), with extends
  • public interface NewInterface extends interface1,
    ..., interfaceN
  • classes can implement more than one interface
    with implements
  • public class NewClass extends BaseClass
    implements interface1, ..., interfaceN

15
Coming up
  • Thursday
  • JTables and JDBC ( Java Database connections)
  • Next Tuesday
  • Final set of lecture notes
  • Thursday
  • Review for final (Answers to midterm 1 and 2,
    sample questions, format of the final, etc)
Write a Comment
User Comments (0)
About PowerShow.com