2

I have a simple interface:

public interface MyInterface{
    public void method1();
}

What I want is that whenever a class implements MyInterface, the correspondig methods will always have a default code. When I write implements MyInterface I want to be able to use Eclipse's suggestion in order to add the code automatically. For example:

public class SomeClass extends AClassImForcedToExtendDueRequirements implements MyInterface{

    ...
    /*Now I use Eclipse's code-completition suggestion*/
}

And then I would like to have this situation:

public Class SomeClass extends AClassImForcedToExtendDueRequirements implements MyInterface{

    ...
    @Override
    public void method1(){
        System.out.println("This is a default line of code, added automatically!");
    }
}

So, how can I edit the Interface? Thank you

Matteo A
  • 358
  • 5
  • 15
  • 2
    I believe this feature may be in Java 8, but certain circumstances must surround the method. Possibly called functional interfaces. – Kevin Bowersox Dec 04 '13 at 10:17
  • 2
    If you want to replace the default `// TODO Auto-generated method stub.` that is put into auto completed methods see http://stackoverflow.com/questions/501648/how-to-change-auto-generated-code-when-creating-new-class-in-eclipse Note: that can not be changed for just an interface of certain methods. – zapl Dec 04 '13 at 10:18

2 Answers2

3

Can't be done with interfaces. That's what abstract classes are for.

public abstract class MyAbstractClass
{
  public abstract void myAbstractMethod()
  {
    System.out.println("Default method body");
  }
}

//calling myAbstractMethod on this will output "Default method body"
public class MyClass extends MyAbstractClass
{
 //other stuff
}

//calling myAbstractMethod on this will output "Overriden method body"
public class MyClassWithOverride extends MyAbstractClass
{
  public void myAbstractMethod()
  {
    System.out.println("Overriden method body");
  }
}

I know Eclipse can't do this for you and am unaware of any available plugin or how it would work. The reason being that it would need to get that default implementation from somewhere so one would need to be written and stored somewhere for every interface it is to be done with. Then there's the -however unlikely- problem of when you implement two interfaces with the same method name.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
  • He cannot "`implements`" an abstract class, though. – Birb Dec 04 '13 at 10:20
  • They didn't say that. – Ross Drew Dec 04 '13 at 10:21
  • Does it matter what he says, when that most likely is his ultimate wish? You can't extend multiple classes, so there must (should) be a reason he is using an interface in the first place. – Birb Dec 04 '13 at 10:24
  • Well we have no idea of his level of ability, he could be new to interfaces and abstracts classes and be unaware of the benefit. – Ross Drew Dec 04 '13 at 10:29
  • 1
    Probably I'm unaware of the benefits of the two. What I'd like to have is _something_ that adds automatically a method with some default code in a class. The first part is achieved using interfaces, but the second one is the one I'm asking for :D. I have a 2 years experience on Android programming, and only few months with pure java – Matteo A Dec 04 '13 at 10:56
  • 1
    An abstract class is like an interface except that you can fill some or all of the methods with default code that the class extending will implement. You can also override the default implementation. Sounds like you want an abstract class. – Ross Drew Dec 04 '13 at 10:58
  • 2
    I forgot to say that the class already extended another class (Thread), and in java I can only extend one class. I can't implement Runnable, i'm forced to extend Thread due to specific requirements – Matteo A Dec 04 '13 at 11:03
  • Don't `extends Thread` then, `implements Runnable` instead. As described here http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – Ross Drew Dec 04 '13 at 11:05
  • 1
    @RossDrew I can't do that. See my edited questions, I MUST extends Thread due to requirements :-( – Matteo A Dec 04 '13 at 11:07
  • 1
    Extending `Thread` isn't the best way to do this -like that link says- but if you have to then you cannot do what you ask unless you find an Eclipse plugin to do it. – Ross Drew Dec 04 '13 at 11:09
  • 1
    @RossDrew I know Runnable fits better (in general), but my code rely on a third-party code I cannot change. If I wrote that code, I would probably used `Runnable`, but I cannot change that. It's not my fault. Thanks again for the support, if nothing shows up I will mark your answer as accepted ;-) – Matteo A Dec 04 '13 at 11:13
1

As you can't implement an abstract class, which is able to give you exactly what you're asking for, you should take a step back and see if you could improve your code structure. If you have a bunch of classes that does the same thing, maybe these classes should be sub-classes of a common class?

If you want to use Eclipse to automatically generate something every time no matter what you are implementing, you should look up "Code Templates" in properties.

Birb
  • 866
  • 10
  • 23
  • The problem is that the class I Want to implements my Interface already extends a class (Thread if you ask). I cannot implement Runnable (I am told to extend Thread and i cannot change it). Since in java I can only extends one class, I thought about implementing interface, but id does half of what I'd like to reach. I want something that adds automatically a method with code in a class that already extends some other class. – Matteo A Dec 04 '13 at 11:01