[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Any javafags here? I'm trying to create an anonymous class

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 8
Thread images: 3

File: 1497969841552.jpg (3KB, 200x150px) Image search: [Google]
1497969841552.jpg
3KB, 200x150px
Any javafags here?

I'm trying to create an anonymous class to override a method of an object created by a method return, not from "new Object();"

e.g
>InputStream is = new Socket().getInputStream();
not
>InputStream is = new InputStream();
(Im aware InputStream is abstract, its just an example)

How do I do that?

ps. I posted this on /wsr/ but no one answered. Please help
>>
You can't do what you're asking. You're going to have to make a wrapper object.
>>
>>60993037
I figured out my problem, I just copied the method from the superclass and when the object was created by "new" I overrode it then

What do you mean by that though? You can make wrapper classes yourself? I thought it was only for java's primitive data types
>>
File: noreallystop.png (733KB, 666x523px) Image search: [Google]
noreallystop.png
733KB, 666x523px
>>60992220
Stop using Java
>>
>>60993143

You're confusing wrappers with boxing.
>>
File: 1497209450420.jpg (78KB, 492x559px) Image search: [Google]
1497209450420.jpg
78KB, 492x559px
>>60993179
>mfw i always thought they were the same
Please enlighten me
>>
>>60993233

Boxing is a special case of wrapping. Boxing is when it applies to Java's primitives, and it's supported directly within the language.
int a = 1;
Integer b = new Integer(a);
Integer c = a; // it just werks


What I was suggesting you do is something like this.
public class MyWrapper implements InputStream
{
InputStream wrappedObject;
MyWrapper(InputStream wrappedObject)
{
this.wrappedObject = wrappedObject;
}
public methodToOverride()
{
// Do some shit
}
public otherMethod1()
{
wrappedObject.otherMethod1();
}
public otherMethod2()
{
wrappedObject.otherMethod2();
}
...
}


It's ugly but I don't know how else you can extend the functionality of that object without having access to its point of construction.
>>
>>60993442
I see, thanks man. This makes perfect sense
Thread posts: 8
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.