Java Inheritance — Complete Guide for Interviews
Inheritance is one of the most important concepts of Object-Oriented Programming.
Java Inheritance — Complete Guide for Interviews

Inheritance is one of the most important concepts of Object-Oriented Programming.
You will almost certainly come across inheritance-related questions in Java interviews.
But interviewers usually don’t stop at:
“What is inheritance?”
They may ask about constructors, access modifiers, method overriding, super, multiple inheritance, initialization order, static methods, fields, and runtime polymorphism.
So let’s understand inheritance step by step.
What is Inheritance in Java?
Inheritance in Java is used to extend an existing class by adding additional properties and functionalities.
It allows us to reuse the functionality of an existing class instead of writing the same code again.
The existing class is called the parent class / superclass, and the new class is called the child class / subclass.
Inheritance is implemented using the extends keyword.
Example
Consider an organization.
We have an Employee class:
class Employee {
String name;
double salary;
void work() {
System.out.println("Employee is working");
}
}
Now suppose we want to create a Manager.
A manager is also an employee, but a manager has some additional responsibilities.
Instead of writing the name, salary and work() functionality again, we can extend Employee.
class Manager extends Employee {
void conductMeeting() {
System.out.println("Manager is conducting a meeting");
}
}
Now we can write:
Manager manager = new Manager();
manager.work();
manager.conductMeeting();
Output:
Employee is working
Manager is conducting a meeting
Here, Manager gets the accessible functionality of Employee and adds its own functionality.
This represents an IS-A relationship.
Manager IS-A Employee
Why do we use Inheritance?
There are mainly three reasons to use inheritance.
1. Code Reusability
Common functionality can be written once in the parent class and reused by child classes.
2. Extending Existing Functionality
A child class can add its own properties and methods.
3. Runtime Polymorphism
Inheritance allows us to use a child object through a parent reference.
Employee employee = new Manager();
We will understand this in detail later.
Types of Inheritance in Java
There are different forms of inheritance.
1. Single Inheritance
One child class extends one parent class.
Employee
↓
Manager
class Manager extends Employee {
}
2. Multilevel Inheritance
One class extends another class, and another class extends that class.
Employee
↓
Manager
↓
SeniorManager
class Manager extends Employee {
}
class SeniorManager extends Manager {
}
Now SeniorManager can use accessible functionality from both Manager and Employee.
3. Hierarchical Inheritance
Multiple child classes extend the same parent class.
Employee
/ \
/ \
Developer Manager
class Developer extends Employee {
}
class Manager extends Employee {
}
Does Java Support Multiple Inheritance?
Java does not support multiple inheritance through classes.
This is not allowed:
class A {
}
class B {
}
class C extends A, B { // ❌ Compilation error
}
Why?
One major reason is ambiguity.
Suppose both A and B have a method called show().
class A {
void show() {
System.out.println("A");
}
}
class B {
void show() {
System.out.println("B");
}
}
If C could extend both:
A B
\ /
\ /
C
Then what should happen when we call:
C obj = new C();
obj.show();
Should Java call A.show() or B.show()?
This creates ambiguity.
Therefore, Java does not allow a class to extend multiple classes.
Can Java achieve Multiple Inheritance?
Yes, through interfaces.
interface Payment {
void pay();
}
interface Refund {
void refund();
}
class CreditCardPayment implements Payment, Refund {
public void pay() {
System.out.println("Payment completed");
}
public void refund() {
System.out.println("Refund completed");
}
}
A class can implement multiple interfaces.
class CreditCardPayment implements Payment, Refund {
}
This is one of the common interview questions:
Can a Java class extend multiple classes?
No.
Can a Java class implement multiple interfaces?
Yes.
What Members Are Accessible Through Inheritance?
This is an area where interviewers often ask follow-up questions.
Consider:
class Employee {
private double salary;
String name;
protected String department;
public int id;
}
The access depends on the access modifier.
Modifier Access from child class
**private →**Not directly accessible
**default →** Accessible within the same package
**protected →**Accessible to subclasses, with package rules
**public →**Accessible wherever the class is accessible
For example:
class Manager extends Employee {
void display() {
// salary; ❌ private
System.out.println(name); // depends on package
System.out.println(department); // protected
System.out.println(id); // public
}
}
Important interview point
Do not simply say:
“Private members are not inherited.”
A better interview answer is:
Private members are not directly accessible from the child class because they belong to the parent class’s private implementation. The parent portion of the object still exists, and the parent can expose access through methods such as getters.
This distinction is useful in senior-level interviews.
Are Constructors Inherited?
No.
Constructors belong to the class in which they are defined.
However, when a child object is created, the parent constructor executes first.
Consider:
class Employee {
Employee() {
System.out.println("Employee constructor");
}
}
class Manager extends Employee {
Manager() {
System.out.println("Manager constructor");
}
}
Now:
Manager manager = new Manager();
Output:
Employee constructor
Manager constructor
Why?
Because the parent part of the object must be initialized before the child part.
What is super()?
When we create a child object, the child constructor needs to call a parent constructor.
We can explicitly do this using super().
class Employee {
Employee(String name) {
System.out.println("Employee: " + name);
}
}
class Manager extends Employee {
Manager() {
super("Anushka");
System.out.println("Manager constructor");
}
}
Here:
super("Anushka");
calls the constructor of the parent class.
Important rule
A call to super() must be the first statement in a constructor.
You cannot write:
Manager() {
System.out.println("Manager");
super("Anushka"); // ❌
}
What If the Parent Has No No-Argument Constructor?
This is a very common interview trap.
class Employee {
Employee(String name) {
}
}
class Manager extends Employee {
Manager() {
}
}
This will not compile.
Why?
If you don’t explicitly call a parent constructor, Java tries to insert:
super();
But Employee doesn't have a no-argument constructor.
So we need:
class Manager extends Employee {
Manager() {
super("Anushka");
}
}
Remember
If the parent class does not have an accessible no-argument constructor, the child constructor must explicitly call an available parent constructor.
Can a Child Override a Parent Method?
Yes.
This is called method overriding.
Suppose every employee has a work() method.
class Employee {
void work() {
System.out.println("Employee is working");
}
}
A developer may work differently:
class Developer extends Employee {
@Override
void work() {
System.out.println("Developer is writing code");
}
}
Now:
Employee employee = new Developer();
employee.work();
Output:
Developer is writing code
This is called runtime polymorphism.
Why Did Developer.work() Execute?
Look at this statement:
Employee employee = new Developer();
There are two types involved.
Reference type → Employee
Object type → Developer
For an overridden instance method, Java uses the actual object type at runtime.
The actual object is:
Developer
Therefore:
employee.work();
calls:
Developer.work()
This is called dynamic method dispatch.
Very Important Interview Trap: Fields Are Different
Consider:
class Employee {
String role = "Employee";
}
class Manager extends Employee {
String role = "Manager";
}
Now:
Employee employee = new Manager();
System.out.println(employee.role);
Output:
Employee
Many candidates expect:
Manager
But fields are not overridden like instance methods.
Field access is based on the reference type.
Here:
Reference type = Employee
Therefore:
employee.role
refers to Employee.role.
Remember this table
MemberDecided byOverridden instance methodRuntime object typeFieldReference typeStatic methodCompile-time/reference typePrivate methodNot overridden
This is one of the most useful tables to remember before a Java interview.
Can Static Methods Be Overridden?
No.
Static methods belong to the class, not to an individual object.
If a child declares a static method with the same signature, it is called method hiding, not overriding.
class Employee {
static void showRole() {
System.out.println("Employee");
}
}
class Manager extends Employee {
static void showRole() {
System.out.println("Manager");
}
}
Now:
Employee employee = new Manager();
employee.showRole();
Output:
Employee
This is because static method calls are resolved using the reference type at compile time.
Can Private Methods Be Overridden?
No.
A private method is not visible to the child class.
class Employee {
private void calculateSalary() {
System.out.println("Employee salary");
}
}
class Manager extends Employee {
private void calculateSalary() {
System.out.println("Manager salary");
}
}
The calculateSalary() methods here are two separate methods.
The child method does not override the parent method.
Can Final Methods Be Overridden?
No.
class Employee {
final void calculateSalary() {
}
}
The child cannot override it.
class Manager extends Employee {
// ❌ Compilation error
// void calculateSalary() {}
}
Similarly, a final class cannot be extended.
final class Employee {
}
class Manager extends Employee { // ❌
}
Method Overriding Rules
These rules are extremely important for interviews.
1. Method signature must be compatible
The child method must correctly override the parent method.
2. Access level cannot be reduced
For example:
Parent:
public void work()
The child cannot have:
protected void work() // ❌
But this is allowed:
Parent:
protected void work()
Child:
public void work() // ✅
3. Return type can be covariant
A child can return a more specific type.
class Employee {
Employee getEmployee() {
return this;
}
}
class Manager extends Employee {
@Override
Manager getEmployee() {
return this;
}
}
Manager is a subtype of Employee, so this is allowed.
4. Checked exceptions have restrictions
The child cannot throw a broader checked exception than the parent method.
For example:
class Employee {
void work() throws java.io.IOException {
}
}
The child cannot do:
class Manager extends Employee {
@Override
void work() throws Exception { // ❌
}
}
But it can throw a more specific checked exception.
Upcasting
Upcasting means assigning a child object to a parent reference.
Developer developer = new Developer();
Employee employee = developer;
This is safe because:
Developer IS-A Employee.
It is also commonly used for runtime polymorphism:
Employee employee = new Developer();
Downcasting
Downcasting means converting a parent reference back to a child reference.
Employee employee = new Developer();
Developer developer = (Developer) employee;
This works because the actual object is a Developer.
But consider:
Employee employee = new Manager();
Developer developer = (Developer) employee;
This causes:
ClassCastException
because the actual object is a Manager.
Important
Casting the reference does not change the actual object.
What Happens During Object Creation?
This is another area where interviewers can go deeper.
Consider:
class Employee {
static {
System.out.println("Employee static block");
}
{
System.out.println("Employee instance block");
}
Employee() {
System.out.println("Employee constructor");
}
}
class Manager extends Employee {
static {
System.out.println("Manager static block");
}
{
System.out.println("Manager instance block");
}
Manager() {
System.out.println("Manager constructor");
}
}
When we execute:
new Manager();
the initialization follows the class hierarchy.
The static initialization of the class hierarchy happens first when the classes are initialized, and during object creation the parent instance initialization happens before the child instance initialization.
For a first-time creation in this example, the output is:
Employee static block
Manager static block
Employee instance block
Employee constructor
Manager instance block
Manager constructor
This is a very good output-based interview question.
Inheritance and Object
Every Java class ultimately inherits from java.lang.Object, unless it has another superclass.
So:
class Employee {
}
is effectively:
class Employee extends Object {
}
This is why every normal Java object has methods such as:
toString()
equals()
hashCode()
getClass()
Understanding this becomes especially important when studying:
equals()andhashCode()toString()- Collections
- Polymorphism
Inheritance vs Composition
This is an important question for experienced developers.
Suppose we have:
class Car extends Engine {
}
This is usually incorrect.
Why?
A car is not an engine.
A car has an engine.
So composition is more appropriate:
class Car {
private Engine engine;
}
Therefore:
Inheritance → IS-A
Composition → HAS-A
Examples:
Manager IS-A Employee
Developer IS-A Employee
Car HAS-A Engine
Order HAS-A Payment
House HAS-A Room
A common design principle is:
Prefer composition over inheritance when inheritance does not represent a strong and stable IS-A relationship.
Common Interview Traps 🚨
Q1. Are constructors inherited?
No.
Q2. Can constructors be overridden?
No.
Q3. Can static methods be overridden?
No. They are hidden.
Q4. Can private methods be overridden?
No.
Q5. Can final methods be overridden?
No.
Q6. Can a class extend multiple classes?
No.
Q7. Can a class implement multiple interfaces?
Yes.
Q8. Does the child constructor execute before the parent constructor?
No. Parent initialization happens first.
Q9. Are fields overridden?
No. Fields are hidden.
Q10. Does casting change the actual object?
No.
Interview Output Question 🔥
What is the output?
class Employee {
int salary = 50000;
static void showRole() {
System.out.println("Employee");
}
void work() {
System.out.println("Employee working");
}
}
class Manager extends Employee {
int salary = 100000;
static void showRole() {
System.out.println("Manager");
}
@Override
void work() {
System.out.println("Manager working");
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Manager();
System.out.println(employee.salary);
employee.showRole();
employee.work();
}
}
Answer
50000
Employee
Manager working
Why?
employee.salary
→ field
→ reference type
→ Employee
employee.showRole()
→ static method
→ reference type
→ Employee
employee.work()
→ overridden instance method
→ runtime object
→ Manager
If you understand this output without guessing, you understand one of the most important differences between inheritance, overriding and polymorphism in Java.
Quick Revision — Inheritance
Before an interview, make sure you can explain:
- What is inheritance?
- Why do we use inheritance?
- What is an IS-A relationship?
- What are the types of inheritance?
- Why doesn’t Java support multiple class inheritance?
- Can a class implement multiple interfaces?
- Are constructors inherited?
- What is
super()? - What happens when the parent has only parameterized constructors?
- What happens during object creation?
- What is method overriding?
- What is runtime polymorphism?
- What is dynamic method dispatch?
- What is upcasting?
- What is downcasting?
- What is field hiding?
- What is method hiding?
- Can private methods be overridden?
- Can static methods be overridden?
- Can final methods be overridden?
- What are the rules of method overriding?
- What are covariant return types?
- What is the relationship between inheritance and
Object? - Inheritance vs composition?
Final Takeaway
The simplest way to remember inheritance is:
Inheritance
↓
Reuse existing functionality
↓
Extend it with new functionality
↓
Establish an IS-A relationship
↓
Enables runtime polymorphism
But for interviews, remember these three lines especially:
Instance method → Runtime object
Field → Reference type
Static method → Reference/compile-time type
These three rules can solve many of the tricky inheritance questions asked in Java interviews.
Java Interview Revision Series #01 — Inheritance
Next: Polymorphism in Java
If you’re preparing for Java/backend interviews, save this article for revision.
메타데이터
- post_id
- bc00aef9fe57
- slug
- java-inheritance-complete-guide-for-interviews-bc00aef9fe57
- url
- https://medium.com/@anushkagulhane1/java-inheritance-complete-guide-for-interviews-bc00aef9fe57
- canonical_url
- https://medium.com/@anushkagulhane1/java-inheritance-complete-guide-for-interviews-bc00aef9fe57
- author_url
- https://medium.com/@anushkagulhane1
- status
- ok
- fetched_at
- 2026-08-25 04:43:20