Question

Write a class to try the following code that makes use of polymorphism and explains the...

Write a class to try the following code that makes use of polymorphism and explains the output. Some instructions may cause errors of compilation, in that case explains the error.

Shape s1 = new Circle(5.5, "RED", false); // Upcast Circle to Shape
System.out.println(s1); // which version?
System.out.println(s1.getArea()); // which version?
System.out.println(s1.getPerimeter()); // which version?
System.out.println(s1.getColor());
System.out.println(s1.isFilled());
System.out.println(s1.getRadius());
Circle c1 = (Circle)s1; // Downcast back to Circle
System.out.println(c1);
System.out.println(c1.getArea());
System.out.println(c1.getPerimeter());
System.out.println(c1.getColor());
System.out.println(c1.isFilled());
System.out.println(c1.getRadius());
Shape s2 = new Shape();
Shape s3 = new Rectangle(1.0, 2.0, "RED", false); // Upcast
System.out.println(s3);
System.out.println(s3.getArea());
System.out.println(s3.getPerimeter());
System.out.println(s3.getColor());
System.out.println(s3.getLength());
Rectangle r1 = (Rectangle)s3; // downcast
System.out.println(r1);
System.out.println(r1.getArea());
System.out.println(r1.getColor());
System.out.println(r1.getLength());
Shape s4 = new Square(6.6); // Upcast
System.out.println(s4);
System.out.println(s4.getArea());
System.out.println(s4.getColor());
System.out.println(s4.getSide());
// Take note that we downcast Shape s4 to Rectangle,
// which is a superclass of Square, instead of Square
Rectangle r2 = (Rectangle)s4;
System.out.println(r2);
System.out.println(r2.getArea());
System.out.println(r2.getColor());
System.out.println(r2.getSide());

System.out.println(r2.getLength());
// Downcast Rectangle r2 to Square
Square sq1 = (Square)r2;
System.out.println(sq1);
System.out.println(sq1.getArea());
System.out.println(sq1.getColor());
System.out.println(sq1.getSide());
System.out.println(sq1.getLength());

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Here i used the class name Main which is the main class of entire program using polymorphism.

//one more thing, you called all the methods like getArea,getPerimeter,getRadius,getSide,isFilled etc.. but not implemented.

//And you dont mention to implement all those methods .so i just declare that methods in the program without implementing.

// If you want implementation comment me. otherwise please follow the code step by step.

import java.io.*;
import java.util.*;

// When Subclass type refers to the object of Parent class, it is known as downcasting.
//When Parentclass type refers to the object of sub class, it is known as upcasting.
class Main{
   class Shape{
       public double getArea(){

       }
       public double getColor(){

       }
       public double getPerimeter(){

       }
       public double getLength(){

       }
       public double getRadius(){

       }
       public double isFilled(){

       }
       public double getSide(){

       }

       }
   class Circle extends Shape{
       double a;
       String string;
       boolean b;
       Circle(double a1,String s,boolean b){
           a = a1;
           string = s;
           b = b;

       }

   }
   class Rectangle extends Shape{
       Rectangle(double a2,double b2,String s,boolean b){

       }


   }
   class Square extends Shape{
       Square(double a){

       }
   }
   public static void main(String[] args) {
       Shape s1 = new Circle(5.5, "RED", false); // Upcast Circle to Shape

       System.out.println(s1); // which version?
System.out.println(s1.getArea()); // which version?
System.out.println(s1.getPerimeter()); // which version?
System.out.println(s1.getColor());
System.out.println(s1.isFilled());
System.out.println(s1.getRadius());
Circle c1 = (Circle)s1; // Downcast back to Circle
System.out.println(c1);
System.out.println(c1.getArea());
System.out.println(c1.getPerimeter());
System.out.println(c1.getColor());
System.out.println(c1.isFilled());
System.out.println(c1.getRadius());
Shape s2 = new Shape();
Shape s3 = new Rectangle(1.0, 2.0, "RED", false); // Upcast
System.out.println(s3);
System.out.println(s3.getArea());
System.out.println(s3.getPerimeter());
System.out.println(s3.getColor());
System.out.println(s3.getLength());
Rectangle r1 = (Rectangle)s3; // downcast
System.out.println(r1);
System.out.println(r1.getArea());
System.out.println(r1.getColor());
System.out.println(r1.getLength());
Shape s4 = new Square(6.6); // Upcast
System.out.println(s4);
System.out.println(s4.getArea());
System.out.println(s4.getColor());
System.out.println(s4.getSide());
// Take note that we downcast Shape s4 to Rectangle,
// which is a superclass of Square, instead of Square
//super class of Square is Shape class which is the super class of remaining classes like circle,rectangle
Rectangle r2 = (Rectangle)s4;
System.out.println(r2);
System.out.println(r2.getArea());
System.out.println(r2.getColor());
System.out.println(r2.getSide());

System.out.println(r2.getLength());
// Downcast Rectangle r2 to Square
Square sq1 = (Square)r2;
System.out.println(sq1);
System.out.println(sq1.getArea());
System.out.println(sq1.getColor());
System.out.println(sq1.getSide());
System.out.println(sq1.getLength());
      
   }
}

//you will see the following errors if you run the above code.

if you observe the above image,you will have mainly two errors.

1.non-static varible cannot be referenced from a static context.

Main reason for this is accessing non-static variables in the static method i.e. public static main method.

To fix this, we need to convert the variables to static or change the method type. or create the object by using that object we can access correctly

if you follow above steps you will fix this issue.

2.incompatible types means you are using two different types.

Simply you are doing downcast here.But you will use rectangle class object i.e. r2 but it is not super class to the square .so it is showing error.

if you want to fix this error you need to mention parent class object (here parent class is shape).

so ,in this way we will fix these errors.

You said that mention the errors only.so iam not fixing all these errors and i explained you errors.

Add a comment
Know the answer?
Add Answer to:
Write a class to try the following code that makes use of polymorphism and explains the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle....

    In Lab 5, you completed the MyRectangle class, which is a simple representation of a rectangle. Review Lab 5 before completing this lab and retrieve the MyRectangle class that you completed for this lab, since you will need it again in this lab. Before starting this lab, edit your MyRectangle class in the following way: • change the declaration of your instance variables from private to protected This will enable access of these variables by your MySquare subclass. Here is...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT