Inheritance and interface in Java work together to structure object-oriented code. Inheritance (extends) creates an IS-A relationship so a child class reuses a parent’s state and behaviour. An interface (implements) creates a CAN-DO contract promising certain methods exist. A class can extend only one parent but implement unlimited interfaces simultaneously.
- Key Takeaway 1: A Java class can extend only one parent class but implement multiple interfaces at the same time.
- Key Takeaway 2: Use
extendsfor shared state and behaviour; useimplementsfor shared capability promises. - Key Takeaway 3: Combining both in a single class declaration is legal, common and the basis of most real-world Java frameworks.
- Key Takeaway 4: Interface inheritance (one interface extending another) lets you build layered contracts without touching any class.
- Key Takeaway 5: Getting this right matters in interviews at product companies and in day-to-day code reviews across Indian IT firms.
Class Inheritance Recap
When one class extends another, it inherits all non-private fields and methods. The child class is said to have an IS-A relationship with the parent. A Dog IS-A Animal. That relationship is real and permanent for the lifetime of the design.
Java enforces single inheritance for classes. You cannot write class Dog extends Animal, Pet. This is a deliberate language decision, not a limitation you can work around with clever syntax. It keeps the class hierarchy predictable and prevents the diamond problem that plagued C++.
What extends actually gives you
The child class inherits the parent’s instance variables, regular methods and constructors (indirectly, via super()). It can override methods to change behaviour. It can also call the parent version using super.methodName() when it needs to extend rather than replace the logic.
A common mistake beginners make at this stage: they override a method but forget to call super() in the constructor, which causes the parent’s initialisation to be skipped entirely. That silent bug shows up as a NullPointerException at runtime. If you have hit that before, the guide on Java exceptions and common runtime errors explains exactly why it happens and how to trace it.
IS-A in practice
The IS-A test is a quick design check. Before writing extends, ask: “Is a [Child] always a [Parent]?” If the answer is ever “sometimes” or “kind of”, inheritance is the wrong tool. A Manager IS-A Employee passes the test. A Car IS-A Engine fails it. Applying this test consistently is one of the clearest signs of a developer who understands inheritance and interface in Java at a design level, not just a syntax level.
Interfaces as Contracts
An interface is a named set of method signatures (and, since Java 8, default and static methods) that any implementing class must honour. The keyword is implements. The relationship is CAN-DO, not IS-A. A Dog CAN-DO Trainable things. A Car CAN-DO Serializable things. Neither of those tells you what the object fundamentally is.
Because interfaces carry no instance state, Java lets a single class implement as many interfaces as it needs. This is the standard workaround for single-class-inheritance. You get the flexibility of multiple behavioural contracts without the diamond-problem risk. According to the JetBrains State of Developer Ecosystem 2023, 48% of Java developers work on projects that use Java 11 or later, where interface default methods are a standard design tool used in production codebases worldwide.
Interface inheritance
Interfaces can extend other interfaces using the same extends keyword. This is called interface inheritance. If Flyable extends Movable, then any class implementing Flyable must provide all methods from both interfaces. You are building a hierarchy of contracts, not a hierarchy of implementations.
This pattern is widely used in the Java standard library. List extends Collection, which extends Iterable. Any class implementing List is bound by all three contracts automatically. Understanding inheritance and interface in Java at this level is what lets you read framework source code without getting lost.
Why interfaces matter for design
Interfaces enforce the Interface Segregation Principle from SOLID design: do not force a class to implement methods it does not need. Instead of one fat interface with 15 methods, you write three focused interfaces with 5 methods each. Classes pick up only what applies to them. This keeps code testable, replaceable and easier to review.
According to the Stack Overflow Developer Survey 2024, Java remains one of the top five most-used programming languages globally, and object-oriented design principles like these are consistently cited as core skills employers test in technical interviews. If you are building toward a career in software, the BSc Computer Science subjects and scope overview covers where OOP fits in your full curriculum.
Combining extends and implements
Here is the part most tutorials skip over too quickly. A Java class can use both keywords in a single declaration when applying inheritance and interface in Java together. The syntax rule is simple: extends comes first, implements comes after, and you can list multiple interfaces separated by commas.
| Keyword | Used between | Limit | Example |
|---|---|---|---|
extends (class) |
Class and parent class | One parent only | class ElectricCar extends Car |
implements |
Class and interface(s) | Unlimited | implements Chargeable, Serializable |
extends (interface) |
Interface and parent interface(s) | Unlimited | interface Flyable extends Movable |
Order matters syntactically. Writing implements Chargeable extends Car throws a compile-time error. The JVM expects extends before implements every time.
What the compiler actually checks
When you declare class ElectricCar extends Car implements Chargeable, the compiler checks three things: that Car is a non-final class, that Chargeable is an interface (not a class), and that ElectricCar provides a concrete method body for every abstract method in Chargeable. Miss any one of those and you get a compile-time error, not a runtime surprise.
This is actually a good thing. Catching design violations at compile time rather than runtime is one of Java’s genuine strengths over loosely typed languages. It is one reason enterprise applications in Indian IT services firms, from banking systems at companies like Infosys and TCS to logistics platforms, are still heavily built in Java. Platforms like IRCTC and several Aadhaar-integrated services also rely on Java’s strict type system for reliability at scale.
Common mistake: confusing the two keywords
New developers sometimes write class Dog implements Animal when Animal is a class, not an interface. The error message is clear, but the confusion is about design, not syntax. If Animal needs shared state (like a name field), make it a class and use extends. If it is purely a behavioural contract, make it an interface. This is one of the most common inheritance and interface in Java mistakes flagged in code reviews at Indian product companies.
Interface vs Abstract Class: A Common Interview Question
A question that comes up in almost every Java interview in India is: when do you use an interface versus an abstract class? An abstract class can hold instance state, constructors and concrete methods. An interface (before Java 8) could not. Since Java 8, interfaces support default and static methods, and since Java 9, private methods too. The practical rule: use an abstract class when subclasses share state. Use an interface when you only need to share a behavioural contract. When in doubt, prefer the interface because it keeps your design more flexible.
Design Example
Say you are building a simple simulation for a university project. You have vehicles. Some vehicles are electric. Some can be driven autonomously. Here is how inheritance and interface in Java split the responsibility cleanly.
The class hierarchy
You define a parent class Vehicle with shared state: brand, speed and a method move(). Then Car extends Vehicle and adds car-specific behaviour. ElectricCar extends Car. That is your IS-A chain.
The interface contracts
You define two interfaces separately: Chargeable with a method charge(int units), and Autonomous with a method selfDrive(String destination). Not every car needs both. A basic electric car needs Chargeable. A self-driving electric car needs both.
Putting it together
Your final declaration looks like this: class SelfDrivingEV extends ElectricCar implements Chargeable, Autonomous. The class inherits all vehicle and car behaviour through the chain. It must provide concrete implementations for charge() and selfDrive(). If it does not, the compiler refuses to build.
This design is clean because each piece has one job. The inheritance chain handles shared state. The interfaces handle optional capabilities. Swap out the Autonomous implementation for a different one later and nothing in the Vehicle or Car hierarchy breaks. This is the practical payoff of understanding inheritance and interface in Java as complementary tools.
Comparison: inheritance vs interface at a glance
| Feature | Class Inheritance (extends) | Interface (implements) |
|---|---|---|
| Relationship type | IS-A | CAN-DO |
| Multiple allowed? | No (one parent class) | Yes (unlimited interfaces) |
| Can hold state? | Yes (fields, constructors) | No instance fields (constants only) |
| Default method bodies? | Yes (always) | Yes (since Java 8, via default keyword) |
| Constructor inherited? | Yes (via super()) | No |
| Best for | Shared structure and behaviour | Shared capability contracts |
The TIOBE Index (April 2024) ranks Java at number four among all programming languages globally, behind Python, C and C++. That ranking has been stable for over a decade, which tells you the demand for solid Java OOP knowledge is not going anywhere. According to data from Naukri.com (Q1 2025), Java developer roles account for over 18% of all software engineering job listings in India, making it the single largest language-specific hiring category on the platform. If you are considering where Java fits in a broader career path, check the breakdown of jobs and salary after MCA to see how Java skills map to specific roles and pay bands.
One practical tip from real code reviews: if you find yourself writing the same method signature in three different unrelated classes, that is a strong signal you should extract it into an interface. Do not wait until a senior dev points it out in a pull request.
If you are new to programming fundamentals and want to understand how compiled languages work before going deeper into Java’s type system, the walkthrough on how to write, compile and run a C program gives you a solid mental model for what happens between source code and execution. And when you are ready to think about where Java OOP fits in a full professional toolkit, the guide to software development tools to learn first is worth reading alongside this one.
For career context: whether you are heading toward a full stack developer or software developer role, strong OOP fundamentals in Java come up in technical screening rounds at almost every product and service company in India.
Frequently Asked Questions
How do inheritance and interfaces differ in Java?
Inheritance (extends) creates an IS-A relationship where the child class reuses the parent’s state and behaviour. An interface (implements) creates a CAN-DO contract that promises certain methods exist without sharing any implementation. Understanding the difference between inheritance and interface in Java is fundamental to writing well-structured object-oriented code. A class can extend only one parent but implement as many interfaces as needed.
Can a class extend and implement at the same time in Java?
Yes. A Java class can both extend a parent class and implement one or more interfaces in a single declaration. The syntax is class Child extends Parent implements InterfaceA, InterfaceB. The extends clause must always come before the implements clause or the code will not compile. This combination is the standard pattern for applying inheritance and interface in Java together.
When should you use interface over inheritance in Java?
Use an interface when you need to define a capability that multiple unrelated classes should share, without forcing them into a common class hierarchy. Use inheritance when classes genuinely share state and core behaviour. If you are unsure, prefer the interface because it keeps your design more flexible and easier to test. This is the most common inheritance and interface in Java design decision you will face in real projects.
What is interface inheritance in Java?
Interface inheritance is when one interface extends another using the extends keyword. The child interface inherits all the method signatures of the parent interface. Any class that implements the child interface must provide concrete implementations for all methods in both the child and parent interfaces. This is how Java builds layered contracts in its standard library.
Can you give a combined example of inheritance and interfaces in Java?
Yes. Declare class SelfDrivingEV extends ElectricCar implements Chargeable, Autonomous. Here, SelfDrivingEV inherits all fields and methods from ElectricCar (and through it, from Car and Vehicle), and it must also provide concrete bodies for charge() from Chargeable and selfDrive() from Autonomous. The compiler enforces all of this at build time. This is the clearest real-world demonstration of inheritance and interface in Java working together.
What is the difference between an abstract class and an interface in Java?
An abstract class can hold instance fields, constructors and concrete methods. An interface holds only method signatures, constants and (since Java 8) default and static methods. A class can extend only one abstract class but implement multiple interfaces. Use an abstract class for shared state; use an interface for shared behavioural contracts. Both are central to understanding inheritance and interface in Java at an advanced level.
The core point is this: inheritance and interface in Java are not competing ideas. They are complementary tools for the same goal, which is writing code that is easy to extend, test and hand to the next developer without a lengthy explanation. Use extends when two things genuinely share structure. Use implements when a class needs to promise a capability. Combine them freely when a class is genuinely both.
- This week: Take any Java project you have already written and identify one class that is doing too many things. Extract its secondary responsibilities into a new interface and implement it explicitly.
- Next: Read the Oracle Java tutorial on Interfaces and Inheritance to see the official examples side by side with the concepts covered here.
- Then: Explore 3.0 University online certification courses in Cybersecurity, Ethical Hacking, AI and Blockchain. Strong Java OOP is directly relevant to secure coding, writing smart contracts and building AI-integrated applications. If ethical hacking interests you, the CEH v13 AI online course builds on programming fundamentals like these to teach you how real-world systems are attacked and defended.
Last updated: June 2025. Reviewed by the 3University editorial team.