Menu
Topics Index
...
`

Creating abstract class


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating abstract class
What is the output of the following program?
public class Dhoni {
    static Dhoni dhoni;
    public static void main(String... k) {
        Animal a;
        System.out.print((a = new Cat()).prey().getClass().getSimpleName() + " ");
        System.out.print((a = new Rabbit()).prey().getClass().getSimpleName() + " ");
        System.out.print((a = new Lion()).prey().getClass().getSimpleName() + " ");
    }
}
abstract class Animal {abstract Animal prey(); }
class Cat extends Animal {
    Cat prey() { // LINE B
        return this;
    }
}
class Rabbit extends Animal {    Animal prey() { return this; } }
abstract class Lion extends Animal { // LINE A
    Animal prey() { return this; }
}

A. Cat Rabbit Lion
B. Cat Animal Animal
C. Compilation Error at LINE A, since a class with no abstract methods can not be declared as abstract
D. Compilation Error at LINE B, since overridden methods return type can not be changed
E. Compilation Error because of some other reason
Topic: Static and Non Static Variables - Static and Non Static Methods

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.

Ans is e... lion class is abstract, so we cannot create the lion object directly with new operator

Posted by Krishna Sai    2013-08-15 08:48:24


e is the answer since the Lion cannot be instantiated in the thrid SOP statement in main method

Posted by Karteek Paruchuri    2013-08-15 08:51:11


E.. becoz we cannot create objects for abstract classes(Lion)

Posted by Gopinath Manchikanti    2013-08-15 08:52:28


Congratulations Krishna Sai Mulpuri. You are this dose winner. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-08-16 03:24:48


© meritcampus 2019

All Rights Reserved.

Open In App