在Java中如何模拟多继承
2004-12-01 11:20
597 查看
Simulating Multiple Inheritance in Java
Article Author: Mike Van Atter
Listing 1.1: NextOdd.java
Listing 1.2: NextEven.java
As Java only allows for extending a single class through the
The first step in extending the
Listing 1.3: OddInterface.java
Once we have created
Listing 1.4: OddChild.java
With our implementation of the
In order to be able to call the
Listing 1.5: EvenOdd.java
Unfortunately, because Java does only allow you to extend a single class, you will only be able to cast the
Listing 1.6: Returning a NextOdd instance
In fact, this multiple inheritance limitation is often avoided by creating a factory class with many methods similar to Listing 1.6.
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 all the methods defined in the interface, simply call the corresponding method in the class created in step 2.
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 classes. Unlike C++, Java does not explicitly support multiple inheritance, allowing a class to extend only a single parent class. However, as we will show in this article, it is possible to simulate multiple inheritance, allowing a single class to provide the methods, and the respective implementations, of two parent classes. The strategy that is introduced in this article is also easily extendible to provide inheritance of three or more parent classes.Simulating Multiple Inheritance
In this article we will use a simple example to demonstrate how to simulate multiple inheritance in Java. We will begin with theNextOddand
NextEvenclasses, shown in Listing 1.1 and Listing 1.2 respectively. We will then create a new class, which we will call
EvenOdd, that provides the functionality of both classes.
|
Listing 1.1: NextOdd.java
|
Listing 1.2: NextEven.java
As Java only allows for extending a single class through the
extendskeyword, we will have to provide another manner for extending more than one class. In this example, we will extend the
NextEvenclass by using the
extendskeyword and use a new interface, which we will call
OddInterface, and an implementation of the new interface, which we will call
OddChild, to extend the
NextOddclass.
The first step in extending the
NextOddclass is to define an interface with the same methods as the
NextOddclass, as shown in Listing 1.3. Notice that the parameters, function names, and return values for all methods in the interface must be the same as the original class.
|
Listing 1.3: OddInterface.java
Once we have created
OddInterface, the next step is to create an implementation of
OddInterfacethat also extends the
NextOddclass, as shown in Listing 1.4. By extending the
NextOddclass, which, as previously explained, has all the same method prototypes as
OddInterface, we do not have to implement any of the methods in
OddInterfaceand only have to provide constructors for the new class, which we will call
OddChild. These constructors simply call the constructors of the
NextOddclass using the super() method. The
OddChildclass now provides the exact implementation of all methods of the
NextOddclass, without the developer having to know anything about the way in which
NextOddwas originally implemented.
|
Listing 1.4: OddChild.java
With our implementation of the
OddInterfaceclass, we can now create a class that will extend both the
NextEvenclass and the
NextOddclass. This new class will be called
EvenOddand is shown in Listing 1.5. Because Java allows you to extend only a single class,
EvenOddwill extend the
NextEvenclass and use
OddInterfaceand
OddChildto extend the
NextOddclass.
In order to be able to call the
EvenOdd.getNextOdd()method,
EvenOddwill implement
OddInterfacebecause
OddInterfacehas all the same method prototypes as
NextOdd. This means that we also must provide an implementation of all the
OddInterfacemethods, and as a result all the
NextOddmethods, within
EvenOdd. To ensure these methods have the same implementation as the
NextOddmethods, we will create a private instance of the
OddChildclass, which we will call
oddGenerator, and call the respective
oddGeneratormethod. For example, in the
EvenOdd.getNextOdd()method, we call
oddGenerator.getNextOdd(). The
EvenOddclass now provides the same functionality and implementation of both the
NextOddand
NextEvenclasses.
|
Listing 1.5: EvenOdd.java
Unfortunately, because Java does only allow you to extend a single class, you will only be able to cast the
EvenOddclass to a
NextEvenclass and not to a
NextOddclass as you would be able to if multiple inheritance were directly supported by Java. If you wish to be able to cast an
EvenOddobject to a
NextOddclass, you will have to provide a method for extracting an instance of the
NextOddclass similar to the
getNextOddObj()method in Listing 1.6.
|
Listing 1.6: Returning a NextOdd instance
In fact, this multiple inheritance limitation is often avoided by creating a factory class with many methods similar to Listing 1.6.
Summary
Create an interface with all the same method prototypes as the base class you will 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 all the methods defined in the interface, simply call the corresponding method in the class created in step 2.
相关文章推荐
- Java之我見
- 关于Display.setCurrent()不起作用的原因
- 全面认识UML类图元素
- Java中对文件的操作
- Java人生:J2ee学习流程
- Java 建模:子整体软件开发--宣言
- Java 建模: 子整体软件开发,第二部分--需求收集:工作的恰当过程
- 使用Java技术在Cocoon中实现商业逻辑
- Reading "Thinking in Java" #5
- java学习笔记6--Reusing classes
- iBatis指南、Hibernate指南、Spring开发指南
- 需要在近期内澄清的概念以及需要掌握的方法
- JAVA常用整理(1)--通过java.net.URL类抓取某个网页的内容
- 有关java script调用web servcie
- 线程、并发设计的链接
- Struts:入门资料
- eclipse几个热键
- 正确优雅的解决用户退出问题——JSP和Struts解决方案
- java编码中的一些经验和教训
- Reading "Thinking in Java" #4