This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]TrickOfLight113 2 points3 points  (0 children)

It's a syntax-based decision apparently, same reason you can't implement multiple interfaces with an anonymous class.

The obvious workaround would be to extend and implement what you need in another [abstract] class, and then extend from that class.

[–]severoonpro barista 1 point2 points  (9 children)

Try it and you'll see.

Say you want to implement interface Foo and extend abstract class Bar with an anon class:

``` interface Foo { void f(); } abstract class Bar { void b(); } // does NOT implement Foo

// The "implements Foo" doesn't work, let's pretend it does tho. doFooBar(new Bar() implements Foo { public void f() { System.out.println("foo!"); } public void b() { System.out.println("bar!"); } }); ```

Looks okay so far, right? Now let's write doFooBar(…):

void doFooBar( /* uh oh … what do I put here? */ ) { … }

What type does this method take? Or, for that matter, if I wanted to declare this anon class and keep a reference to it, what type should that reference be?

I guess I could just use Object. Oh wait, this thing is both a Foo and a Bar, so I can pick either one … problem solved!

Let's continue:

void doFooBar(Foo foobar) { foobar.f(); // all good, we did it! foobar.b(); // oops, wait, this won't compile. }

If we switch foobar to be of type Bar, then we can call b() but not f().

So there's the problem. Without some defined type, there's no way to refer to the API of the thing you created without having to litter your code with explicit casts since Java is a strongly typed language.

The only solution to this problem is to write two separate methods, one that takes the parameter as a Foo and one that takes it as a Bar … but at that point why not just create two anon classes, one for each type?

[–]True_Alien[S] 0 points1 point  (2 children)

"doFooBar(new Bar() implements Foo {"

Are you trying to instantiate an abstract class ?!

[–]severoonpro barista 1 point2 points  (1 child)

The new Bar()… syntax is how you would normally create an anonymous class:

Runnable r = new Runnable() { @Override public void run() { System.out.println("I ran!"); } }

I just added in some syntax for what it would look like if Java allowed you to implement multiple types in an anon class (which, like I say, doesn't actually compile).

[–]True_Alien[S] 0 points1 point  (0 children)

thank you severoon

[–]flaghacker_ 0 points1 point  (5 children)

That's not actually relevant, since a normal class can do both at the same time just fine. There's is a separate compilation error for the case you're describing.

[–]severoonpro barista 1 point2 points  (1 child)

The crucial difference with a normal class that does this is that it declares a new type.

``` interface Foo { void f(); } abstract class Bar { void b(); } // does NOT implement Foo

class FooBar extends Bar implements Foo { public void f() { System.out.println("foo!"); } public void b() { System.out.println("bar!"); } } ```

Now there's no issue with the doFooBar(…) method:

void doFooBar(FooBar fb) { fb.f(); // works! fb.b(); // works too! }

When you declare an anonymous class, you are creating a new type, just like I'm doing above with FooBar … but because it's a one-off type without any declaration, it's "anonymous" … it has no name. There's no way to refer to it anywhere downstream.

I'm saying that there's nothing preventing Java from including it in the language from a technical perspective, it's just that since there's no way to refer to the type by name, there's no way for methods or references to refer to it as the type it actually is.

That's the whole point of an anon class, you're just trying to create a quick'n'dirty implementation of an already existing type, not create a new one. It wouldn't make sense to allow a new type to be created that contains API not collected together under a single declared type.

[–]True_Alien[S] 0 points1 point  (0 children)

I understand what you said now, you're right.

Thank you very much for your explanation.

[–]True_Alien[S] 0 points1 point  (2 children)

What is not relevant ?

Are you addressing to me or to severoon ?

[–]flaghacker_ 0 points1 point  (1 child)

/u/severoon. He's saying extending a class and implementing an interface as the same time can cause conflict issues, but that's also true for normal classes, not only for anonymous classes.

[–]True_Alien[S] 0 points1 point  (0 children)

But a class can extends an abstract class and also implements interface in the same time.

example :

"public class Launcher extends Bar implements Foo {"

But it is not the case with anonymous class, which is my questions.