Note that there are some explanatory texts on larger screens.

plurals
  1. POReview on Inheritance and polymorphism in Java
    primarykey
    data
    text
    <p>I am studying for a final and I encountered an example question that deals with understanding inheritance and polymorphism. I have to explain whether each line will produce a compiler error (C), a run-time error(R), or whether it runs fine (F). I wrote down and explained the outcome of each line, but I would like someone to provide or point out mistakes that I have in my answers and perhaps correct my misunderstanding. </p> <p>Given the following classes:</p> <pre><code>public abstract class Shape{ protected double area; public double getArea(){ return area; } public abstract void computeArea(); } </code></pre> <p>.</p> <pre><code>public class Rectangle extends Shape{ private double s1,s2; public static String name="Rectangle"; public void setS1(double s1){ this.s1 = s1; } public void setS2(double s){ s2 = s; } public void computeArea(){ area = s1*s2; } } </code></pre> <p>.</p> <pre><code> public class TestRectComp{ public static void main(String[] args){ Shape s = new Rectangle(); Rectangle r = (Rectangle)s; Rectangle r2 = s; Rectangle[] rar = new Rectangle[1]; s.setS1(3.0); s.computeArea(); rar[0].computeArea(); r.s1 = 4.5; r.setS1(5.0); r.setS2(3.0); s.getArea(); System.out.println(r.computeArea()); r = null; rar[1] = new Rectangle(); System.out.println(Rectangle.name); } } </code></pre> <p>This is what I wrote:</p> <pre><code>Shape s = new Rectangle(); </code></pre> <p>This line is fine because you are creating a shape s that looks like a rectangle since Rectangle extends Shape. However, s can only access methods from the class Shape in this situation (it normally would access the other methods from the class Rectangle if there are over ridden methods or constructors). This line is fine (F).</p> <pre><code>Rectangle r = (Rectangle)s; </code></pre> <p>I'm having hard time understanding this line, but i'm thinking that this line is also fine (F) because you are casting the s Shape down to Rectangle. That is, Shape s can also use methods from class Rectangle unlike the line above. </p> <pre><code>Rectangle r2 = s; </code></pre> <p>Here you are casting s into a Rectangle. This will result in a compiler error (C) because you can't cast the parent class (s is a object of the class Shape) into it's child class (the class Rectangle)</p> <pre><code>Rectangle[] rar = new Rectangle[1]; </code></pre> <p>This line is fine (F) because you are just creating an array called rar that has a length of 2.</p> <pre><code>s.setS1(3.0); </code></pre> <p>Since the method setS1 in the class Rectangle has no restrictions. And since s is able to access class Rectangle because we casted into a a type of Rectangle from the line <code>Rectangle r = (Rectangle)s;</code> this line is also fine (F).</p> <pre><code>s.computeArea(); </code></pre> <p>This line will result in an run time error (R) because the computeArea method is null since area was never initialized? I'm not sure about this one. </p> <pre><code>rar[0].computeArea(); </code></pre> <p>Here the line will work fine (F). However there is nothing in the computeArea because s1 and s2 are not yet initialized. </p> <p>Anyway, any inputs is appreciated. Thanks </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload