Avatar of CrazyOne
CrazyOne
Flag for United States of America asked on

DatagramPacket

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.
Java

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
Scobber

public class mydatagram extends DatagramPacket { }

Hope this helps

Scobber
CrazyOne

ASKER
Unfortunately when compile it gives this error

cannot inherit from final DatagramPacket
ASKER CERTIFIED SOLUTION
chaau

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CEHJ

public class mydatagram

Open in new window

That would be OK but Java class names begin upper case
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
krakatoa

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?
CEHJ

why do you think you need to do this?
Is actually a good question
krakatoa

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CrazyOne

ASKER
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.
krakatoa

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).
CrazyOne

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
chaau

Strangely then why you have not marked my answer as a solution then.
krakatoa

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?
CEHJ

Now i'm getting confused ...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
krakatoa

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.
krakatoa

@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?
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CrazyOne

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
CrazyOne

ASKER
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.
krakatoa

@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.
krakatoa

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.