Topics Index

calling super class Constructor1744

If you need explanation Read this topic

If you need Answer Take test on this topic

calling super class Constructor1744
What will be the output of the following program?
public class Display1 extends Display2 {
    int number = Math.round(7.4f);
    public static void main(String args[]) {
        Display2 base = new Display1();
    void print() {    System.out.print(number + " ");    }
class Display2 {
    Display2() {    print();    }
    void print() {    System.out.print("Base1" + " ");    }

A. Base1 7
B. Base1 Base1
C. 7 7
D. 0 7
E. Compilation Error or Runtime Error
Topic: Super Keyword In Java To Call Super Class Constructor

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 A.
number will contain value 7
After creating Display1() object it will internally calls Display2() constructor becoz by default Display1() contains super() method. In Display2() constructor it calls print() method of Display2.
So it prints "Base1"
As the print() method is overridden, runtime polymorphism occurs.. so Display1 class print() will be executed..
So it prints "7"

Output is : Base1 7

Posted by Gopinath Manchikanti    2014-10-13 04:15:21


Posted by Elias Dblo    2014-10-13 06:06:25

A.....coz we create object for the reference of Display2

Posted by Arul Palanisamy    2014-10-13 08:02:01


Posted by Shubham Bansal    2014-10-13 13:48:53

Ans is  D

we have two classes..Display1 and Displa2() it is an Parent class

coming to the program we create reference for the Displ2 and Object for the Disp1..
Parent class reference can refer Child class Object

wen you create this Parent class Constructor wil cal before calling the Child class Constructor..
in Parent class we are calling print() means it wil call the method in the Child class than there it wil print as 0..
next we are calling base.print() means it wil cal print() in dips1 and prints as 7, 7.5f wil rounded it into 7..
0 7 Op

Posted by Uday Kumar    2014-10-13 15:30:20

ans d.

class display 1 inherits all the properties of display 2.

int number value is globally assigned with math.round(7.4f) value of number is 7 .bcz as per round of rule if value less than 7.5 is 7,..if value greater than or equal to 7.5 than value of number will be 8.

inside main method display 2 reference variable is assigned with display 1 object.
constructrs are always executed 1st so print func executed .

number value prints default 0. bcz display 2 base class it don't have permission to access derived class.

base.print method prints 7.

Posted by Maheshwari Natarajan    2014-10-13 16:01:21

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Maheshwari Natarajan,  for 'Best Comment' and Šâí Râm for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-10-14 04:12:26

© meritcampus 2019

All Rights Reserved.

Open In App