Menu
Topics Index
...
`

CPU and MotherBoard


If you need explanation Read this topic

If you need Answer Take test on this topic

CPU and MotherBoard
What will be the output of the following program?
class MotherBoard
{
    protected MotherBoard()
    {
        System.out.print(" MotherBoard ");
    }
}

public class CPU extends MotherBoard
{
    private CPU()
    {
        System.out.print(" Inside CPU ");
    }

    public static void main(String args[])
    {
        new CPU();
    }
}

A. MotherBoard
B. MotherBoard  Inside CPU
C. Inside CPU
D. Inside CPU  MotherBoard
E. Compilation 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.

here ans is B
here in concept of inheritence
in class CPU extends MOTherboard means all methodsand members in motherboard class will be come into cpu class
and in main() method we creating a object for Cpu class when object was created constructer will be called  but  in Cpu class there are two constucters
1.child constructer(means cpu());
2.parent constructer(means motherboard();)

but first parent constructer will be excuted and after child constructer will be called if exclusively call for child constructer use this() method;
thats why  result printed is MotherBoard  Inside CPU
option is B

Posted by Goutham Singarapu    2015-03-06 13:04:17


Ans is B here we have the inheritance concept where child class inherits the properties of the Parent class Except the Private members
Execution:
here we have the MotherBoard class with the constructor as protected and class CPU extending the MotherBoard class in the main() we create the instance for the child class(CPU) so when inheritance is involved when we create the instance for the Child class than the Control will goes to corresponding constructor but before executing the child class constructor control will goes to the default constructor in the Parent class than after completing the execution control will comes back to the Child class constructor and complete the execution, if we want pass control to some other constructor in the Parent class from the child class than we need to use the Super()

Posted by Uday Kumar    2015-03-07 02:23:42


ANS IS B (Motherbord Inside CPU)

--Here the concept of "Inheritance" is used.

--Here,2 classes are defined namely:
1.Motherbord (parent/super class)
2.CPU (child/sub class which extends Motherbord class)

         Motherbord (Parent class)
         ----------
             |   Inheritance
             V
            CPU    (child/sub class)

--Inside motherboard class constructor with access specifier protected is declared inside which "motherboard" will get printed.

----In CPU class constructor with access specifier private is declared inside which "Inside CPU" will get printed.

--In main() method object of CPU is created which calls the constructor of CPU class,which also calls the constructor of parent/base class i.e. motherboard so "MotherBoard" will get printed.

OUTPUT:
Motherbord Inside CPU

Posted by Mânïshå Mùlchåndânï    2015-03-07 13:20:09


This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is TâRüñ TK. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-09 02:02:09


© meritcampus 2019

All Rights Reserved.

Open In App