How to simulate multiple inheritance in Java

Source: Internet
Author: User
Tags constructor extend generator implement inheritance interface return multiple inheritance in java
Inherit simulating multiple inheritance in Java
Article author:mike Van Atter
From Book:codenotes for Java
Date Published:february 1, 2002
Purpose of multiple inheritance
Multiple inheritance allows a single class to extend two parent classes and thus provide the methods of both parent Classe S. Unlike C + +, Java does not explicitly support multiple inheritance, allowing a class to extend only a single parent clas S. However, as we'll show in this article, it's possible to simulate multiple inheritance, allowing a single class to P Rovide the methods, and the respective implementations, of two parent classes. The strategy is introduced in this article are also easily extendible to provide inheritance of three or more parent C Lasses.

Simulating multiple inheritance
In this article we'll use a simple example to demonstrate you to simulate multiple inheritance in Java. We'll begin with the nextodd and Nexteven classes, shown in Listing 1.1 and Listing 1.2. We'll then create a new class, which we call EVENODD, that provides the functionality of both classes.


Repeated calls to the Getnextodd method would
Return the next
Odd number (i.e. the a call would return 1, the second
Call 3, etc.
public class Nextodd {
The last odd number returned by the Getnextodd method
private int lastodd =-1;

Public nextodd () {
this.lastodd =-1;
}//Nextodd

Selects a different starting point for the odd numbers
Ensures that's starting point are in fact a odd number
Public nextodd (int start) {
This.lastodd = ((int) START/2) * 2 + 1;
}//Nextodd

Retrieves the next odd number
public int getnextodd () {
return lastodd + + 2;
}//GetNext
}//Nextodd




Listing 1.1:nextodd.java


Repeated calls to the Getnexteven method would
Return the
Next even number (i.e the ' I call'll return 0, the
Second call would return 2, etc.
public class Nexteven {
The last even number returned by Getnexteven
private int lasteven =-2;

Public Nexteven () {
This.lasteven =-2;
}//Nexteven

Selects a different starting point for the even numbers
Ensures that's the starting point are in fact a even #
Public Nexteven (int start) {
This.lasteven = ((int) (START/2)) * 2;
}//Constructor

Retrieves the next even number
public int Getnexteven () {
return Lasteven +=2;
}//Getnexteven

}//Nexteven




Listing 1.2:nexteven.java

As Java only allows for extending a single class through the extends keyword, we'll have to provide another for E Xtending more than one class. In this example, we'll extend the Nexteven class by using the extends keyword and use a new interface, which we cal L Oddinterface, and an implementation of the new interface, which we'll call Oddchild, to extend the Nextodd class.

The "the" in extending "Nextodd class is to define a interface with the same as the methods class, as Nextodd WN in Listing 1.3. Notice That's parameters, function names, and return values to all methods in the interface must to be the same as the Ori Ginal class.


Public interface Oddinterface {
public int getnextodd ();
}//Oddinterface




Listing 1.3:oddinterface.java

Once we have created Oddinterface, the next step are to create a implementation of oddinterface that also extends the next Odd class, as shown in Listing 1.4. By extending the Nextodd class, which, as previously explained, has all the same method prototypes as Oddinterface, and we do Not have to implement the "Methods in Oddinterface" and "have to" provide for the new class, constructors we would call Oddchild. These constructors simply call the constructors's Nextodd class using the Super () method. The Oddchild class now provides the exact implementation to all methods of the Nextodd class, without the developer has To know anything about the way in which Nextodd is originally implemented.


public class Oddchild extends nextodd implements
Oddinterface {
Public Oddchild () {
Super ();
}//Constructor

Public oddchild (int start) {
Super (start);
}//Constructor

}//Oddchild




Listing 1.4:oddchild.java

With our implementation of the ' Oddinterface class, we can now create a class that'll extend both the Nexteven class and The Nextodd class. This new class would be called EVENODD and are shown in Listing 1.5. Because Java allows you to extend only a single class, EvenOdd'll extend the Nexteven class and use Oddinterface and ODD Child to extend the Nextodd class.

In order to is able to call the Evenodd.getnextodd () method, EvenOdd'll implement Oddinterface because Oddinterface The same method prototypes as Nextodd. This means so we also must provide an implementation of all oddinterface methods, and as a result all the nextodd me Thods, within EVENODD. To ensure these methods have the same implementation as the nextodd methods, we'll create a private instance of the ODDC Hild class, which we call Oddgenerator, and call the respective Oddgenerator method. For example, in the Evenodd.getnextodd () method, we call Oddgenerator.getnextodd (). The EvenOdd class now provides the same functionality and implementation of the both and nextodd the Nexteven.


public class EvenOdd extends Nexteven implements
Oddinterface {
Public EvenOdd () {
Super ();
Oddgenerator = new Oddchild ();
}//EvenOdd

Initializes the starting point of both the odd # generator
And the Even # generator
Public EvenOdd (int oddstart, int evenstart) {
Super (Evenstart);
Oddgenerator = new Oddchild (Oddstart);
}//EvenOdd

public int getnextodd () {
return oddgenerator.getnextodd ();
}//Getnextodd

Private final Oddinterface Oddgenerator;
}//EvenOdd




Listing 1.5:evenodd.java

Unfortunately, because Java does only allow you to extend a single class, you'll only be able to cast the EvenOdd class To a Nexteven class and is nextodd class as you would is able to if multiple inheritance were directly of supported by Java. If you are wish to is able to cast the EvenOdd object to a Nextodd class, you'll have to provide a method for extracting a I Nstance of the "nextodd class similar to" Getnextoddobj () method in Listing 1.6.


Public nextodd Getnextoddobj () {
Return (nextodd) Oddgenerator;
}//Getnextodd




Listing 1.6:returning a Nextodd instance

In fact, this multiple inheritance limitation are often avoided by creating a factory class with many methods similar to Li Sting 1.6.

Summary

Create a interface with the same method prototypes as the base class for you to be extending.
Create A class that implements the interface created in step 1 and extends the base class.
In the "child class", implement the interface created in step 1 and create a private instance of the class defined in step 2 . In the "All" methods defined in the interface, simply call the corresponding method in the class created in step 2.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.