Specifying the default return while declaring an abstract method

3k views Asked by At

I have the following classes

The interface:

public abstract class MyAbstractClass
{
  public abstract boolean checkSomething();
}

The class inheriting the abstract class:

public class MyClass extends MyAbstractClass
{
  public boolean checkSomething()
  {
    return true;
  }
}

When I create MyClass in eclipse the checkSomething() method is created automaticly but with false as the default return value. Is there a way to specify in an abstract method definition what the default return type shoud be?

3

There are 3 answers

0
Jon Skeet On

No, there's no such thing as a "default return value" in Java. Each implementation should decide that for itself - it's just the method templating that Eclipse uses which happens to have picked false. Given that you should be replacing the method body anyway, I don't see this as much of the problem.

If a default implementation of "just return true" is reasonable, it sounds like checkSomething shouldn't be abstract in the first place - that's where the default implementation would live, and be overridden by classes wanting to provide more specific behaviour.

0
Ravi Bhatt On

Have a look at eclipse code templates

Other than that, you can create your custom annotations to achieve the same.

0
Shehan Simen On

You can not have a method body in an abstract method declaration. So there is no way to specify a default return value in an abstract method. This is independent from the IDE template.