SlideShare a Scribd company logo
1 of 5
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

What's hot (20)

Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
OOP java
OOP javaOOP java
OOP java
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \Object Orinted Programing(OOP) concepts \
Object Orinted Programing(OOP) concepts \
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 

Viewers also liked

Viewers also liked (17)

Access Protection
Access ProtectionAccess Protection
Access Protection
 
Interfaces in JAVA !! why??
Interfaces in JAVA !!  why??Interfaces in JAVA !!  why??
Interfaces in JAVA !! why??
 
Inheritance concepts
Inheritance concepts Inheritance concepts
Inheritance concepts
 
Inheritance
InheritanceInheritance
Inheritance
 
7. Genetics And Inheritance
7. Genetics And Inheritance7. Genetics And Inheritance
7. Genetics And Inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Genetics - Mendellian Principles of Heredity
Genetics - Mendellian Principles of HeredityGenetics - Mendellian Principles of Heredity
Genetics - Mendellian Principles of Heredity
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Vogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution MethodVogel's Approximation Method & Modified Distribution Method
Vogel's Approximation Method & Modified Distribution Method
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Modi method
Modi methodModi method
Modi method
 
Java packages
Java packagesJava packages
Java packages
 
Maslow's hierarchy of needs
Maslow's hierarchy of needsMaslow's hierarchy of needs
Maslow's hierarchy of needs
 

Similar to How would you implement multiple inheritance in java

ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxkristinatemen
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS ConceptRicha Gupta
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objectsvmadan89
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with javaAAKANKSHA JAIN
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questionsGradeup
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - InheritanceMichael Heron
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programmingPraveen Chowdary
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developersC#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developersSanjaya Prakash Pradhan
 
C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?calltutors
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 

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

ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Java mcq
Java mcqJava mcq
Java mcq
 
Core_Java_Interview.pdf
Core_Java_Interview.pdfCore_Java_Interview.pdf
Core_Java_Interview.pdf
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
Java_notes.ppt
Java_notes.pptJava_notes.ppt
Java_notes.ppt
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
20 most important java programming interview questions
20 most important java programming interview questions20 most important java programming interview questions
20 most important java programming interview questions
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programming
 
Introduction to OOP.pptx
Introduction to OOP.pptxIntroduction to OOP.pptx
Introduction to OOP.pptx
 
Object
ObjectObject
Object
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developersC#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developers
 
C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?C++ vs Java: Which one is the best?
C++ vs Java: Which one is the best?
 
Java_presesntation.ppt
Java_presesntation.pptJava_presesntation.ppt
Java_presesntation.ppt
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Java 06
Java 06Java 06
Java 06
 

Recently uploaded

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

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.