Home » JAVA Programming » Inheritance » Question
  1. What is the outcome of below code SquareExample class?
    class Shape 
    {
    public int area()
    {
    return 2;
    }
    }

    class Square extends Shape
    {
    public int area()
    {
    return 7;
    }
    }

    public class SquareExample
    {
    public static void main(String[] args)
    {
    Shape obj = new Shape();
    Square obj0 = new Square();
    obj0 = obj;
    System.out.println(obj0.area());
    }
    }
    1. Compilation Error
    2. Runtime Error
    3. Default Value
    4. 7
    5. None of these
Correct Option: A

Compilation Error

SquareExample.java:23: error: incompatible types: Shape cannot be converted to Square
obj0 = obj;
^
1 error



Your comments will be displayed only after manual approval.