Menu
Topics Index
...
`

Classes890


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes890
What will be the output of the following program?
class OutPut
{
    public static void main(String[] args)
    {
        PrintB c = new PrintB();

        System.out.println("printA = " + c.printA);

        System.out.println("printB = " + c.printB);
    }
}

class PrintA
{
    int printA = 5;
}

class PrintB extends PrintA
{
    printA = 10;
    int printB = 5;
}

A. printA = 10
printB = 5
B. printA = 5
printB = 5
C. Compilation Error
D. Runtime Error
Topic: Assigning Sub Class Object To Super Class Reference 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 C ,, due to line "printA=10;" in PrintB class,,super class variable only define in constructor,,

Posted by Raju VarshnEy    2014-11-25 12:19:22


Ans is C...here we have the Inheritance concept...by inheritance Child class can get properties of parent class...

here printA will inherited into int PrintB class But initilization must be done Constructor or Method...so compiler will raise an error

Posted by Uday Kumar    2014-11-25 12:24:57


Answer is C...
printB is a sub class of printA and printB class is trying to assign the value to printA in the printB class....
printA should be actually initialised through the constructor using super i.e. super.printA or by invoking the constructor of super class from the current class.

Posted by Shashanka Mogaliraju    2014-11-25 16:37:07


ans c.      in this program we hav 3 classes.in main class we created object for PrintB. ,here class PrintB inherits all the features of PrintA.thn here we trying to assign value for superclass member variable,so compilr throws error. Each class contains code to initialize the fields explicitly declared in that class. Unlike methods, constructors are never inherited. If you don't explicitly declare a constructor in a class, that class will not inherit a constructor from its direct superclass. Instead, the compiler will generate a default constructor for that class. This is because a superclass constructor can't initialize fields in the subclass. A subclass must have its own constructor to initialize its own instance variables. In the class file, this translates to: every class has at least one <init> method responsible for initializing the class variables explicitly declared in that class.                                      restrictions that a class must obey to allow inheritance. Constructors must not invoke overridable methods, directly or indirectly. If you violate this rule, program failure will result. The superclass constructor runs before the subclass constructor, so the overriding method in the subclass will get invoked before the subclass constructor has run. If the overriding method depends on any initialization performed by the subclass constructor, the method will not behave as expected.                                                                                                                                          You can't initialize printA in PrintB class  because printA is  not member of PrintB. it is  member of PrintA, therefore  PrintA class only can initialize them. You can make them public, then do assignment in B, but that is not a recommended option since it would destroy encapsulation. Instead, create a constructor in PrintA to allow PrintB to initialize them:

Posted by Maheshwari Natarajan    2014-11-25 18:09:04


This dose is now closed and the winners are Uday Kumar, for 'First Correct Comment', Maheshwari Natarajan, for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Sai Ram. 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    2014-11-26 05:37:39


© meritcampus 2019

All Rights Reserved.

Open In App