BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Does Java support Multiple inheritance?

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

When one class extends more than one classes then this is called multiple inheritance. For example: Class C extends class A and B then this type of inheritance is known as multiple inheritance. Java doesn’t allow multiple inheritance. In this article, we will discuss why java doesn’t allow multiple inheritance and how we can use interfaces instead of classes to achieve the same purpose.

Multiple Inheritance

Why Java doesn’t support multiple inheritance?

C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem that occurs in multiple inheritance.

To understand the basics of inheritance, refer this main guide: Inheritance in Java

What is diamond problem?
We will discuss this problem with the help of the diagram below: which shows multiple inheritance as Class D extends both classes B & C. Now lets assume we have a method in class A and class B & C overrides that method in their own way. Wait!! here the problem comes – Because D is extending both B & C so if D wants to use the same method which method would be called (the overridden method of B or the overridden method of C). Ambiguity. That’s the main reason why Java doesn’t support multiple inheritance.
Diamond Problem

Can we implement more than one interfaces in a class

Yes, we can implement more than one interfaces in our program because that doesn’t cause any ambiguity(see the explanation below).

interface X
{
   public void myMethod();
}
interface Y
{
   public void myMethod();
}
class JavaExample implements X, Y
{
   public void myMethod()
   {
       System.out.println("Implementing more than one interfaces");
   }
   public static void main(String args[]){
	   JavaExample obj = new JavaExample();
	   obj.myMethod();
   }
}

Output:

Implementing more than one interfaces

As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in interface itself.

❮ PreviousNext ❯

Top Related Articles:

  1. Multithreading in java with examples
  2. Abstract Class in Java with example
  3. Constructor Overloading in Java with examples
  4. Java – Static Class, Block, Methods and Variables
  5. Java Functional Interfaces

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Sanjay says

    February 19, 2015 at 3:53 PM

    Thanks, I have a question.
    1. Why do we need interfaces anyway? Couldn’t we just declare and define myMethod() inside Demo class? I don’t see the point of multiple inheritance as both interfaces have empty abstract methods.
    2. Can you give me a real example which we really need interfaces in real life.
    3. Are there any real-world examples where we really need interfaces and things that we can’t accomplish without them?

    Reply
    • Kaamakshi Sharma says

      April 24, 2015 at 5:55 AM

      real life interface is also called abstraction because as in interface we only declare methods and variable not defining it any way .
      consider that you r going from half day leave so in order to inform to your friends you will say that i m going to my home not describing that what you will do at home this is similar to your interface…

      Reply
    • MONIS AZHAR says

      July 10, 2016 at 7:55 AM

      We need interface because it gives order to must implement its requirements.
      Example:
      If you have 2 shops, one shop is required ISO-9000 certifiction then you’ll just inherit its class with ISO-9000 interface in order to fullfill their requirements. Where ISO-9000 interface contains all the requirements which one must have to implement before got certified.
      That is why one class can inherit with multiple interfaces because your shop can have multiple certifications.
      I hope now you’ll understand the use of interfaces.

      Reply
  2. Shashikumar says

    March 2, 2015 at 1:43 PM

    I just have one question.
    As a real time example can any one show us the use of interfaces to inherit all the features Class “Applet” & Class “Swing”.

    Reply
  3. anji says

    September 8, 2015 at 9:58 AM

    I just have one question.
    In above example Demo class myMethod function,Which interface has been utilized to override.

    Reply
  4. Joseph says

    November 1, 2015 at 8:34 AM

    What if interface x and interface y’s methods have different return types? Apparently it’s legal because it’s not a part of the method signature. How would you deal with that?

    Reply
    • Husoski says

      September 8, 2016 at 7:39 PM

      It’s legal for two different interfaces to have methods with the same name and arguments, but different return types. However, if that’s the case, it’s impossible for any class to implement both interfaces.

      Reply
  5. muralidhar says

    November 17, 2015 at 7:24 AM

    how can we say multiple inheritance supported in java at interface level. if we implement two or more interfaces into our class with same variables in all interfaces . I got error doing this. give me example.

    Reply
  6. Somnath Pagar says

    May 11, 2016 at 4:58 PM

    Nice explanation.
    I have question,
    Some people says interface supports only syntax of multiple inheritance does not supports implementation of multiple inheritance
    Is it true?if yes then how?

    Reply
  7. amrish yadav says

    January 20, 2017 at 7:52 AM

    i have a 1 question how multiple inheritance is suspended in java please give me answer

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap