0% found this document useful (0 votes)
18 views

Abstract Class

An abstract class can contain abstract and non-abstract methods. It cannot be instantiated but requires subclasses to implement abstract methods. The document provides an example of an abstract GraphicObject class with subclasses Rectangle and Circle that implement abstract draw and resize methods.

Uploaded by

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

Abstract Class

An abstract class can contain abstract and non-abstract methods. It cannot be instantiated but requires subclasses to implement abstract methods. The document provides an example of an abstract GraphicObject class with subclasses Rectangle and Circle that implement abstract draw and resize methods.

Uploaded by

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

Abstract Class

Declared with abstract keyword.


Contains abstract method.
Cant instantiated.
Requires subclasses to implement abstract
method.
can have data member, abstract method, method body,
constructor and even main() method.
Also used in Factory pattern.
If you are extending any abstract class that have abstract method, you must
either provide the implementation of the method or make this class abstract.

Scenario
These objects all have certain states (for example: position,
orientation, line color, fill color) and behaviors (for example:
moveTo, rotate, resize, draw) in common. Some of these states
and behaviors are the same for all graphic objects (for
example: position, fill color, and moveTo). Others require
different implementations (for example, resize or draw).
All GraphicObjects must be able to draw or resize
themselves; they just differ in how they do it.

Classes Rectangle, Line, Bezier, and Circle Inherit from GraphicObject

Program
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}

Interview question
Q. Can an abstract parent class have non-abstract children?
ANS. Yesan abstract parent can have both abstract and
non-abstract children.

Q. Can an abstract method be defined in a non-abstract


class?
ANS.
Noif a class defines an abstract method the class
itself must be abstract.

Q. Can an object of a child type be assigned to a variable of


the parent type? For example,
Card crd;
BirthDay bd = new BirthDay("Lucinda", 42);
crd = bd;

//

is this correct?

ANS.
Yesan object can be assigned to a reference
variable of the parent type.

Q. A class can be made abstract without any abstract


method?
ANS. YES

You might also like