In: Computer Science
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Here is the code for the problem:
//The abstract class called Shape
abstract class Shape {
//The strin variable to set the color
String color;
Shape(String color){
this.color = color;
}
//getter method for color
public String getColor() {
return color;
}
//setter method for color
public void setColor(String color) {
this.color = color;
}
abstract void getArea();
}
//Driver class that extends the above abstrcat class
public class AbstractClassExample extends Shape {
//The test class constructor
AbstractClassExample() {
//calls the Super class
constructor
super("Blue");
}
void getArea()
{
System.out.println("The getArea()
method");
}
public static void main(String[] args) {
//Instantiating the abstract class
using the extended class
AbstractClassExample shape = new
AbstractClassExample();
//setting the color of the
shape
shape.setColor("Blue");
//output the color set for the
shape
System.out.println("The color set
is: " + shape.getColor());
shape.getArea();
}
}
SCREENSHOT:
OUTPUT: