What are the four pillars of OOP in Java?
Encapsulation (private fields behind methods), inheritance (
extendsto reuse a class), polymorphism (Animal a = new Dog(); a.sound()runsDog's method) and abstraction (interfaces and abstract classes hide how things work).Back each with one example from your own code.
What is the difference between method overloading and overriding?
Overloading is one name with different parameter lists, chosen at compile time. Overriding is a subclass replacing an inherited method with the same signature, chosen at runtime.
Javaclass Shape { double area() { return 0; } } class Circle extends Shape { double r; Circle(double r) { this.r = r; } @Override double area() { return Math.PI * r * r; } // overriding } public class Main { static String describe(int n) { return "int " + n; } // overloading static String describe(String s) { return "String " + s; } public static void main(String[] args) { Shape s = new Circle(1); System.out.printf("%.2f%n", s.area()); System.out.println(describe(5) + ", " + describe("five")); } }It prints
3.14andint 5, String five. Changing only the return type is not overloading and does not compile. See method overloading.Does creating a subclass object call the parent constructor?
Javaclass Parent { Parent() { System.out.print("Parent "); } } class Child extends Parent { Child() { System.out.print("Child"); } } public class Main { public static void main(String[] args) { new Child(); System.out.println(); } }It prints
Parent Child. A constructor first calls a superclass constructor; without an explicitsuper(...), the compiler insertssuper().If
Parenthad only a constructor with parameters,Childwould needsuper(args)to compile.What happens when a subclass redeclares a field and overrides a method?
Javaclass Animal { String name = "animal"; String sound() { return "..."; } } class Dog extends Animal { String name = "dog"; @Override String sound() { return "woof"; } } public class Main { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.name + " " + a.sound()); } }It prints
animal woof. Methods dispatch on the object's runtime class (Dog), but fields resolve at compile time from the reference type (Animal), so the subclass field only hides the parent's. Keep fields private.Can you override a static method in Java?
Javaclass P { static String who() { return "P"; } } class C extends P { static String who() { return "C"; } } public class Main { public static void main(String[] args) { P p = new C(); System.out.println(p.who()); } }It prints
P. Static methods are hidden, not overridden: the call binds at compile time to the declared type, so this isP.who(). Call static methods through the class name.What is the difference between an abstract class and an interface in Java?
Use an interface for a capability unrelated classes share, and an abstract class to share state and code among related ones.
An abstract class can have instance fields and constructors, and a class extends only one. An interface holds only constants, but a class implements many, and since Java 8 interfaces can carry
defaultandstaticmethods. See abstract classes.Why does Java not support multiple inheritance of classes? How do default methods handle the diamond problem?
To avoid the diamond problem: if two parent classes had the same method, the child's version would be ambiguous. Default methods can still clash between interfaces, so Java forces an override, which can pick one with
Interface.super.method():Javainterface Walker { default String move() { return "walk"; } } interface Swimmer { default String move() { return "swim"; } } class Duck implements Walker, Swimmer { @Override public String move() { // required, or it does not compile return Walker.super.move() + " and " + Swimmer.super.move(); } } public class Main { public static void main(String[] args) { System.out.println(new Duck().move()); } }It prints
walk and swim. A method from a class always beats an interface default.What are the access modifiers in Java?
From most to least open:
public(everywhere),protected(package and subclasses), default with no keyword (package only) andprivate(the class only).A top-level class can only be
publicor package-private. More in access modifiers.What is composition and why is it often preferred over inheritance?
Composition means a class holds another object and delegates to it ("has-a": a
Carhas anEngine) instead of extending it ("is-a").Prefer it because inheritance ties the child to the parent's implementation and exposes every inherited public method, as with
Stack extends Vectorin the JDK; a composed field can also be swapped at runtime. Keep inheritance for true subtypes.How do you create an immutable class in Java?
Make the class
finaland its fieldsprivate final, add no setters, and copy mutable inputs and outputs.Javaimport java.util.ArrayList; import java.util.List; final class Team { private final String name; private final List<String> members; Team(String name, List<String> members) { this.name = name; this.members = List.copyOf(members); // defensive, unmodifiable copy } List<String> members() { return members; } } public class Main { public static void main(String[] args) { List<String> input = new ArrayList<>(List.of("Asha", "Ben")); Team t = new Team("core", input); input.add("Eve"); // does not affect the team System.out.println(t.members()); } }It prints
[Asha, Ben]; the defensive copy is what most candidates forget. Arecord(Java 16) covers the rest, but copy its mutable components in a compact constructor.
OOPs in Java Interview Questions
The four pillars, and where fields and static methods behave differently from instance methods.