DatagramPacket

CrazyOne
CrazyOne used Ask the Experts™
on
I am interested in creating a class based on the DatagramPacket object. I want it to behave just as if it is a DatagramPacket, and I would like to add a few fields/methods to the object. Is there a way to do this?

Actually I am pretty sure there is a way, I Just don't remember how to do it.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
public class mydatagram extends DatagramPacket { }

Hope this helps

Scobber

Author

Commented:
Unfortunately when compile it gives this error

cannot inherit from final DatagramPacket
Top Expert 2013
Commented:
You can create a class and add an instance of DatagramPacket object as a member of the class, like this:
public class mydatagram
{
public DatagramPacket dgp;
}

Open in new window

You will add additional members to your mydatagram class.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Top Expert 2016

Commented:
public class mydatagram

Open in new window

That would be OK but Java class names begin upper case
Well I don't think it is to be recommended. DatagramPacket is a final class anyway - which is why it cannot be extended. You would want to handle dgps as discrete entities in a for-real app., and avoid the muddle of mixing in your own digressions. You could get away with what chaau suggests, but why do you think you need to do this?
Top Expert 2016

Commented:
why do you think you need to do this?
Is actually a good question
When I said " . . . you could get away with . . . ", your DatagramPacket is in any case always going to be a member of some class or other. How else would you hold a reference to it, if  not in a class?

Author

Commented:
Sorry for the delay, got busy.

I find it rather annoying to get an email from EE stating "Your attention is requested on your question" after only 3 days of inactivity. Seems a little pushy to me. I know it is probably built into the sites code, but nonetheless it felt insulting to me.
It's a bit similar to how I feel in a sense. You don't mention why you want to create a class that upstages DatagramPacket, when I explicitly said it is a waste of resource to do so. (And can't be done in any case).

Author

Commented:
chaau provided a way to work around not being able to extend the DatagramPacket and it was sufficient for the project I was working on. It allowed me to add a couple of fields I needed to attach to the packet for purposes I would rather not expand upon.
Top Expert 2013

Commented:
Strangely then why you have not marked my answer as a solution then.
for purposes I would rather not expand upon.
Lol. CIA?

Errrrr . . . you'd have to include a DGP in some class or other, unless your name is Merlin. :) Hardly a "workaround", but I'll have to try it myself now I know. And so why didn't you choose his answer?
Top Expert 2016

Commented:
Now i'm getting confused ...
Now that the question is closed, and will be, or has been, archived for later consultation, I feel I should make the point for future readers of this question, that it is entirely pointless - indeed impossible -  to attempt to extend DatagramPacket,  and  / or to think that you can enhance its functionality in any way from a Java perspective.

DatagramPackets should be created new per required packet payload; its fields and methods are the correct way to interrogate and load and send and receive it, all via a DatagramSocket.

And of course you can only manipulate one from a class object, in which you declare a DGP. So how this question was ever resolved by simply stating that one of these objects should be in a class is astonishing. But true.
@CrazyOne -

I am not being ironic when I say that looking at your impressive certificates here, I see (surprisingly) that Java isn't amongst them.  So I would draw your attention to the fact that in Java, everything is written in a class. That being the case, and a DatagramPacket being a class that cannot be extended, in order to use it, you would have to make it a member of one of your classes, and that is the reason why the answer you have accepted, although it is correct in itself declarationally, is not a workaround nor some form of quasi-extendability hack. All you have done is to include another field in your class - but if you were not aware that Java works like this, then, presumably, you are still in the dark?
Top Expert 2013
Commented:
@krakatoa: BTW, there are cases where you would want to have a "complete" datagram class with some additional fields. E.g. you are collecting these datagrams from different sources and somehow queue them for later processing. The simplest thing would be not to parse them straight away but put them to a List or similar collection. But because you are collecting them from different sources you would want to add some identifiers. This is when it will be useful to encapsulate them within another class.

Author

Commented:
Yeah I did choose the wrong expert please reassign the points to chaau. Sorry for the confusion, and chaau's last comment hit the nail on the head. I think it is worth another 500 points. Sometimes one has to do what the client asks, and that was the case here. Thanks chaau. BTW the packet I needed the extra fields for was when it was received from multiple connections, it had nothing to do with sending out a packet. chaau's suggestion, although may not be the perfect solution, it was simple and an easy work around for what I needed.

Author

Commented:
krakatoa if I may, say a little something. Please be cautious when being forth coming with your thoughts. Your last comment borders on the line of being a bit rude and insulting. Given the fact that I screwed up with who I awarded the points to, I can understand  the comment was probably directly related to the answer originally chosen, which was not the one I intended to choose.

I get the passion, I really do. I was the first expert to reach 2 million and 3 million points on this site. I was knocking on the door of being the first expert to reach 4 million points, before I stopped participating as an expert. I was the expert with the most points on this site for a long time. So I do get it.

Hmm I just looked at your profile, we became members of EE about the same time, cool.
@chauu ID: 40052163

This is completely wrong.

All that you are doing is queuing packets in another object, which itself is a member of your class. You comment is misleading, and there is no way that anyone should run away with the idea that you have somehow done anything with the packet that can't be done with any other object in the lexicon.
The problem with this question has been, and remains, not that the answers are wrong per se, but the question. It simply starts from the wrong premise.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial