What will be the output of the following Java code?
class A
{
public int i;
private int j;
}
class B extends A
{
void display()
{
super.j = super.i + 1;
System.out.println(super.i + ” ” + super.j);
}
}
class inheritance
{
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
Answer: d
Explanation: Class contains a private member variable j, this cannot be inherited by subclass B and does not have access to it.
output:
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field A.j is not visible
Related Posts
Which one of the following operations returns the first occurrence of bit 1 in bit arrays?
Bit fields and Bit arrays are same.
What does Hamming weight/population count mean in Bit arrays?
Run-Length encoding is used to compress data in bit arrays.
Which of the following bitwise operator will you use to invert all the bits in a bit array?
Which class in Java can be used to represent bit array?
Which of the following is/are not applications of bit arrays?
Join The Discussion