SlideShare a Scribd company logo
How would you implement multiple
    inheritance in Java?
    Multiple inheritance is the ability to inherit from multiple classes. Java does not have this
    capability.

    However, C++ does support multiple inheritance. Syntactically, multiple inheritance in
    C++ would look like this, where class X derives from 3 classes – A, B, and C:

    class X : public A, private B, public C { /* ... */ };




    Why doesn't the Java language support multiple inheritance?

     Whenever you find yourself asking why Java has or does not have some feature, consider
    the design goals behind the Java language. With that in mind, I started my search by
    skimming through "The Java Language Environment" by James Gosling and Henry McGilton
    (Sun Microsystems), a white paper published in May 1996 that explains some of the
    reasoning behind Java's design.

    As the white paper states, the Java design team strove to make Java:

•    Simple, object oriented, and familiar
•    Robust and secure
•    Architecture neutral and portable
•    High performance
•    Interpreted, threaded, and dynamic




    The reasons for omitting multiple inheritance from the Java language mostly stem from the
    "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a
    language that most developers could grasp without extensive training. To that end, they
    worked to make the language as similar to C++ as possible (familiar) without carrying over
    C++'s unnecessary complexity (simple).

    In the designers' opinion, multiple inheritance causes more problems and confusion than it
    solves. So they cut multiple inheritance from the language (just as they cut operator
    overloading). The designers' extensive C++ experience taught them that multiple inheritance
    just wasn't worth the headache.
Note: For a discussion of the diamond problem, a classic multiple inheritance challenge,
read Bill Venners's "Designing with Interfaces" (JavaWorld, December 1998) and Tony
Sintes's "Java Diamonds Are Forever" (JavaWorld, March 2001).



Instead, Java's designers chose to allow multiple interface inheritance through the use of
interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance
allows an object to inherit many different method signatures with the caveat that the
inheriting object must implement those inherited methods.

Multiple interface inheritance still allows an object to inherit methods and to behave
polymorphically on those methods. The inheriting object just doesn't get an implementation
free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's
"Reveal the Magic Behind Subtype Polymorphism
   When Sun was designing Java, it omitted multiple inheritance - or more precisely multiple
  implementation inheritance - on purpose. Yet multiple inheritance can be useful, particularly
 when the potential ancestors of a class have orthogonal concerns. This article presents a utility
  class that not only allows multiple inheritance to be simulated, but also has other far-reaching
                                            applications.
               Have you ever found yourself wanting to write something similar to:
                       public class Employee extends Person, Employment {
                                          // detail omitted
                                                  }
 Here, Person is a concrete class that represents a person, while Employment is another concrete
class that represents the details of a person who is employed. If you could only put them together,
you would have everything necessary to define and implement an Employee class. Except in Java
- you can't. Inheriting implementation from more than one superclass - multiple implementation
inheritance - is not a feature of the language. Java allows a class to have a single superclass and no
                                               more.
   On the other hand, a class can implement multiple interfaces. In other words, Java supports
               multiple interface inheritance. Suppose the PersonLike interface is:
                                   public interface PersonLike {
                                         String getName();
                                            int getAge();
                                                  }
                                and the EmployeeLike interface is:
                                  public interface EmployeeLike {
                                          float getSalary();
                                    java.util.Date getHireDate();
                                                  }
Introduction

Java was designed without multiple inheritance. While some developers think of this as a
flaw, it is actually true that the overall design of Java supports the solution of problems
commonly solved with multiple inheritance in other ways. In particular, the singly rooted
hierarchy (with Object as the ultimate ancestor of all classes) and Java interfaces solves
most problems that are commonly solved using multiple inheritance in C++.

However, there are a few situations in which multiple inheritance is very helpful. In this
note we will consider one special case and also the general case of multiple inheritance.

Mixin Inheritance

In mixin inheritance, one class is specifically designed to be used as one of the classes in
a multiple inheritance scheme. We say that it provides some functionality that is "mixed
in" to some other class that wants this functionality. Another way to think of mixin
inheritance is that a mixin class is given a new parent class so that the mixin seems to
extend the other class. In some projects it is necessary to rely on common services that
must be provided by several classes. Mixin inheritance is one way to centralize the
development of these services.

To provide for mixin inheritance we will need to define two interfaces as well a at least
one class that provides the service: the Mixin class. In some situations, one of these
interfaces is empty and may then be omitted.

The Interfaces

The first interface (always required) defines what the mixin class will provide for
services. It defines one or more methods that will be implemented by the mixin class. We
will take a simple and abstract example here. The class will be called (abstractly)
MProvides to emphasize that it defines what any compatible mixin must provide. We will
also assume that the only service provided is a void function and we will give it the
abstract name func. In practice, however, there may be any number of methods defined
and they may have any signatures.

interface MProvides
{       void func();
}

One special feature of mixin inheritance that is not usually present in the general multiple
inheritance case is that the mixin class may require services of the class into which it is
mixed. That is, in order to provide the "func" service, the mixin may need to get some
information from the other class. We define this with another interface. We will give it
the abstract name MRequires to indicate that it requires one or more services from the
class into which it is mixed.
Here we will suppose that the compatible mixins require that the other class provides a
method getValue that returns an int.

interface MRequires
{       int getValue();
}


Why multiple inheritance is not allowed
in Java?
For long time I had a question “why Sun introduced Interface concept instead of C++ style of multiple

inheritance?“. I did googling but many articles and forums talks about difference between abstract class

and Interface not why Interface concept required in Java. After extensive search and analysis I came to

know the reason behind the Interface concept in Java.


In C++ multiple inheritance is possible, virtual keyword is been used to define virtual function. Whereas in

Java multiple inheritance is not possible however using Interface concept the same class can represent

different structure.


Lets take an example in C++ where two parent class contains same virtual function.


Example:


       1 class BaseCol
2        {
3        public:
4        virtual void a() { cout << "BaseCol::a" << endl; }
5        virtual void b() { cout << "BaseCol::b" < endl; }
6        };
7
8          class ChildCol1 : public BaseCol
9          {
10         public:
11         virtual void a() { cout << "ChildCol1::a" << endl; }
12         virtual void b() { cout << "ChildCol1::b" << endl; }
13         };
14
15         class ChildCol2 : public BaseCol
16         {
17         public:
18         virtual void a() { cout << "ChildCol2::a" << endl; }
19         virtual void b() { cout << "ChildCol2::b" << endl; }
20         };
21
22         class Row : public ChildCol1, public ChildCol2
23       {
24       public:
25       // i want to overide the b function of ChildCol1 only !!
26       void b() { cout << "Row1::b" << endl; }
27       };


While executing the above code at runtime ambiguity error will occur since the implementation has

conflicting methods. This problem is called Diamond inheritance problem.


To avoid this complexity Java provides single inheritance with interface concept. Since interface cannot

have method implementation, the problem is therefore avoided since there is always only one

implementation to a specific method and hence no ambiguity will arise.

More Related Content

What's hot (20)

Inheritance in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance in Java
Inheritance in Java
Ferdin Joe John Joseph PhD
 
java-06inheritance
java-06inheritance
Arjun Shanka
 
inheritance
inheritance
Mohit Patodia
 
Dacj 2-1 a
Dacj 2-1 a
Niit Care
 
Inheritance in oops
Inheritance in oops
Hirra Sultan
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
baabtra.com - No. 1 supplier of quality freshers
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
OOP java
OOP java
xball977
 
C# classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Object Oriented Programming with C#
Object Oriented Programming with C#
foreverredpb
 
Implementation of oop concept in c++
Implementation of oop concept in c++
Swarup Kumar Boro
 
Unit 3 Java
Unit 3 Java
arnold 7490
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
Pritom Chaki
 
Lecture 2
Lecture 2
emailharmeet
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword in java
TharuniDiddekunta
 
Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Inheritance in java
Inheritance in java
yash jain
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
Uzair Salman
 

Viewers also liked (17)

Access Protection
Access Protection
myrajendra
 
Interfaces in JAVA !! why??
Interfaces in JAVA !! why??
vedprakashrai
 
Inheritance concepts
Inheritance concepts
Kumar
 
Inheritance
Inheritance
ankush_kumar
 
7. Genetics And Inheritance
7. Genetics And Inheritance
rossbiology
 
Inheritance and Polymorphism
Inheritance and Polymorphism
BG Java EE Course
 
Inheritance
Inheritance
Srinath Dhayalamoorthy
 
Genetics - Mendellian Principles of Heredity
Genetics - Mendellian Principles of Heredity
Christine Joyce Javier
 
inheritance c++
inheritance c++
Muraleedhar Sundararajan
 
Inheritance
Inheritance
Selvin Josy Bai Somu
 
Vogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution Method
Kaushik Maitra
 
Packages in java
Packages in java
Abhishek Khune
 
Inheritance
Inheritance
Sapna Sharma
 
Packages and interfaces
Packages and interfaces
vanithaRamasamy
 
Modi method
Modi method
manpreetgrewal
 
Java packages
Java packages
Raja Sekhar
 
Maslow's hierarchy of needs
Maslow's hierarchy of needs
Prathamesh Parab
 
Ad

Similar to How would you implement multiple inheritance in java (20)

ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Java OOPS Concept
Java OOPS Concept
Richa Gupta
 
Intro Java Rev010
Intro Java Rev010
Rich Helton
 
Question and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Core Java Interview Questions PDF By ScholarHat
Core Java Interview Questions PDF By ScholarHat
Scholarhat
 
Java mcq
Java mcq
avinash9821
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
Core_Java_Interview.pdf
Core_Java_Interview.pdf
ansariparveen06
 
Classes and Objects
Classes and Objects
vmadan89
 
Java_notes.ppt
Java_notes.ppt
tuyambazejeanclaude
 
Inheritance in OOPs with java
Inheritance in OOPs with java
AAKANKSHA JAIN
 
20 most important java programming interview questions
20 most important java programming interview questions
Gradeup
 
Structural pattern 3
Structural pattern 3
Naga Muruga
 
2CPP07 - Inheritance
2CPP07 - Inheritance
Michael Heron
 
How to Work with Java Interfaces and Abstract Classes
How to Work with Java Interfaces and Abstract Classes
Naresh IT
 
M.c.a. (sem iv)- java programming
M.c.a. (sem iv)- java programming
Praveen Chowdary
 
Introduction to OOP.pptx
Introduction to OOP.pptx
ParthaSarathiBehera9
 
Object
Object
Mohammad Mahdi Rahimi Moghadam
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developers
Sanjaya Prakash Pradhan
 
C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?
calltutors
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Java OOPS Concept
Java OOPS Concept
Richa Gupta
 
Intro Java Rev010
Intro Java Rev010
Rich Helton
 
Question and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Core Java Interview Questions PDF By ScholarHat
Core Java Interview Questions PDF By ScholarHat
Scholarhat
 
web program-Inheritance,pack&except in Java.ppt
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
Classes and Objects
Classes and Objects
vmadan89
 
Inheritance in OOPs with java
Inheritance in OOPs with java
AAKANKSHA JAIN
 
20 most important java programming interview questions
20 most important java programming interview questions
Gradeup
 
Structural pattern 3
Structural pattern 3
Naga Muruga
 
2CPP07 - Inheritance
2CPP07 - Inheritance
Michael Heron
 
How to Work with Java Interfaces and Abstract Classes
How to Work with Java Interfaces and Abstract Classes
Naresh IT
 
M.c.a. (sem iv)- java programming
M.c.a. (sem iv)- java programming
Praveen Chowdary
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developers
Sanjaya Prakash Pradhan
 
C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?
calltutors
 
Ad

Recently uploaded (20)

Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
“Why It’s Critical to Have an Integrated Development Methodology for Edge AI,...
Edge AI and Vision Alliance
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 

How would you implement multiple inheritance in java

  • 1. How would you implement multiple inheritance in Java? Multiple inheritance is the ability to inherit from multiple classes. Java does not have this capability. However, C++ does support multiple inheritance. Syntactically, multiple inheritance in C++ would look like this, where class X derives from 3 classes – A, B, and C: class X : public A, private B, public C { /* ... */ }; Why doesn't the Java language support multiple inheritance? Whenever you find yourself asking why Java has or does not have some feature, consider the design goals behind the Java language. With that in mind, I started my search by skimming through "The Java Language Environment" by James Gosling and Henry McGilton (Sun Microsystems), a white paper published in May 1996 that explains some of the reasoning behind Java's design. As the white paper states, the Java design team strove to make Java: • Simple, object oriented, and familiar • Robust and secure • Architecture neutral and portable • High performance • Interpreted, threaded, and dynamic The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple). In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.
  • 2. Note: For a discussion of the diamond problem, a classic multiple inheritance challenge, read Bill Venners's "Designing with Interfaces" (JavaWorld, December 1998) and Tony Sintes's "Java Diamonds Are Forever" (JavaWorld, March 2001). Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods. Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride. For an excellent discussion of interface inheritance, read Wm. Paul Rogers's "Reveal the Magic Behind Subtype Polymorphism When Sun was designing Java, it omitted multiple inheritance - or more precisely multiple implementation inheritance - on purpose. Yet multiple inheritance can be useful, particularly when the potential ancestors of a class have orthogonal concerns. This article presents a utility class that not only allows multiple inheritance to be simulated, but also has other far-reaching applications. Have you ever found yourself wanting to write something similar to: public class Employee extends Person, Employment { // detail omitted } Here, Person is a concrete class that represents a person, while Employment is another concrete class that represents the details of a person who is employed. If you could only put them together, you would have everything necessary to define and implement an Employee class. Except in Java - you can't. Inheriting implementation from more than one superclass - multiple implementation inheritance - is not a feature of the language. Java allows a class to have a single superclass and no more. On the other hand, a class can implement multiple interfaces. In other words, Java supports multiple interface inheritance. Suppose the PersonLike interface is: public interface PersonLike { String getName(); int getAge(); } and the EmployeeLike interface is: public interface EmployeeLike { float getSalary(); java.util.Date getHireDate(); }
  • 3. Introduction Java was designed without multiple inheritance. While some developers think of this as a flaw, it is actually true that the overall design of Java supports the solution of problems commonly solved with multiple inheritance in other ways. In particular, the singly rooted hierarchy (with Object as the ultimate ancestor of all classes) and Java interfaces solves most problems that are commonly solved using multiple inheritance in C++. However, there are a few situations in which multiple inheritance is very helpful. In this note we will consider one special case and also the general case of multiple inheritance. Mixin Inheritance In mixin inheritance, one class is specifically designed to be used as one of the classes in a multiple inheritance scheme. We say that it provides some functionality that is "mixed in" to some other class that wants this functionality. Another way to think of mixin inheritance is that a mixin class is given a new parent class so that the mixin seems to extend the other class. In some projects it is necessary to rely on common services that must be provided by several classes. Mixin inheritance is one way to centralize the development of these services. To provide for mixin inheritance we will need to define two interfaces as well a at least one class that provides the service: the Mixin class. In some situations, one of these interfaces is empty and may then be omitted. The Interfaces The first interface (always required) defines what the mixin class will provide for services. It defines one or more methods that will be implemented by the mixin class. We will take a simple and abstract example here. The class will be called (abstractly) MProvides to emphasize that it defines what any compatible mixin must provide. We will also assume that the only service provided is a void function and we will give it the abstract name func. In practice, however, there may be any number of methods defined and they may have any signatures. interface MProvides { void func(); } One special feature of mixin inheritance that is not usually present in the general multiple inheritance case is that the mixin class may require services of the class into which it is mixed. That is, in order to provide the "func" service, the mixin may need to get some information from the other class. We define this with another interface. We will give it the abstract name MRequires to indicate that it requires one or more services from the class into which it is mixed.
  • 4. Here we will suppose that the compatible mixins require that the other class provides a method getValue that returns an int. interface MRequires { int getValue(); } Why multiple inheritance is not allowed in Java? For long time I had a question “why Sun introduced Interface concept instead of C++ style of multiple inheritance?“. I did googling but many articles and forums talks about difference between abstract class and Interface not why Interface concept required in Java. After extensive search and analysis I came to know the reason behind the Interface concept in Java. In C++ multiple inheritance is possible, virtual keyword is been used to define virtual function. Whereas in Java multiple inheritance is not possible however using Interface concept the same class can represent different structure. Lets take an example in C++ where two parent class contains same virtual function. Example: 1 class BaseCol 2 { 3 public: 4 virtual void a() { cout << "BaseCol::a" << endl; } 5 virtual void b() { cout << "BaseCol::b" < endl; } 6 }; 7 8 class ChildCol1 : public BaseCol 9 { 10 public: 11 virtual void a() { cout << "ChildCol1::a" << endl; } 12 virtual void b() { cout << "ChildCol1::b" << endl; } 13 }; 14 15 class ChildCol2 : public BaseCol 16 { 17 public: 18 virtual void a() { cout << "ChildCol2::a" << endl; } 19 virtual void b() { cout << "ChildCol2::b" << endl; } 20 }; 21 22 class Row : public ChildCol1, public ChildCol2
  • 5. 23 { 24 public: 25 // i want to overide the b function of ChildCol1 only !! 26 void b() { cout << "Row1::b" << endl; } 27 }; While executing the above code at runtime ambiguity error will occur since the implementation has conflicting methods. This problem is called Diamond inheritance problem. To avoid this complexity Java provides single inheritance with interface concept. Since interface cannot have method implementation, the problem is therefore avoided since there is always only one implementation to a specific method and hence no ambiguity will arise.