Professional Documents
Culture Documents
Abstract
Lahore Campus
Section: C
Course: OOP
Observation
Q#1 Remove the implementation of getArea() from the Rectangle class. Is there any
error? Why?
Ans: Yes, Error in Rectangle class because it is inheriting from Shape class which is
abstract, and it is must to define the implementation of abstract method in child class.
Q#2 Remove the abstract keyword with Shape class. Is there any error? Why?
Ans: Yes, there are two methods implemented abstract in Shape class which requires a
class to be defined abstract.
Q#3 Try to create an object of class Shape. Is there any error? Why?
Ans: It is not possible to create an object in Shape class because it holds the general
concept not the proper implementation for the objects.
Shape:
public Shape() {
setColor("Green");
setFilled(true);
@Override
public String toString() {
return " A " + isFilled() + " Shape with " + getColor() + "
color."; }
Rectangle:
public class Rectangle extends Shape {
private double width;
private double length;
public Rectangle() {
setWidth(1.0);
setLength(1.0);
}
setWidth(width);
setLength(length);
}
@Override
public String toString() {
return " A Rectangle with width = " + getWidth() + " and length = "
+ getLe ngth() + " which is a subclass of "
+ super.toString();
}
Square:
public class Square extends Rectangle {
public Square() {
}
@Override
public void setWidth(double side) {
super.setWidth(side);
super.setLength(side);
}
@Override
public void setLength(double side) {
super.setWidth(side);
super.setLength(side);
}
@Override
public String toString() {
return "A square with side = " + getLength() + " which is a
subclass of " + super.toString();
}
}
ShapeTest:
import java.util.ArrayList;
shapes.add(new Square());
shapes.add(new Square(3.0));
shapes.add(new Square("yellow", true, 5.0));
}
}
Output