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

    class Rectangle extends Shape
    {
    public int area()
    {
    return 6;
    }
    }

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

Child object can be assigned to parent variable without change in behaviour.



Your comments will be displayed only after manual approval.