What is the output of the following Java program?
class A
{
int a,b;
A()
{
a=20;
b=30;
}
}
class B
{
int a=10,b=5;
B()
{
a=this.a+super.a;
b=this.b+super.b;
}
}
class C
{
C()
{
System.out.println(super.a+super.b);
}
}
public class Hello
{
public static void main(String[] args)
{
C c=new C();
}
}
-
50
-
15
-
100
-
65
-
None of the above
D
Correct answer
Explanation
Key concepts:
this is used to access methods or variables of the current class.
super is used to access methods or variables of the base class.
Program execution after creating object for child class C , is given as follows
|||
|---|---|
|class A
{
int a,b;
A()
{
a=20;
b=30;
}
}|a=20,
b=30|
|class B
{
int a=10,b=5;
B()
{
a=this.a+super.a;
b=this.b+super.b;
}
}|a=10+20=30,
b=5+30=35
|
|class C
{
C()
{
System.out.println(super.a+super.b);
}
}|prints a+b value
a+b=30+35=65|
| |Hence 65 is the output of the given program|