Menu
Question Index
...

Why does the following program fail to compile?

public class BabyBoyBabyGirl {
    public static void main(String[] args) {
        BabyBoy bb = new BabyBoy();
        bb.print();
        AbstractBaby ab = bb;
        ((BabyBoy) ab).print();
        ab = new BabyGirl();
        ab.print();
        ((BabyGirl) ab).print();
    }
}
interface Baby {
    String getName();
    int getAge();
    void print();
}
abstract class AbstractBaby {
    String name;
    int age;
    public String getName() { return name; }
    public int getAge() { return age; }
}
class BabyBoy extends AbstractBaby implements Baby {
    public void print() {
        System.out.println("Boy " + getName() + "(" + getAge() + ")");
    }
}
class BabyGirl extends AbstractBaby implements Baby {
    public void print() {
        System.out.println("Girl " + getName() + "(" + getAge() + ")");
    }
}


Compilation Error because the abstract class does not have abstract methods
Compilation Error because we can not call print on bb
Compilation Error for some other reason
The program compiles with out any errors

Doubts

Problems

Topic: Unknown

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App