Lecture 12

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

11/14/2016

Abstract Classes and Abstract Methods


GeometricObject Abstract class

-color: String
-filled: boolean

Lecture 12 The # sign indicates


protected modifie r
-dateCreated: java.util.Date

#Geo metric Object()


#Geo metric Object(color: string,

Abstract Classes filled: boolean)


+getColor(): St ring
+setColor(colo r: String): void
+isFilled(): boolean
+setFilled(filled : boolean): void
+getDateCreated(): java.util.Date
+toString(): String
+getArea(): double
Abstract methods +getPerimeter(): double
Methods getArea and getPerimeter a re overridden in
are ita lic ized
Circ le and Rectangle. Superclass methods are generally
omitted in the UM L d iagra m for subclasses .

Circle Rectangle
-radius: double -width: double
-height: double
+Circle ()
+Circle (radius: double) +Rectangle()

+Circle (radius: double, color: string, +Rectangle(width: double, height: double)


filled: boolean) +Rectangle(width: double, height: double,
+getRadius(): double color: string, filled: boolean)

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

+getDia meter(): double +setWidth(width: double): void


+getHeight(): double
+setHeight(height: double): void

1 2

abstract method in abstract class object cannot be created from abstract


class
• An abstract method cannot be contained in a non-
abstract class. • An abstract class cannot be instantiated
• If a subclass of an abstract superclass does not using the new operator, but you can still
implement all the abstract methods, the subclass define its constructors, which are invoked
must be defined abstract. in the constructors of its subclasses.
• In other words, in a non-abstract subclass • For instance, the constructors of
extended from an abstract class, all the abstract
methods must be implemented, even if they are GeometricObject are invoked in the
not used in the subclass. Circle class and the Rectangle class.

3 4

1
11/14/2016

superclass of abstract class may be


abstract class without abstract method
concrete
• A class that contains abstract methods must be
abstract. • A subclass can be abstract even if its
superclass is concrete.
• However, it is possible to define an abstract
class that contains no abstract methods. • For example, the Object class is concrete,
• In this case, you cannot create instances of the but its subclasses, such as
class using the new operator. This class is used GeometricObject, may be abstract.
as a base class for defining a new subclass.

5 6

concrete method overridden to be abstract class as type


abstract
• You cannot create an instance from an
A subclass can override a method from its abstract class using the new operator, but
superclass to define it abstract. This is rare, an abstract class can be used as a data type.
but useful when the implementation of the
method in the superclass becomes invalid in • Therefore, the following statement, which
the subclass. In this case, the subclass must be creates an array whose elements are of
defined as abstract. GeometricObject type, is correct.
GeometricObject[] geo = new GeometricObject[10];

7 8

2
11/14/2016

The Abstract Calendar Class and Its The Abstract Calendar Class and Its
GregorianCalendar subclass GregorianCalendar subclass
java.util.Calendar • An instance of java.util.Date represents a specific
#Calendar() Constructs a default calendar.
+get(field: int): int Returns the value of the given calendar field. instant in time with millisecond precision.
+set(field: int, value: int): void Sets the given calendar to the specified value.
+set(year: int, month: int, Sets the calendar with the specified year, month, and date. The month • java.util.Calendar is an abstract base class for
dayOfMonth: int): void parameter is 0-based, that is, 0 is for January.
+getActualMaximum(field: int): int Returns the maximum value that the specified calendar field could have. extracting detailed information such as year, month,
+add(field: int, amount: int): void Adds or subtracts the specified amount of time to the given calendar field.
+getTime(): java.util.Date Returns a Date object representing this calendar’s time value (million
date, hour, minute and second from a Date object.
second offset from the Unix epoch).
+setTime(date: java.util.Date): void Sets this calendar’s time with the given Date object. • Subclasses of Calendar can implement specific
java.util.GregorianCalendar
calendar systems such as Gregorian calendar, Lunar
+GregorianCalendar() Constructs a GregorianCalendar for the current time. Calendar and Jewish calendar.
+GregorianCalendar(year: int, Constructs a GregorianCalendar for the specified year, month, and day of
month: int, dayOfMonth: int)
+GregorianCalendar(year: int,
month.
Constructs a GregorianCalendar for the specified year, month, day of
• Currently, java.util.GregorianCalendar for the
month: int, dayOfMonth: int,
hour:int, minute: int, second: int)
month, hour, minute, and second. The month parameter is 0-based, that
is, 0 is for January.
Gregorian calendar is supported in the Java API.
9 10

The GregorianCalendar Class


You can use new GregorianCalendar() to construct
a default GregorianCalendar with the current time
and use new GregorianCalendar(year, month, date)
to construct a GregorianCalendar with the
specified year, month, and date. The month
parameter is 0-based, i.e., 0 is for January.

11

You might also like