0% found this document useful (0 votes)
28 views12 pages

Abstract Class: Cse215: Programming Language Ii Silvia Ahmed (Sva)

The document discusses abstract classes and abstract methods in Java. It provides an example of an abstract GeometricObject class that is the superclass of Circle and Rectangle classes. The GeometricObject class contains abstract methods like getArea() and getPerimeter() that are implemented in the subclasses. An abstract class cannot be instantiated and exists only to be subclassed. The GregorianCalendar class is provided as an example of a concrete subclass of the abstract Calendar class.

Uploaded by

amir hamzha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

Abstract Class: Cse215: Programming Language Ii Silvia Ahmed (Sva)

The document discusses abstract classes and abstract methods in Java. It provides an example of an abstract GeometricObject class that is the superclass of Circle and Rectangle classes. The GeometricObject class contains abstract methods like getArea() and getPerimeter() that are implemented in the subclasses. An abstract class cannot be instantiated and exists only to be subclassed. The GregorianCalendar class is provided as an example of a concrete subclass of the abstract Calendar class.

Uploaded by

amir hamzha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 11

Abstract Class

Department of Electrical and Computer Engineering,

North South University

Silvia Ahmed (SvA) CSE215: Programming Language II 1


Abstract Classes and Abstract Methods
GeometricObject Abstract class
-color: String
-filled: boolean
-dateCreated: java.util.Date
The # sign indicates
protected modifier #Geo metricObject()
#Geo metricObject(color: string,
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
are italicized Methods getArea and getPerimeter are overridden in
Circle and Rectangle. Superclass methods are generally
omitted in the UM L d iagram for subclasses.

Circle Rectangle
-radius: double -width: double

+Circle() -height: double


+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
+getDiameter(): double +setWidth(width: double): void
+getHeight(): double
+setHeight(height: double): void

Silvia Ahmed (SvA) CSE215: Programming Language II 2


Definition: abstract method & abstract class
• A method with empty body is known as an
abstract method.
• They are denoted with “abstract” modifier in the
method header.
• If a class contains one or more abstract methods,
then the class must be declared as abstract.

Silvia Ahmed (SvA) CSE215: Programming Language II 3


abstract method in abstract class

• An abstract method cannot be contained in a non-


abstract class.
• If a subclass of an abstract superclass does not
implement all the abstract methods, the subclass
must be defined abstract.
• In other words, in a non-abstract subclass
extended from an abstract class, all the abstract
methods must be implemented, even if they are
not used in the subclass.

Silvia Ahmed (SvA) CSE215: Programming Language II 4


object cannot be created from abstract class

• An abstract class cannot be instantiated


using the new operator, but you can still
define its constructors, which are invoked
in the constructors of its subclasses.
• For instance, the constructors of
GeometricObject are invoked in the
Circle class and the Rectangle class.

Silvia Ahmed (SvA) CSE215: Programming Language II 5


abstract class without abstract method

• A class that contains abstract methods must be


abstract.
• However, it is possible to define an abstract
class that contains no abstract methods.
• In this case, you cannot create instances of the
class using the new operator. This class is used
as a base class for defining a new subclass.

Silvia Ahmed (SvA) CSE215: Programming Language II 6


superclass of abstract class may be concrete

• A subclass can be abstract even if its


superclass is concrete.
• For example, the Object class is concrete,
but its subclasses, such as
GeometricObject, may be abstract.

Silvia Ahmed (SvA) CSE215: Programming Language II 7


concrete method overridden to be abstract

A subclass can override a method from its


superclass to define it abstract. This is rare,
but useful when the implementation of the
method in the superclass becomes invalid in
the subclass. In this case, the subclass must be
defined as abstract.

Silvia Ahmed (SvA) CSE215: Programming Language II 8


abstract class as type

• You cannot create an instance from an


abstract class using the new operator, but
an abstract class can be used as a data type.
• Therefore, the following statement, which
creates an array whose elements are of
GeometricObject type, is correct.
GeometricObject[] geo = new GeometricObject[10];

Silvia Ahmed (SvA) CSE215: Programming Language II 9


The Abstract Calendar Class and Its
GregorianCalendar subclass
java.util.Calendar
#Calendar() Constructs a default calendar.
+get(field: int): int Returns the value of the given calendar field.
+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
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.
+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
second offset from the Unix epoch).
+setTime(date: java.util.Date): void Sets this calendar’s time with the given Date object.

java.util.GregorianCalendar
+GregorianCalendar() Constructs a GregorianCalendar for the current time.
+GregorianCalendar(year: int, Constructs a GregorianCalendar for the specified year, month, and day of
month: int, dayOfMonth: int) month.
+GregorianCalendar(year: int, Constructs a GregorianCalendar for the specified year, month, day of
month: int, dayOfMonth: int, month, hour, minute, and second. The month parameter is 0-based, that
hour:int, minute: int, second: int) is, 0 is for January.

Silvia Ahmed (SvA) CSE215: Programming Language II 10


The Abstract Calendar Class and Its
GregorianCalendar subclass
• An instance of java.util.Date represents a specific
instant in time with millisecond precision.
• java.util.Calendar is an abstract base class for
extracting detailed information such as year, month,
date, hour, minute and second from a Date object.
• Subclasses of Calendar can implement specific
calendar systems such as Gregorian calendar, Lunar
Calendar and Jewish calendar.
• Currently, java.util.GregorianCalendar for the
Gregorian calendar is supported in the Java API.
Silvia Ahmed (SvA) CSE215: Programming Language II 11
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.

Silvia Ahmed (SvA) CSE215: Programming Language II 12

You might also like