In this article
Classes, objects and encapsulation
What are the four principles of object-oriented programming (OOP)?
They are encapsulation, inheritance, polymorphism and abstraction. Encapsulation hides an object's state behind its methods, inheritance lets a class reuse and extend another class, polymorphism lets one call behave differently depending on the object, and abstraction exposes what something does while hiding how (Oracle object-oriented concepts lesson). The entries below take each in turn.
Why it is asked: it is the usual opener, and a clear one-line definition of each sets the tone. Related: the coding interviews hub. Read the full explanation: Oracle tutorial on object-oriented concepts.
What is the difference between a class and an object in Java?
A class is a blueprint from which objects are created, and an object is a software bundle of related state and behavior, in the wording of Oracle's Java Tutorials. State lives in fields and behavior in methods. One class can produce many objects, each with its own field values.
Why it is asked: it is the opening definition check, and vague answers show up fast. Read the full explanation: Oracle tutorial on object-oriented concepts.
What is encapsulation, and how do access modifiers support it?
Encapsulation is hiding an object's internal state behind a small set of methods, so the implementation can change without breaking callers. Java has four access levels: public, protected, package-private (no modifier) and private. The Oracle tutorial on access control advises using "the most restrictive access level that makes sense" and avoiding public fields except for constants.
Why it is asked: it separates memorised terms from design judgment. Read the full explanation: Oracle tutorial on access control.
Who can see a protected member?
A protected member is visible inside its own package and to subclasses of its class in other packages. A member with no modifier is visible only inside its package, and a private member only inside its own class. A top-level class can only be public or package-private (Oracle access control tutorial).
Why it is asked: the package-private level is the one candidates forget. Read the full explanation: Oracle tutorial on access control.
What is a package, and why does Java have them?
A package is a namespace for organising classes and interfaces in a logical manner, according to the Oracle object-oriented concepts lesson. Packages prevent name clashes between classes with the same simple name and set the boundary for package-private access.
Why it is asked: it links naming, organisation and visibility in one answer. Read the full explanation: Oracle tutorial on object-oriented concepts.
What is a nested class, and how do static nested and inner classes differ?
A nested class is a class defined inside another class. An inner class belongs to an instance of the outer class and can read its private members directly, while a static nested class belongs to the outer class itself and reaches instance members only through an object reference (Oracle nested classes tutorial). Use nesting when a class is useful to only one other class.
Why it is asked: it tests whether you know what a hidden reference to the outer object costs. Read the full explanation: Oracle tutorial on nested classes.
Inheritance
What does a subclass inherit?
A subclass inherits the public and protected members of its parent, and package-private members only when it sits in the same package. It does not inherit private members, and constructors are not inherited, though a subclass constructor can invoke one (Oracle inheritance tutorial). Public or protected accessors still reach private fields.
Why it is asked: a precise answer shows you know constructors are not members. Read the full explanation: Oracle tutorial on inheritance.
Composition versus inheritance: when is it is-a and when is it has-a?
Inheritance models an is-a relationship: a subclass is a kind of its parent. Composition models has-a: a class holds another object in a field and uses it. Prefer inheritance only when the subclass can truly stand in for the parent everywhere, and composition when you only want to reuse behavior, because a subclass is tied to its parent's implementation (Oracle inheritance tutorial for the is-a wording; the preference is a common design guideline, not a Java rule).
Why it is asked: it shows design judgment beyond definitions. Read the full explanation: Oracle tutorial on inheritance.
Why does Java not allow a class to extend two classes?
Java allows one superclass to avoid multiple inheritance of state. The Oracle tutorial asks what happens if methods or constructors from different superclasses instantiate the same field, and which takes precedence. A class can still implement many interfaces, which gives multiple inheritance of type, and interface default methods give a limited form of inherited implementation (Oracle multiple inheritance tutorial).
Why it is asked: it checks whether you understand the reason, not just the rule. Read the full explanation: Oracle tutorial on multiple inheritance.
What does super do, and what happens if you never call it in a constructor?
super(...) invokes a superclass constructor. Up to Java 24 it had to be the first statement of the subclass constructor; since Java Development Kit (JDK) 25 (Java Enhancement Proposal (JEP) 513, Flexible Constructor Bodies, delivered in release 25, read 2026-10-24) statements that do not use the object under construction may come before it. If you write no explicit call, the compiler inserts a call to the superclass's no-argument constructor, and a compile-time error follows if none exists (Oracle super tutorial). The chain runs up to the Object constructor. super.method() calls the superclass version of an overridden method, and super.field reaches a hidden field.
Why it is asked: constructor chaining is a common source of compile errors. Read the full explanation: Oracle tutorial on super.
What does final mean on a class, a method and a variable?
A final method cannot be overridden and a final class cannot be subclassed. The Oracle tutorial names the String class as the example of a final class used to keep a class immutable, and it advises that methods called from constructors should generally be final (Oracle final tutorial). A final variable can be assigned only once.
Why it is asked: it separates three meanings of one keyword. Read the full explanation: Oracle tutorial on final.
How do upcasting, downcasting and instanceof work?
Assigning a subclass object to a superclass variable is implicit upcasting. Going back down needs an explicit cast, which inserts a runtime check and throws an exception if the object is not of that type. Testing with instanceof first avoids the exception (Oracle inheritance tutorial).
Why it is asked: it tests whether you know the compiler checks the variable type and the runtime checks the object. Read the full explanation: Oracle tutorial on inheritance.
Knowing it is half the job. Saying it is the other half.
Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.
Start Free Trial →Polymorphism
What is polymorphism, and how does Java implement it?
Polymorphism is the ability of subclasses to define their own behavior while sharing the functionality of the parent class. The Java virtual machine (JVM) calls the method of the object a variable refers to, not the method defined by the variable's type; the tutorial calls this virtual method invocation (Oracle polymorphism tutorial). In a run on Java 20.0.1, an Animal variable holding a Cat printed Cat.instance.1
Why it is asked: it is the core object-oriented idea, and interviewers want the mechanism, not the slogan. Read the full explanation: Oracle tutorial on polymorphism.
What is the difference between overriding and overloading?
Overriding replaces an inherited instance method with the same signature and return type, and the subclass version runs at runtime. Overloading adds a method with the same name and different parameters, and neither hides nor overrides the original (Oracle overriding tutorial). Overloads are chosen by the declared argument types: a String held in an Object variable selected the Object overload in our run.1
Why it is asked: candidates mix up the two, and this is the quickest check. Read the full explanation: Oracle tutorial on overriding.
Can you override a static method?
No. A static method with the same signature in a subclass hides the superclass method, and the version called depends on the reference type at the call site. The Oracle tutorial's example prints the superclass static method and the subclass instance method for the same objects (Oracle overriding tutorial). Our run reproduced it: the static call printed Animal.static.1
Why it is asked: it exposes whether you know static methods belong to classes. Read the full explanation: Oracle tutorial on overriding.
What rules apply when you override a method?
The overriding method must keep the signature, may return a subtype (a covariant return type), and may allow more access but not less. A protected method can become public, not private. Changing an instance method to static, or the reverse, is a compile-time error (Oracle overriding tutorial). The @Override annotation makes the compiler check that the method really overrides.
Why it is asked: it tests the details that cause real compile errors. Read the full explanation: Oracle tutorial on overriding.
Abstraction and interfaces
Abstraction means exposing what an object does while hiding how it does it. In Java it is expressed through abstract classes and interfaces.
What is the difference between an abstract class and an interface?
Neither can be instantiated, and both can mix methods with and without bodies. An abstract class can hold non-static, non-final fields and methods of any access level, while interface fields are always public, static and final. A class extends one class but implements any number of interfaces (Oracle abstract classes tutorial).
Why it is asked: it is the most common Java design question. Read the full explanation: Oracle tutorial on abstract classes.
When would you choose an abstract class over an interface?
Choose an abstract class to share code and state among closely related classes, or when subclasses need non-public members. Choose an interface when unrelated classes will implement it, as with Comparable and Cloneable, or when you want multiple inheritance of type (Oracle abstract classes tutorial). Real libraries use both: HashMap implements interfaces and extends AbstractMap.
Why it is asked: it shows whether you design from the relationship rather than habit. Read the full explanation: Oracle tutorial on abstract classes.
What are default methods, and what happens when two interfaces define the same one?
A default method is an interface method with a body, added so libraries can extend interfaces without breaking existing implementers (Oracle default methods tutorial). If a class inherits the same default from two unrelated interfaces, it must override the method and may pick one with InterfaceName.super.method() (Oracle multiple inheritance tutorial).
Why it is asked: it probes how Java handles the diamond ambiguity. Read the full explanation: Oracle tutorial on default methods.
Object equality
What is the difference between == and equals?
The == operator on references is true only when both operands refer to the same object or are both null, while equals compares whatever the class defines; for String it is the same character sequence (Java Language Specification, section 15.21.3, String class documentation). In our run two new String("hi") objects gave false for == and true for equals.1
Why it is asked: the classic first Java trap. Read the full explanation: HashMap versus HashSet.
What is the equals and hashCode contract?
equals must be reflexive, symmetric, transitive and consistent, and x.equals(null) must be false. If two objects are equal, their hashCode values must be equal, and unequal objects need not have distinct hash codes (Object class documentation). Override hashCode whenever you override equals.
Why it is asked: breaking it silently corrupts collections. Read the full explanation: HashMap versus HashSet.
What happens if you override equals but not hashCode?
Equal objects can land in different buckets of a hash-based collection, so lookups miss them. In our run, 1,000 keys of a class overriding only equals were stored in a HashMap, and looking each up with a new equal key found 0 of 1,000; the same class with hashCode as well, equal keys found 1,000 of 1,000.1
Text description of the diagram
- A key class that overrides only equals found 0 of 1,000 equal keys in a HashMap.
- A key class that overrides equals and hashCode found 1,000 of 1,000.
Why it is asked: it shows you can predict a bug, not just state a rule. Read the full explanation: HashMap versus HashSet, and see how interview loops place such rounds.
Why is String immutable and final?
String objects are constant: their values cannot be changed after creation, and because they are immutable they can be shared. String is declared final, so no subclass can break that property (String class documentation, Oracle final tutorial).
Why it is asked: it ties immutability to safe sharing. Read the full explanation: Oracle tutorial on final.
Related reading: the same coding interviews hub lists the other rounds, industry coding interviews cover the practical rounds, and system design interviews show where object design meets architecture.
- A class is a blueprint and an object is a bundle of state and behavior; use the most restrictive access level that works.
- Java allows one superclass, many interfaces, and resolves conflicting defaults by an explicit override.
- Instance methods are overridden and chosen at runtime; static methods are hidden and chosen by the variable's type.
- Override hashCode whenever you override equals: in our run the lookup found 0 of 1,000 keys without it.
- Pick an abstract class to share state, an interface to define a role for unrelated classes.
Sources
- Lesson: Object-Oriented Programming Concepts (The Java Tutorials) — Oracle (page shows no date; copyright 1995-2024; read 2026-10-24), 24 Oct 2026
- Controlling Access to Members of a Class (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Overriding and Hiding Methods (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Polymorphism (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Abstract Methods and Classes (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Default Methods (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Multiple Inheritance of State, Implementation, and Type (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Writing Final Classes and Methods (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Using the Keyword super (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- JEP 513: Flexible Constructor Bodies — OpenJDK (status Closed/Delivered, release 25; read 2026-10-24), 24 Oct 2026
- Inheritance (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Nested Classes (The Java Tutorials) — Oracle (page shows no date; read 2026-10-24), 24 Oct 2026
- Chapter 15, Expressions: Reference Equality Operators (Java Language Specification, Java SE 21) — Oracle (read 2026-10-24), 24 Oct 2026
- Object (Java SE 21 API documentation) — Oracle (read 2026-10-24), 24 Oct 2026
- String (Java SE 21 API documentation) — Oracle (read 2026-10-24), 24 Oct 2026