Simulation of multiple inheritance in Java-Tips for extending multiple classes

Source: Internet
Author: User
Tags multiple inheritance in java

Abstract:
Although JAVA does not allow multiple inheritance, it is allowed to be used in some cases. This article describes how to simulate multiple inheritance in a web application.

In general development, Java's single inheritance restrictions generally do not cause any problems. In fact, the use of multiple inheritance often implies poor design. However, there are still some situations where programmers want to inherit multiple classes. Although JAVA does not allow inheritance of multiple classes, some techniques can simulate multiple inheritance.

Me
This technique has been used in both swing and Web applications. Pack and deploy the swing application on the application server. In this case, because I want to drag and drop objects between different components
Gui components share the same drag-and-drop method. In this way, all gui components need to expand two classes: Gui components themselves (jtree or jlist) and general drag-and-drop classes (drag-and-
Drop class ). The technology described in this article simplifies the implementation of drag and drop classes.

To better elaborate on the Multi-inheritance Technology in Java, let's first use it in a web application. In this web application, Servlet classes and other classes need to be extended. My application is simple. It is a text-based information transmission system that can transmit information to another mobile phone through a mobile phone, PDA, or other network terminal devices.

As shown in, the core of this system is a service that can receive information from the client and then pass it to the mobile phone. To simplify client development, I wrote a messageclient class that contains all common methods for communicating with services. This class can be used by all possible terminal devices, which simplifies client development.
The source code of the messageclient class is as follows:

Import
Java. RMI. Naming;


Public
 
Abstract
 
Class
Messageclient


{


Private
Messageserver;



Public
Messageclient ()



{

System. Out. println (
"
Initializing message client
"
);

}




/***/
/**


* Method used to connect to the Message Server

*

*
@ Param
Servername name of the server that contains the Message Server


*/




Protected
 
Void
Connecttoserver ()



{

String servername
=
Getservername ();


Try




{

String name
=
 
"
//
"
 
+
Servername
+
 
"
/Messageserver
"
;

Messageserver
=
(Messageserver) Naming. Lookup (name ));

}




Catch
(Exception E)



{

System. Out. println (
"
Error connecting to message server. Exception is
"
 
+
E );

E. printstacktrace ();

}



}





/***/
/**


* Method used to send message to server

*

*
@ Param
Phonenum phone number to send message

*
@ Param
Message message to send


*/




Public
 
Boolean
Sendmessage (string phonenum, string message)



{


Try




{


Return
(Messageserver. sendmessage (phonenum, message ));

}




Catch
(Exception E)



{

System. Out. println (
"
Error sending message. Exception is
"
 
+
E );

E. printstacktrace ();


Return
(
False
);

}



}





Public
 
Abstract
String getservername ();

}

 
This class contains three methods:
Sendmessage ()
Method to send the actual information to the server;
Connectserver ()
Method is responsible for connecting to the service. In this example, the service is an Rmi service;
Getservername ()
Is an abstract class, because different devices decide the service name in different ways. That is to say, any class inherited from messageclient must implement the getservername () method.

When developing client programs for communications with information services, we need to inherit from each other. Our network client is a simple Servlet used to receive information from the form and send it to the server. To complete the preceding task, the servlet must inheritHttpservlet
Inherit againMessageclient
. Because Java does not allow this inheritance behavior, I allow the primary class to inheritHttpservlet
To Inherit internal classes in the main class.Messageclient
And an instance of the internal class is created for the external class. The Code is as follows:

Public
 
Class
Sendmessageservlet
Extends
Httpservlet

{



Private
Messageclient m_messageclient;


Private
String m_servername;



Public
 
Void
Doget (httpservletrequest request, httpservletresponse response)


Throws
Servletexception, ioexception

{


Try

{


//
Get server name


M_servername
=
Request. getservername ();

System. Out. println (
"
Servername is
"
 
+
M_servername );


//
Create message client to communicate with Message Server


M_messageclient
=
 
New
Servletmessageclient ();

System. Out. println (
"
Created message client
"
);

M_messageclient.connecttoserver ();



//
Get message and phone number


String phonenum
=
(String) request. getparameter (
"
Phonenum
"
);

String message
=
(String) request. getparameter (
"
Message
"
);



//
Send message


M_messageclient.sendmessage (phonenum, message );



//
Display page to tell user message was sent


Response. setcontenttype (
"
Text/html
"
);

Requestdispatcher
=
Getservletcontext (). getrequestdispatcher (
"
/Sendmessageform. jsp
"
);

Dispatcher. Include (request, response );

}

Catch
(Exception E)

{

E. printstacktrace ();

}



}

 



Public
 
Void
Dopost (httpservletrequest request, httpservletresponse response)


Throws
Servletexception, ioexception

{

Doget (request, response );

}





/***/
/**
Inner class used to extend messageclient
*/




Public
 
Class
Servletmessageclient
Extends
Messageclient

{


Public
Servletmessageclient ()

{


Super
();

}




Public
String getservername ()

{

System. Out. println (
"
Returning servername
"
 
+
M_servername );


Return
(M_servername );

}



}



}

 

This
Methods are not actually multi-inheritance, because we use a proxy (for example, messageclient is inherited by many external classes, but not the external class itself), but the effect is the same. Although
Messageclient can be extended by a separate class, but an internal class is used to allow it to access all members and methods in an external class. This makes interaction between two classes easy.

This example only extends two classes. Of course you can use this technology to expand any class.

Of course, in fact, this information transmission service example can be implemented without multiple inheritance. If messageclient has a constructor that can receive the service name, The getservername () method is not abstract. This means that the Web client does not have to inherit the messageclient class.

Finally, developers should be reminded that they should use the above technology only when there is a clear reason, and be careful when using it. Multi-inheritance makes the design complex and easily misused.

Related Article

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.