Abstract Class
Abstract Class
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.
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.
//
is this correct?
ANS.
Yesan object can be assigned to a reference
variable of the parent type.