Knowee
Questions
Features
Study Tools

Will the following code get executed successfully ?abstract class Shape{    int i = 111, j = 222;    abstract void calcArea();    abstract void calcVolume();}class Square extends Shape{    void calcVolume() { System.out.println(j); }    void calcArea(){ System.out.println(j); }}public class Test{    public static void main(String[] args)    {        Square c = new Square();         c.calcArea();         c.calcVolume();          }}Select one:a.Yes, the code will get executed successfully.b.No – Compilation error.

Question

Will the following code get executed successfully ?abstract class Shape{    int i = 111, j = 222;    abstract void calcArea();    abstract void calcVolume();}class Square extends Shape{    void calcVolume() { System.out.println(j); }    void calcArea(){ System.out.println(j); }}public class Test{    public static void main(String[] args)    {        Square c = new Square();         c.calcArea();         c.calcVolume();          }}Select one:a.Yes, the code will get executed successfully.b.No – Compilation error.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

b. No – Compilation error.

The code will not compile successfully because the class 'Square' is extending an abstract class 'Shape' but it's not providing the implementation for all the abstract methods of the 'Shape' class. In Java, if a class extends an abstract class, it must provide the implementation for all the abstract methods of the abstract class, otherwise, the class itself should be declared as abstract. In this case, the 'Shape' class has two abstract methods 'calcArea()' and 'calcVolume()', but the 'Square' class is only providing the implementation for 'calcArea()' method. So, the 'Square' class should either provide the implementation for 'calcVolume()' method as well or it should be declared as abstract.

This problem has been solved

Similar Questions

What is the output of the given code?abstract class Shape{    int i = 111, j = 222;    abstract void calcArea();    abstract void calcVolume();} abstract class Quadrilateral extends Shape{    void calcArea()    {        System.out.println(i);     }}class Square extends Quadrilateral{    void calcVolume()    {        System.out.println(j);     }}public class Test{    public static void main(String[] args)    {        Square c = new Square();         c.calcArea();         c.calcVolume();              }}Select one:a.Compile time error because 'class Square' is not override all the abstract methods, so should declare it as 'abstract'b.Compile time error because trying to instantiate the 'class Square' which does not override all the abstract methodsc.Run time Errord.111222

Will the below code will execute successfully ?abstract class Shape{    private abstract int calcArea();}Select one:TrueFalse

What will be the output of the following code?public class Circle {    private double radius;    public Circle(double radius) {        this.radius = radius;    }    public double getArea() {        return Math.PI * radius * radius;    }    public static void main(String[] args) {        Circle c = new Circle(2.5);        System.out.println(c.getArea());    }}Question 14Answera.3.14b.6.28c.19.63d.Compilation error

Which two classes use the Shape class correctly?A. public class Circle implements Shape   {  private int radius;  }B. public abstract class Circle extends Shape   {  private int radius;  }C. public class Circle extends Shape   {  private int radius;  public void draw();  }D. public abstract class Circle implements Shape   {  private int radius;  public void draw();  }E. public class Circle extends Shape   {  private int radius;  public void draw()  {   /* code here */  }  }F. public abstract class Circle implements Shape   {   private int radius;   public void draw()    {    /* code here */    }  }OptionsA,CB,EC,ET,H

Select the correct answerWhat will be the output of the following Java program? class shape {  int breadth;  int height;  int area;  void area()  {   area = 1/2 * breadth * height;  }   void area(int x)  {   area = x;  }  }  class triangle  {  public static void  main(String args[])  {   shape obj = new shape();   obj.breadth = 4;   obj.height = 4;   obj.area(4);    System.out.println(obj.area);   }  }Options04816

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.