In this tutorial, you’ll learn 30+ scenario-based Java OOPs interview questions and answers covering constructors, inheritance, static vs instance variables, polymorphism, and overriding.
These are the exact kind of tricky questions interviewers ask to test your deep understanding of OOP concepts. Each example includes code, output, and explanation — so you’ll never get confused again!
1. Constructor Call Order in Inheritance
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
public static void main(String[] args) {
new Child();
}
}
Output:
Parent constructor
Child constructor
Explanation: The parent constructor always executes before the child’s constructor because super() is implicitly called as the first line inside the child’s constructor.
2. Parameterized Constructor in Parent
class Parent {
Parent(String msg) {
System.out.println("Parent: " + msg);
}
}
class Child extends Parent {
Child() {
super("Hello");
System.out.println("Child constructor");
}
public static void main(String[] args) {
new Child();
}
}
Output:
Parent: Hello
Child constructor
3. Missing super() Call (Compilation Error)
class Parent {
Parent(String msg) {}
}
class Child extends Parent {
Child() {} // ❌ Compilation error
}
Error: constructor Parent in class Parent cannot be applied to given types;
Explanation: Since the parent does not have a no-arg constructor, the child must explicitly call super("msg").
4. Instance Block and Constructor Order
class A {
{ System.out.println("A instance block"); }
A() { System.out.println("A constructor"); }
}
class B extends A {
{ System.out.println("B instance block"); }
B() { System.out.println("B constructor"); }
public static void main(String[] args) {
new B();
}
}
Output:
A instance block
A constructor
B instance block
B constructor
5. Static Block and Instance Block Order
class A {
static { System.out.println("A static block"); }
{ System.out.println("A instance block"); }
}
class B extends A {
static { System.out.println("B static block"); }
{ System.out.println("B instance block"); }
public static void main(String[] args) {
new B();
}
}
Output:
A static block
B static block
A instance block
B instance block
6. Static Variable Hiding
class A {
static int x = 10;
}
class B extends A {
static int x = 20;
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}
Output:
10
7. Instance Variable Hiding
class A { int x = 10; }
class B extends A { int x = 20; }
public class Test {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}
Output:
10
8. Method Overriding with Upcasting
class A {
void show() { System.out.println("A"); }
}
class B extends A {
void show() { System.out.println("B"); }
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
Output:
B
9. Constructor Overloading
class Student {
Student() { System.out.println("Default constructor"); }
Student(String name) { System.out.println("Parameterized: " + name); }
public static void main(String[] args) {
new Student();
new Student("Ashish");
}
}
Output:
Default constructor
Parameterized: Ashish
10. Static vs Instance Method Overriding
class A {
static void print() { System.out.println("Static A"); }
}
class B extends A {
static void print() { System.out.println("Static B"); }
public static void main(String[] args) {
A obj = new B();
obj.print();
}
}
Output:
Static A
11. Super Keyword in Overriding
class A { void show() { System.out.println("Parent"); } }
class B extends A {
void show() {
super.show();
System.out.println("Child");
}
public static void main(String[] args) { new B().show(); }
}
Output:
Parent
Child
12. Object Initialization using this()
class Demo {
Demo() {
this(10);
System.out.println("Default");
}
Demo(int x) {
System.out.println("Parameterized");
}
public static void main(String[] args) {
new Demo();
}
}
Output:
Parameterized
Default
13. Using both this() and super() (Error)
class A { A() {} }
class B extends A {
B() {
this();
super(); // ❌ Error
}
}
Error: Constructor call must be the first statement in a constructor.
14. Instance Count using Static Variable
class Counter {
static int count = 0;
Counter() { count++; }
public static void main(String[] args) {
new Counter();
new Counter();
System.out.println(count);
}
}
Output:
2
15. Static Block vs Main Method
class Test {
static { System.out.println("Static block"); }
public static void main(String[] args) {
System.out.println("Main method");
}
}
Output:
Static block
Main method
16. Constructor Chaining
class Chaining {
Chaining() {
this(5);
System.out.println("Default Constructor");
}
Chaining(int x) {
System.out.println("Parameterized Constructor");
}
public static void main(String[] args) {
new Chaining();
}
}
Output:
Parameterized Constructor
Default Constructor
17. Final Variable Initialization
class Demo {
final int x;
Demo() {
x = 100;
System.out.println("Final variable = " + x);
}
public static void main(String[] args) {
new Demo();
}
}
Output:
Final variable = 100
18. Final Method Cannot be Overridden
class A {
final void display() { System.out.println("Final Method"); }
}
class B extends A {
// void display() {} // ❌ Compile-time error
}
Explanation: Final methods cannot be overridden to preserve consistent behavior.
19. Abstract Class Constructor Call
abstract class A {
A() {
System.out.println("Abstract class constructor");
}
}
class B extends A {
B() {
System.out.println("Child class constructor");
}
public static void main(String[] args) {
new B();
}
}
Output:
Abstract class constructor
Child class constructor
20. Interface Default and Static Methods
interface I {
default void show() { System.out.println("Default Method"); }
static void display() { System.out.println("Static Method"); }
}
class Demo implements I {
public static void main(String[] args) {
new Demo().show();
I.display();
}
}
Output:
Default Method
Static Method
21. Covariant Return Type Example
class A {
A get() { return this; }
}
class B extends A {
@Override
B get() { return this; }
public static void main(String[] args) {
new B().get();
System.out.println("Covariant type allowed");
}
}
Output:
Covariant type allowed
22. Private Method Overriding (Trick)
class A {
private void show() { System.out.println("Private A"); }
}
class B extends A {
void show() { System.out.println("Public B"); }
public static void main(String[] args) {
new B().show();
}
}
Output:
Public B
Explanation: Private methods are not inherited, so the method in B is a new one, not an override.
23. Static vs Instance Memory Allocation
class Demo {
static int staticVar = 100;
int instanceVar = 50;
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.instanceVar = 200;
d1.staticVar = 300;
System.out.println(d2.instanceVar); // ?
System.out.println(d2.staticVar); // ?
}
}
Output:
50
300
Explanation: Each object has its own copy of instance variables, but static variables are shared across all objects.
24. Overloading vs Overriding
class Example {
void show(int a) { System.out.println("int"); }
void show(String a) { System.out.println("String"); }
}
class Child extends Example {
@Override
void show(String a) { System.out.println("Child String"); }
public static void main(String[] args) {
Example e = new Child();
e.show("Hi");
e.show(5);
}
}
Output:
Child String
int
25. Order of Static and Instance Execution with Inheritance
class Parent {
static { System.out.println("Parent static"); }
{ System.out.println("Parent instance"); }
Parent() { System.out.println("Parent constructor"); }
}
class Child extends Parent {
static { System.out.println("Child static"); }
{ System.out.println("Child instance"); }
Child() { System.out.println("Child constructor"); }
public static void main(String[] args) {
new Child();
}
}
Output:
Parent static
Child static
Parent instance
Parent constructor
Child instance
Child constructor
26. Accessing Static Methods Using Object Reference
class Demo {
static void display() { System.out.println("Static method"); }
public static void main(String[] args) {
Demo d = new Demo();
d.display(); // Allowed but not recommended
}
}
Output:
Static method
Explanation: Static methods belong to the class, not the object — but can still be called using an object reference.
27. Multiple Constructors with Super and This
class A {
A() { System.out.println("Parent constructor"); }
}
class B extends A {
B() { this(5); System.out.println("Default constructor"); }
B(int x) { System.out.println("Parameterized constructor"); }
public static void main(String[] args) {
new B();
}
}
Output:
Parent constructor
Parameterized constructor
Default constructor
28. Static Block Execution Without main()
class Example {
static {
System.out.println("Static executed before main");
System.exit(0);
}
}
Output:
Static executed before main
29. Polymorphism with Constructor Call (Trick)
class A {
A() { print(); }
void print() { System.out.println("A"); }
}
class B extends A {
int x = 10;
void print() { System.out.println(x); }
public static void main(String[] args) {
new B();
}
}
Output:
0
Explanation: The constructor of A calls print() before B’s variables are initialized — hence prints 0, not 10.
30. Object Reference and Garbage Collection
class Demo {
Demo() { System.out.println("Constructor called"); }
protected void finalize() { System.out.println("Object destroyed"); }
public static void main(String[] args) {
Demo d = new Demo();
d = null;
System.gc();
}
}
Output:
Constructor called
Object destroyed
Conclusion
These 30+ Java OOPs scenario-based interview questions cover every tricky area — from constructor order to static hiding, polymorphism, overriding, and inheritance behavior.
Practice them to master the flow of object creation, memory allocation, and method execution order — and you’ll confidently handle any OOPs question in interviews!
Next Step: Try modifying examples and predicting outputs before running — this builds real confidence and deep understanding.
0 Comments