-5
\$\begingroup\$

I am trying to understand the compiler's reasoning about a certain implementation involving a default method and an abstract one with this question.

If you see the initial version, it explains that a version of the code does not compile, and what the compilation forces me to do. Yet, this version is put on hold, due to containing non-compiling code, which is what I am trying to understand.

For convenience here is the question below:

The following is not allowed in Java-8:

public interface Operator {
    default String call() throws Exception {
      // do some default things
      return performOperation();
    }

    String performOperation();
}

public abstract class AbstractTestClass {
    public abstract String call() throws Exception;
}

public class TestClass extends AbstractTestClass implements Operator {
    String performOperation() throws Exception {
      // do some operation
    }
}

The above fails to compile with the error message saying that the TestClass needs to be either abstract or override the call method.

I was thinking that the default method could provide the necessary override. Why does that not work?

I am forced to do something like the following:

public interface Operator {
    default String doCall() throws Exception {
      // do some default things
      return performOperation();
    }

    String performOperation();
}

public abstract class AbstractTestClass {
    public abstract String call() throws Exception;
}

public class TestClass extends AbstractTestClass implements Operator {
    String call() throws Exception {
      doCall();
    }

    String performOperation() throws Exception {
      // do some operation
    }
}

This is lacking the clean design that I am looking for. Is there some way I can hide the call implementation?

Is this the right approach to closing questions involving "broken" code?

\$\endgroup\$
  • 3
    \$\begingroup\$ Do you have another suggestion for how to handle it? \$\endgroup\$ – Simon Forsberg Dec 15 '16 at 11:26
  • \$\begingroup\$ @SimonForsberg yeah, mentioning in a comment would be nice instead of just deleting it, at the least. Editing is an option. Moving the question over to StackOverflow is another. \$\endgroup\$ – arin Dec 15 '16 at 14:27
  • 2
    \$\begingroup\$ 1. What should be mentioned in the comment? 2. The question was closed by a moderator, not deleted. You deleted it all by yourself. 3. How can we possibly edit it to make it on-topic? 4. Many questions doesn't meet the standard requirements for Stack Overflow, if you want your question moved then you can flag it and ask it to be moved, if it is not moved it is usually because we are trying to save you from getting downvoted on Stack Overflow. \$\endgroup\$ – Simon Forsberg Dec 15 '16 at 14:55
  • 1
    \$\begingroup\$ @SimonForsberg no need to argue about this. I was obviously looking for help and closing the topic without any comment or a link to where this belongs in the exchange community is not helpful. In my above comment, "delete" was obviously a mistake and should have said "closed". Additionally, I have no concerns about getting "down-voted", I am looking for help from the community. The bureaucracy is clearly getting ahead of helping people asking questions. \$\endgroup\$ – arin Dec 15 '16 at 17:45
  • \$\begingroup\$ I'm not trying to argue, I'm trying to understand your point of view. Did you read the link that is in the close reason? meta.codereview.stackexchange.com/a/3650/31562 does mention Stack Overflow. \$\endgroup\$ – Simon Forsberg Dec 15 '16 at 17:56
  • \$\begingroup\$ @SimonForsberg yeah, thanks. I saw that and thought it wasn't helpful, since I do not have a "bug" per-se, which led me to the question here. Additionally, there is the warning of getting the same question closed off there, too. So I was anxious to just move the question over there. Something like the below answer from Phrancis was what I was looking for. Noted, going forward. \$\endgroup\$ – arin Dec 15 '16 at 17:58
15
\$\begingroup\$

I will refer first to Stack Overflow's on-topic page, as your question (in its original form) would actually be a better fit there (emphasis mine):

What topics can I ask about here?

Stack Overflow is for professional and enthusiast programmers, people who write code because they love it. We feel the best Stack Overflow questions have a bit of source code in them, but if your question generally covers…

  • a specific programming problem, or
  • a software algorithm, or
  • software tools commonly used by programmers; and is
  • a practical, answerable problem that is unique to software development

In comparison, here is Code Review's on-topic page (emphasis not mine)

If you are looking for feedback on a specific working piece of code from your project in the following areas…

  • Best practices and design pattern usage
  • Security issues
  • Performance
  • Correctness in unanticipated cases

Note on the same page, in what is not on-topic, the following line: (emphasis mine)

Trouble-shooting, debugging, or understanding code snippets

As such, even if your question did not contain broken code, it would still be off-topic if it is seeking for reviewers to explain how or why something is happening.

That being said, is "broken code" the best closing reason? Well, maybe or maybe not, that would be for @Jamal to clarify since he closed it. We do have a close reason for questions seeking explanation of code, perhaps that would have been closer. Perhaps "example code" would also be appropriate, as the code presented doesn't do anything in its current form...

String performOperation() {
  // do some operation
}
\$\endgroup\$
  • \$\begingroup\$ Thanks for the good explanation. The difference between these sites and StackOverflow isn't very clear for me, this helps. \$\endgroup\$ – arin Dec 15 '16 at 14:28
  • 5
    \$\begingroup\$ @arin a guide to CR for SO users does a good job at explaining these differences. \$\endgroup\$ – Mathieu Guindon Dec 15 '16 at 14:33
  • \$\begingroup\$ @Mat'sMug thanks for the helpful link \$\endgroup\$ – arin Dec 15 '16 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .