Menu
Topics Index
...
`

Creating multiple Class with multiple methods and foreach loop


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating multiple Class with multiple methods and foreach loop
What will be the output of the following program?
import java.util.*;
class Shape {
    int value;
    public Shape(int value) {
        this.value = value;
    }
    public int shape() {
        return value + value;
    }
}
class Car extends Shape {
    public Car(int value) {
        super(value);
    }
    public int shape() {
        return value * value;
    }
}
public class SuperClassAndSubClass {
    public static void main(String arg[]) {
        List<Car> shapes = new ArrayList<Car>();
        shapes.add(new Car(12));  shapes.add(new Car(2));
        Car[] shapesArray = new Car[2];
        shapesArray[0] = new Car(12);     shapesArray[1] = new Car(2);
        SuperClassAndSubClass object = new SuperClassAndSubClass();
        object.printShapesArray(shapesArray);
        object.printShapesList(shapes);
    }
    public void printShapesList(List<Shape> shapes) {
        System.out.println();
        System.out.print("List :");
        for (Shape shape : shapes) {
            System.out.print(shape.shape() + " ");
        }
    }
    public void printShapesArray(Shape[] shapes) {
        System.out.print("Array :");
        for (Shape shape : shapes) {
            System.out.print(((Car) shape).shape() + " ");
        }
    }
}

A. Array :144 4
List :144 4
B. Array :24 4
List :144 4
C. Some other output
D. Compilation Error or Runtime Error
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

D.comp error shld happn..bcz incompatible type is there in shapesArray to take in Car.

Posted by Shabaaz Shaikz    2014-02-18 12:48:35


Ans:D compilation error int the class SuperClassAndSubClass since shapes cannot be accessed through list ..object declared for class and accessing list elements shapesArray as it is an object it can access but shapes is not possible

Posted by Karthik Narahari    2014-02-18 12:54:31


Congratulations Shabaaz Shaikz. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-19 03:56:57


tnksss..mc:-)

Posted by Shabaaz Shaikz    2014-02-19 06:22:28


© meritcampus 2019

All Rights Reserved.

Open In App