In this post, there are some 40 Java inheritance quiz type questions and answers which will help you to understand Java inheritance concept better.
Java Inheritance Quiz Practice Coding Questions :
1) Tinku has written the code like below. But, it is showing compile time error. Can you identify what mistake he has done?
class X
{
//Class X Members
}
class Y
{
//Class Y Members
}
class Z extends X, Y
{
//Class Z Members
}
In Java, a class can not extend more than one class. Class Z is extending two classes – Class X and Class Y. It is a compile time error in Java.
2) What will be the output of this program?
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
10
3) What will be the output of the below program?
class A
{
{
System.out.println(1);
}
}
class B extends A
{
{
System.out.println(2);
}
}
class C extends B
{
{
System.out.println(3);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}
1
2
3
4) Can a class extend itself?
No. A class can not extend itself.
5) What will be the output of the following program?
class A
{
String s = "Class A";
}
class B extends A
{
String s = "Class B";
{
System.out.println(super.s);
}
}
class C extends B
{
String s = "Class C";
{
System.out.println(super.s);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
System.out.println(c.s);
}
}
Class A
Class B
Class C
6) What will be the output of this program?
class A
{
static
{
System.out.println("THIRD");
}
}
class B extends A
{
static
{
System.out.println("SECOND");
}
}
class C extends B
{
static
{
System.out.println("FIRST");
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}
THIRD
SECOND
FIRST
7) What will be the output of the below program?
class A
{
public A()
{
System.out.println("Class A Constructor");
}
}
class B extends A
{
public B()
{
System.out.println("Class B Constructor");
}
}
class C extends B
{
public C()
{
System.out.println("Class C Constructor");
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}
Class A Constructor
Class B Constructor
Class C Constructor
8) Private members of a class are inherited to sub class. True or false?
false. Private members are not inherited to sub class.
9) What will be the output of the following program?
class X
{
static void staticMethod()
{
System.out.println("Class X");
}
}
class Y extends X
{
static void staticMethod()
{
System.out.println("Class Y");
}
}
public class MainClass
{
public static void main(String[] args)
{
Y.staticMethod();
}
}
Class Y
10) Below code is showing compile time error. Can you suggest the corrections?
class X
{
public X(int i)
{
System.out.println(1);
}
}
class Y extends X
{
public Y()
{
System.out.println(2);
}
}
Write explicit calling statement to super class constructor in Class Y constructor.
class X
{
public X(int i)
{
System.out.println(1);
}
}
class Y extends X
{
public Y()
{
super(10); //Correction
System.out.println(2);
}
}
11) What is wrong with the below code? Why it is showing compile time error?
public class A
{
public A()
{
System.out.println(1);
super();
System.out.println(2);
}
}
Constructor calling statements ( super() or this() ), if written, must be the first statements in the constructor.Correct Code…
public class A
{
public A()
{
super();
System.out.println(1);
System.out.println(2);
}
}
12) You know that compiler will keep super() calling statement implicitly as a first statement in every constructor. What happens if we write this() as a first statement in our constructor?
Compiler will not keep super() if you are writing this() as a first statement in your constructor.
13) Can you find out the error in the below code?
public class A
{
public A(int i)
{
}
}
class B extends A
{
}
Class B doesn’t have any constructors written explicitly. So, compiler will keep default constructor. In that default constructor, there will be a calling statement to super class constructor (super()). But, it is undefined in Class A.To remove the errors, either define A() constructor in class A or write B() constructor in class B and call super class constructor explicitly.
public class A
{
public A()
{
//Either keep this constructor
}
public A(int i)
{
}
}
class B extends A
{
public B()
{
super(10); //or else write this statement
}
}
14) Which class is a default super class for every class in java?
java.lang.Object class
15) Can you find out the error in the below code?
public class A
{
public A()
{
super();
this(10);
}
public A(int i)
{
System.out.println(i);
}
}
A constructor can have either super() or this() but not both.
16) What will be the output of this program?
class M
{
static
{
System.out.println('A');
}
{
System.out.println('B');
}
public M()
{
System.out.println('C');
}
}
class N extends M
{
static
{
System.out.println('D');
}
{
System.out.println('E');
}
public N()
{
System.out.println('F');
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N();
}
}
A
D
B
C
E
F
17) What will be the output of the below program?
class M
{
int i;
public M(int i)
{
this.i = i--;
}
}
class N extends M
{
public N(int i)
{
super(++i);
System.out.println(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
}
}
27
18) What will be the output of the following program?
class M
{
int i = 51;
public M(int j)
{
System.out.println(i);
this.i = j * 10;
}
}
class N extends M
{
public N(int j)
{
super(j);
System.out.println(i);
this.i = j * 20;
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N(26);
System.out.println(n.i);
}
}
51
260
520
19) Why Line 10 in the below code is showing compilation error?
class X
{
private int m = 48;
}
class Y extends X
{
void methodOfY()
{
System.out.println(m);
}
}
Because, private field ‘m’ is not visible to class Y.
20) What will be the output of the below program?
class X
{
int m = 1111;
{
m = m++;
System.out.println(m);
}
}
class Y extends X
{
{
System.out.println(methodOfY());
}
int methodOfY()
{
return m-- + --m;
}
}
public class MainClass
{
public static void main(String[] args)
{
Y y = new Y();
}
}
1111
2220
21) What will be the output of this program?
class A
{
static String s = "AAA";
static
{
s = s + "BBB";
}
{
s = "AAABBB";
}
}
class B extends A
{
static
{
s = s + "BBBAAA";
}
{
System.out.println(s);
}
}
public class MainClass
{
public static void main(String[] args)
{
B b = new B();
}
}
AAABBB
22) What will be the output of the following program?
class X
{
int i = 101010;
public X()
{
i = i++ + i-- - i;
}
static int staticMethod(int i)
{
return --i;
}
}
class Y extends X
{
public Y()
{
System.out.println(staticMethod(i));
}
}
public class MainClass
{
public static void main(String[] args)
{
Y y = new Y();
}
}
101010
23) What happens if both super class and sub class have a field with same name?
Super class field will be hidden in the sub class.
24) Does the below program execute successfully? If yes, what will be the output?
class A
{
void A()
{
System.out.println(1);
}
}
class B extends A
{
void B()
{
A();
}
}
public class MainClass
{
public static void main(String[] args)
{
new B().B();
}
}
Yes, but with a warning that method has a constructor name. Output will be
1
25) How do you prevent a field or a method of any class from inheriting to sub classes?
By declaring that particular field or method as private.
26) What will be the output of the below program?
class A
{
int i = 1212;
}
class B extends A
{
A a;
public B(A a)
{
this.a = a;
}
}
public class MainClass
{
public static void main(String[] args)
{
A a = new A();
B b = new B(a);
System.out.println(a.i);
System.out.println(b.i);
System.out.println(b.a.i);
b.a.i = 2121;
System.out.println("--------");
System.out.println(a.i);
System.out.println(b.i);
}
}
1212
1212
1212
——–
2121
1212
27) Does java support multiple inheritance?
Yes, but only through interfaces. Classes can implement more than one interfaces but can not extend more than one class.
28) What will be the output of this program?
class ClassOne
{
static int i, j = 191919;
{
--i;
}
{
j++;
}
}
public class ClassTwo extends ClassOne
{
static
{
i++;
}
static
{
--j;
}
public static void main(String[] args)
{
System.out.println(i);
System.out.println(j);
}
}
1
191918
29) Does the fields with default visibility inherited to sub classes outside the package?
No. Fields with default access modifier are inherited to sub classes within the package.
30) Constructors are also inherited to sub classes. True or false?
false. Constructors, SIB and IIB are not inherited to sub classes. But, they are executed while instantiating a subclass.
31) What will be the output of the below program?
class A
{
int[] a = new int[5];
{
a[0] = 10;
}
}
public class MainClass extends A
{
{
a = new int[5];
}
{
System.out.println(a[0]);
}
public static void main(String[] args)
{
MainClass main = new MainClass();
}
}
0
32) What happens if both super class and sub class have a field with same name?
Super class field will be hidden in the sub class.
33) What will be the output of the below program?
class A
{
int methodOfA(int i)
{
i /= 10;
return i;
}
}
class B extends A
{
int methodOfB(int i)
{
i *= 20;
return methodOfA(i);
}
}
public class MainClass
{
public static void main(String[] args)
{
B b = new B();
System.out.println(b.methodOfB(100));
}
}
200
34) What will be the outcome of the following program?
class A
{
static int i;
static
{
i++;
}
{
++i;
}
}
class B extends A
{
static
{
--i;
}
{
i--;
}
}
public class MainClass
{
public static void main(String[] args)
{
System.out.println(new B().i);
}
}
0
35) Can a class be extended by more than one classes?
Yes, A class in java can be extended by multiple classes.
36) Does the below program written correctly? If yes, what will be the output?
public class MainClass
{
public MainClass(int i, int j)
{
System.out.println(method(i, j));
}
int method(int i, int j)
{
return i++ + ++j;
}
public static void main(String[] args)
{
MainClass main = new MainClass(10, 20);
}
}
Yes, class is written correctly. Output will be 31.
37) Does the below code prints “Hi….” on the console? If yes, how many times?
class X
{
static
{
Y.methodOfY();
}
}
class Y extends X
{
static void methodOfY()
{
System.out.println("Hi....");
}
}
public class MainClass
{
public static void main(String[] args)
{
Y.methodOfY();
}
}
Above code prints 2 times “Hi….” on the console.
38) What value the fields ‘i’ and ‘j’ will hold when you instantiate ‘ClassTwo’ in the below code?
class ClassOne
{
static int i = 111;
int j = 222;
{
i = i++ - ++j;
}
}
class ClassTwo extends ClassOne
{
{
j = i-- + --j;
}
}
i = -113
j = 110
39) When you instantiate a sub class, super class constructor will be also executed. True or False?
True
40) Does the below code written correctly? If yes, what will be the output?
class One
{
int x = 2121;
}
class Two
{
int x = 1212;
{
System.out.println(x);
}
}
public class MainClass
{
public static void main(String[] args)
{
Two two = new Two();
}
}
Yes, above code is written correctly. Output will be 1212.
Also Read :
- Classes & Objects Quiz
- Polymorphism Quiz
- Abstract Classes Quiz
- Java Interfaces Quiz
- Java Modifiers Quiz
- Nested Classes Quiz
- Java Enums Quiz
- i++, i– & ++i, –i Quiz
- Java Arrays Quiz
- Java Strings Quiz
- Java Inheritance Oracle Docs

Q 38 ) pls cross-verify the o/p of Q38.
class ClassOne
{
static int i = 111;
int j = 222;
{
i = i++ – ++j;
System.out.println(i);
}
}
class ClassTwo extends ClassOne
{
{
j = i– + –j;
System.out.println(j);
}
}
public class MainClass
{
public static void main(String[] args)
{
ClassTwo two = new ClassTwo();
}
}
o/p-
-112
110
Yes, I am also getting the same out put
-112
110
in question 38, line number 15; that is
j = i– + –j;
value of “i” will get decremented from -112 to -113
so the final value of i is -113
in question 38, line number 15; that is
j = i– + –j;
value of “i” will get same value 111(i-) + (-j) j value will get decremented 221
so the final value is 332
class ClassOne
{
static int i = 111;
int j = 222;
{
i = i++ – ++j; // i = 111(increment after instruction execution) – 223(increment before execution) => i = -112 , j =223
}
}
class ClassTwo extends ClassOne
{
{
j = i– + –j; => j = -112(decrement after execution) + 222(decrement before execution) => j = 110 && i gets decremented after execution => i = -112-1 = -113
}
} Hence i = -113, j=110 at the end
its giving error: non-static variable j cannot be referenced from a static context
System.out.println(“i: ” + i +”\n” + “j: ” + j);
my code :
class ClassOne
{
static int i = 111;
int j = 222;
{
i = i++ – ++j;
}
}
public class ClassTwo extends ClassOne
{
public static void main(String [] args) {
System.out.println(“i: ” + i +”\n” + “j: ” + j);
}
{
j = i– + –j;
}
}
i also gt sm otpt
variable j must be static
static int j=222;
then error will be removed
but output same as given value
output:111
222
becoz your all of instance block is not exicuted. it will be exicute during the class object creation.
if you write this statenment in main method so all of instance block will exicute.
ClassTwo obj1=new ClassTwo();
print the value of i and j in main class
then the output will be
-113
110
how
yes the output is wrong in the answer.
yes,output is wrong.
correct output is
-112 for i
110 for j
For Q.38), output is right.
Try it on any software like netbeans, or can use online compiler.
I am Confused, please correct me.
I believe, Question number 2 is wrong. Because A a = new B() and a.i should be = 20.
Hi pri,
It points to the type of the reference it is referred to, not the type of the reference it is assigned.
In case of overriding of method, it is reverse and it is so called Runtime Method Dispatch
hello divya can you please elaborate?
for eg. what would be the output in the following?
lass Animal{
public void move(){
System.out.println(“Animals can move”);
}
}
class Dog extends Animal{
public void move(){
System.out.println(“Dogs can walk and run”);
}
}
public class TestDog{
public static void main(String args[]){
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();// runs the method in Animal class
b.move();//Runs the method in Dog class
For me, both seems the same. Please enlighten me on this !!!
Hello Ash,
the example u gave was of Run-time polymorphism, when u give reference of subclass to parent class variable and are calling the subclass method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime.
and in question no. 2 he is trying to do the same but Method is overridden not the datamembers, so runtime polymorphism can’t be achieved by data members. Since you are accessing the datamember which is not overridden, hence it will access the datamember of Parent class always.
animals can move
dogs can walk and run
Hi pri
This is variable not method variable not comes under overriding so its work as a overloading
I got the same view on it
i also think so.
the answer is true b/c it is a rule in java when is being created then control goes through parent class if it exist
Variables cannot be overloaded bro…
in 28 question i value not declared but it becomes 1 how is it
i is initialized to 0 in Parent Class and Child Class can access it has default access specifier.
Execution Flow:
All static blocks are executed, since there are no constructor’s initializer blocks are not executed.
please explain the line number 6 of question 20
m = m++;
There is a solution my friend
M=1111 is initialized .
When compiler goes to the statement
M=m++; then the processing of code is
Firstly m++ is a post increament value so it follows first use then change so without changing to1112 it assigns 1111 to M.
Thats y we r getting output 1111.
if you compile only m++ and not assigning this m++ to M then definitely u will get 1112.
hope it helps you.
Q 26) Can enay explain below
b.a.i = 2121;
System.out.println(“——–“);
System.out.println(a.i);
System.out.println(b.i);
Output:
———-
——–
2121
1212
Yes, b’s reference to (A a) actually points to the ‘a’ that was instantiated with this statement A a = new A();.
This statement B b = new B(a); Notice the ‘a’ as an argument to the constructor. b’s b.a actually points to ‘a’. So b.a.i changes a’s int i.
can u explain question no 5
can u plzz explain 20 question
are u say Q20# or total 1-20 questions?
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
This calls the parent class variable. But
class A {
int i = 10;
public void print() {
System.out.println(“class A”);
}
}
class B extends A {
int i = 20;
public void print() {
System.out.println(“class B”);
}
}
public class MainClass {
public static void main(String[] args) {
A a = new B();
a.print();
}
}
It calls the child method. Its like same inheritance. Can anybody tell me how is this?
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
This calls the parent class variable. But
class A {
int i = 10;
public void print() {
System.out.println(“class A”);
}
}
class B extends A {
int i = 20;
public void print() {
System.out.println(“class B”);
}
}
public class MainClass {
public static void main(String[] args) {
A a = new B();
a.print();
}
}
In this case method call is based on actual reference type.And here in this case you are passing the reference of parent class.Where as in another question your are orerriding the print method in its sub-class in that case method cal l is based on actual object passed to it.
why output is 10 instead of 20?
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
Its because when you override a variable it creates separate copy of the variable, one corresponds to superclass , the other one for subclass. So here, a is a superclass reference of the object of type B. So it’s referring to the A portion of the variable.
here the output is 10 instead of 20 because during time of overriding non static data member create their own distinct copy,
class A
{
String s = “Class A”;
}
class B extends A
{
String s = “Class B”;
{
System.out.println(super.s);
}
}
class C extends B
{
String s = “Class C”;
{
System.out.println(super.s);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
System.out.println(c.s);
}
}
View Answer
Answer :
Class A
Class B
Class C
according to me output is
class C
class b
class a
please explain why it’s reverse.
First of all, initialization block is been put inside constructor by compiler. Here default constructor is used by compiler. C c = new C(); –> here C() constructor is called & by default , compiler will call super constructor which is B(), but there is no constructor mentioned, it will call again default constructor for B class & as I said it will put initialization block inside constructor.
So System.out.println(super.s); of class B will be executed first, (super.s) is “Class A”. After this System.out.println(super.s); of class C will be executed so “Class B” will be print Then System.out.println(c.s);–> “Class C”
class X
{
int m = 1111;
{
m = m++;
System.out.println(m);
}
}
class Y extends X
{
{
System.out.println(methodOfY());
}
int methodOfY()
{
return m– + –m;
}
}
public class MainClass
{
public static void main(String[] args)
{
Y y = new Y();
}
}
30) Constructors are also inherited to sub classes. True or false?
Answer : false. Constructors, SIB and IIB are not inherited to sub classes. But, they are executed while instantiating a subclass.
but on question 28) the code is executing without instantiating the class, means SIB is executing and giving output. anyone plz explain me more on this.
SIBs are executed during class loading i.e., when a .class file is loaded but before even a class is instantiated(or not instantiated) during execution time. So in any program all the static blocks will be executed even if an instance is created or not.
Excellent stuff good for java people.
Question 10)
I thought super class constructor will be call automatically.
why it is added super(10).
Can a class be extended by more than one classes?
ans is NO
question no 35 is wrong answer because multiple inheritance not support in java
class A {
……
}
class B extends A{
……..
}
class C extends A{
………….
}
The above code is what the question interprets, that means A can be a parent class for any number of classes. This is example for hierarchical Inheritance.
On the other hand multiple inheritance means a class extending more than one class at the same time i.e.,
For example: class A extends B,C
which creates ambiguity.
class A{
void msg(){System.out.println(“Hello”);}
}
class B{
void msg(){System.out.println(“Welcome”);}
}
class C extends A,B{//suppose if it were
Public Static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}
O/p: Compile time error
The object is created for class C but msg() is not overridden. so compiler throws an error.
no , i think you misunderstand the question,question is “can we use base /parent/super class in more than one class its a hierarical inheritance
|—-c2
|—-c3
c1—-> |—-c4
|—-c5
Can anybody explain me the qstn number 16….as well as qstn 20 output is why 2220 why not 2221……
class M
{
static
{
System.out.println(‘A’);
}
{
System.out.println(‘B’);
}
public M()
{
System.out.println(‘C’);
}
}
class N extends M
{
static
{
System.out.println(‘D’);
}
{
System.out.println(‘E’);
}
public N()
{
System.out.println(‘F’);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N();
}
}
can anyone explain question no. 16?
Please Explain Q 18 ?
public N(int j)
{
super(j);
System.out.println(i);
this.i = j * 20;
When the above code starts executing as a per the Statement;
N n = new N(26);
At this point of time, super(j); will point the execution to
public M(int j)
{
System.out.println(i);
this.i = j * 10;
}
And ‘i’ will be printed which is 51 at that time, then the instance variable ‘i’ will change since its value as per
this.i = j * 10;
where j is 26, which makes the value of i as 260.
So the o/p so far is
51
Now the execution is handed back to the constructor in the child class N constructor as shown below:
public N(int j)
{
super(j);
System.out.println(i);
this.i = j * 20;}
Now value of i at this instance(i.e., 260) is printed.
Next
this.i = j * 20;
is executed and the value of i now is 520.
which is printed when the print statement in the main() method is executed.
in a file the classes like class A and class B are inherited and they are public. And even the main Class also public, then in what name the file should be saved.
import java.io.*;
public class A
{
int i = 10;
}
public class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
Question 17 has compile time error because super class don’t have default constructor.
Can anyone explain the reason of question no-2
class A
{
int i = 10;
}
class B extends A
{
int i = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.i);
}
}
The object a is declared as type A but it will not be aware of the variable i in class B.
if you try to change the declaration as
B a = new B();
Then you will get 20.
Can anyone explain the following code in Q.No-26
System.out.println(b.a.i);
b.a.i = 2121;
System.out.println(“——–“);
System.out.println(a.i);
System.out.println(b.i);
Yes, b’s reference to (A a) actually points to the ‘a’ that was instantiated with this statement A a = new A();.
This statement B b = new B(a); Notice the ‘a’ as an argument to the constructor. b’s b.a actually points to ‘a’. So b.a.i changes a’s int i.
is call by value, call by reference applicable here, or is it part of c++ only?
Hi Rahul,
A a = new B(); this is called casting. This statement tells Child class (B) referencing to parent class (A), so parent class instance varible will be printed. If this code is replaced with
B b = new B();
System.out.println(b.i); // you can see “20” output. here child class instance is referncing to child class variable.
To admin
Answer of Q#2 is wrong .You should correct it. Its correct answer is 20
Answer 10 is correct, overriden is applied only method not on instance variable.
I also thinks it should be 20.
can anyone explain ques 26
Q10) can you please explain the flow of execution
please help explaining 20 detail
please explain q32
please modify the code for Question 1 to make it runnable
I have a query regarding question number 12. As you said “Compiler will not keep super() if you are writing this() as a first statement in your constructor.” Please refer blow code and output
public class Test {
public static void main(String[] args) {
Y y = new Y(10);
}
}
class X
{
X(){
System.out.println(“X class”);
}
}
class Y extends X
{
Y(){
System.out.println(“Y class”);
}
Y(int i){
this();
System.out.println(“Y class : “+ i);
}
}
Actual Output :
X class
Y class
Y class : 10
Expected output :
Y class
Y class : 10
So conclusion is “Compiler will always keep super() whether we are writing this() or not as a first statement in your constructor.”
Please correct me if I did anything wrong.
Anyone explain question 28.
How the value of i will become 1.
Thanks, brother. Nice Article, Great work…!?
class A
{
public A(){
System.out.println(” A”);
}
static
{
System.out.println(“THIRD”);
}
}
class B extends A
{
public B(){
System.out.println(“B”);
}
static
{
System.out.println(“SECOND”);
}
}
class C extends B
{
static
{
System.out.println(“FIRST”);
}
public C(){
System.out.println(“C”);
}
}
public class MainClass
{
public static void main(String[] args)
{
A c = new C();
}
}
Q no 9: Why output is Class Y and why not it is :
Class X
Class Y
Please explain Q20.
Q 21 class A
{
static String s = “AAA”;
static
{
s = s + “BBB”;
}
{
s = “AAABBB”;
}
}
class B extends A
{
static
{
s = s + “BBBAAA”;
}
{
System.out.println(s);
}
}
public class MainClass
{
public static void main(String[] args)
{
B b = new B();
}
how is answer AAABBB . shouldn’t be AAABBBBBBAAA .
When the Object is created B b = new B() ; it calls the default constructor in B which in turns calls the default constructor of A .. where s = AAA , static function of A is executed which makes s as AAABBB then IIB is executed which makes s = AAABBB, then control goes back to Class B where SIB is executed where s = s + “BBBAAA” which makes s as AAABBBBBBAAA and then IIB is executed which prints AAABBBBBBAAA ? Isn’t it correct . Please explain .
Q 21 ) first static methods are executed then non-static so s will get “AAABBBBBBAAA” and then s = “AAABBB” will and then print statement.
for Q20 output is as follows:
1112
2222
you are not taking into consideration the decrement and assignment rules for “–“:
i = m–; // the value of m is decreased after m is assigned to i, so “i” doesn’t change: i = 1111 and m=1110
j= –m; // the value of m is decreased before m is assigned to j, so m = 1110 – 1 = 1109 and then the result will be assigned to j, j = 1109
z = i + j; // thus z = 1111 + 1109 = 2220
Could anyone please explain the execution flow of the program given in Q16?
it’s a nice code
plz explain Question 26 I don’t understand what is b.a.i
Answer of Q31 is 0
Please explain question. 16 I didn’t understand the order of execution.
class M
{
static
{
System.out.println(‘A’);
}
{
System.out.println(‘B’);
}
public M()
{
System.out.println(‘C’);
}
}
class N extends M
{
static
{
System.out.println(‘D’);
}
{
System.out.println(‘E’);
}
public N()
{
System.out.println(‘F’);
}
}
public class MainClass
{
public static void main(String[] args)
{
N n = new N();
}
}
//output : A
D
B
C
E
F
==> We know that static block will be execute before the main ( So A & D will be printed at the top )
==> Now the child class created an object for its own class and its constructor get invoked
A child class constructor has super class as default so parent class constructor invoked and prints B & C, later child class constructors get printed E & F
Hi
Can i have an explanation for number 5 , my answer was Class A only and yet also B and C are printed
class A
{
{
System.out.println(1);
}
}
class B extends A
{
{
System.out.println(2);
}
}
class C extends B
{
{
System.out.println(3);
}
}
public class MainClass
{
public static void main(String[] args)
{
C c = new C();
}
}
Can someone explain why its printing 1 2 3?
the order of excution in java is
static block
instance block
constructor
and any function that is called by an object
instance block example
{
//some code
} above block is called instance block
after C class object declaration there is call to its base class that is B and B is also inherited that is derived class if A so class A is called and the first thing is be executed is static block but there is no static block so instance block is executed of class A, B, C respectively in same order as
1
2
3