Menu
Topics Index
...
`

Creating classes inside other class1636


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating classes inside other class1636
What will be the output of the following program?
class BaseClass {
    BaseClass() {
        System.out.print("*");
    }
    BaseClass(String str) {
        this();
        System.out.print("@" + str);
    }
}
public class ChildClass extends BaseClass {
    ChildClass() {
        System.out.print("#");
    }
    ChildClass(String name) {
        this();
        System.out.print("$" + name);
    }
    public static void main(String args[]) {
        new ChildClass("hello");
    }
}

A. *#$hello
B. *@#$hello
C. *@$hello
D. Some other output
E. Compilation Error or Runtime Error
Topic: Inheritance And Constructors In Java

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 b bcz
first reference object of child class is created which invokes parent class constructor baseclass(string)which then executes this() implies baseclass() then print stmts then childclass() is executed bcz of this() then child class(string) is executed which prints "$hello"

Posted by Pooja Singh    2014-07-04 13:46:54


Ans: A ChildClass(String) calls ChildClass() which inturn calls its super class default constructor BaseClass() which inturn calls Object() constructor but the execution of function calls follows from super class to child class in the order *#$hello gets printed

Posted by Srinivas Nalla    2014-07-04 14:05:53


its D....because the child class constructor doesn't include the super(); reference to the base class constructors

Posted by Arslan Karamat    2014-07-04 14:08:38


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

Posted by Merit Campus    2014-07-07 05:23:09


© meritcampus 2019

All Rights Reserved.

Open In App