In this lab, you will demonstrate the Java Abstraction and we will utilize the previous lab (GLAB - 303.10.2)
Introduction:
In the previous lab (GLAB - 303.10.2), we used examples of Shapes. We created Circle, Rectangle, and Triangle objects. The Shape class can only be used as a superclass for Inheritance and Polymorphism purposes; it cannot be used for objects. The class that is not used for creating objects is known as abstract. Using an abstract class, you can improve the Shape class that was shown in the previous lab (GLAB - 303.10.2). Since there is no meaningful concept of area for an undefined two-dimensional shape, the following version of the program declares getArea( ) as an abstract method inside the Shape class. This means that all classes derived from the Shape class must override getArea( ). Remember that we cannot instantiate the Abstract class; so there is no need to create a Constructor in the Abstract class. We will remove the constructor from the Shape class and make a few changes in all subclasses accordingly.
Shape Class
Create a class named Shape. This will be an Abstract class and superclass. Write the code below:
publicabstractclassShape {protectedString color;protecteddouble height;protecteddouble width;protecteddouble base;publicvoidsetColor(String color) {this.color= color; }publicvoidsetWidth(double width) {this.width= width; }publicvoidsetHeight(double height) {this.height= height; }publicvoidsetBase(double base) {this.base= base; }// The getArea method is abstract.// It must be overridden in a subclass. /** All shapes must provide a method called getArea() */publicabstractdoublegetArea(); /** Returns a self-descriptive string */publicStringtoString() {return"Shape[color="+ color +"]"; }publicvoiddisplayshapName() {System.out.println("I am a Shape."); }}
Circle Class
Create a class named Circle. This will be a Child class. Write a code below:
publicclassCircleextendsShape {protecteddouble radius;privatefinaldouble PI =Math.PI;publicCircle(double radius) {this.radius= radius; }publicCircle(double radius,double height) {this.radius= radius; super.height= height; }publicdoublegetArea() {double area = PI *Math.pow(this.radius,2); // initializing value in parent class variablereturn area; //reference to parent class variable } @OverridepublicvoiddisplayshapName() {System.out.println("Drawing a Circle of radius "+this.radius); } /** Returns a self-descriptive string */ @OverridepublicStringtoString() {return"Circle[ radius = "+ radius + super.toString() +"]"; }}
Rectangle Class
Create a class named Rectangle. This will be a Child class. Write the code below:
publicclassRectangleextendsShape {publicRectangle(String color) { super.color= color; }publicRectangle() { }publicRectangle(String color,double width,double height) { super.height= height; super.width= width; super.color= color; } @OverridepublicdoublegetArea() {return super.width* super.height; }//Overriding method of base class with different implementation @OverridepublicvoiddisplayshapName() {System.out.println("I am a Rectangle" ); } @OverridepublicStringtoString() {return"Rectangle[height="+ height +",width="+ width +","+ super.toString() +"]"; }}
Triangle Class
Create a class named Triangle. This will be a Child class. Write the code below:
publicclassTriangleextendsShape {publicTriangle(){}publicTriangle(String color) { super.color= color; }publicvoidsetBase(int base) {this.base= base; } @OverridepublicdoublegetArea() {return0.5*super.base* super.height; }//Overriding method of base class with different implementation @OverridepublicvoiddisplayshapName() {System.out.println("I am a TriAngle" ); } /** Returns a self-descriptive string */ @OverridepublicStringtoString() {return"Triangle[base="+ super.base+",height="+ super.height+","+ super.toString() +"]"; }}
myRunner Class
Create a class named myRunner. This will be the Main class or entry point for the application. Write the code below:
publicclassmyRunner {publicstaticvoidmain(String[] args) {Circle c =newCircle(100);System.out.println("Area of Circle "+c.getArea());// Shape sObj = new Shape(); // This will give Error, we can not instantiate Abstract class// object creation of the Circle classSystem.out.println("+++++++++++++++++");// it’s fine because a Circle is a Shape by inheritanceShape shapeCircleObj =newCircle(100); // UpCastingshapeCircleObj.displayshapName();System.out.println("Area of Circle "+shapeCircleObj.getArea());System.out.println(shapeCircleObj); // Run circle's toString()// Use instanceof operator for ValidationSystem.out.println(shapeCircleObj instanceof Circle); // trueSystem.out.println("+++++++++++++++++");Shape shapeRectangleObj =newRectangle("Red"); //UpCastingshapeRectangleObj.displayshapName();shapeRectangleObj.setHeight(2);shapeRectangleObj.setWidth(4);System.out.println("Area of Rectangle is "+shapeRectangleObj.getArea());System.out.println(shapeRectangleObj); // Run Rectangle's toString()// Use instanceof operator for ValidationSystem.out.println(shapeRectangleObj instanceof Rectangle); // trueSystem.out.println("--------------------");Shape shapeTriangleObj =newTriangle("Blue"); //UpCastingshapeTriangleObj.displayshapName();shapeTriangleObj.setHeight(10);shapeTriangleObj.setBase(15);System.out.println("Area of Triangle is "+shapeTriangleObj.getArea());System.out.println(shapeTriangleObj); // Run Triangle's toString() }}
Output:
Area of Circle 31415.926535897932
+++++++++++++++++
Drawing a Circle of radius 100.0
Area of Circle 31415.926535897932
Circle[ radius = 100.0Shape[color=null]]
true
+++++++++++++++++
I am a Rectangle
Area of Rectangle is 8.0
Rectangle[height=2.0,width=4.0,Shape[color=Red]]
true
--------------------
I am a TriAngle
Area of Triangle is 75.0
Triangle[base=15.0,height=10.0,Shape[color=Blue]]
true