How do I convert an abstract class into an interface?

abstract-class, interface, java

Solution

how do I convert an abstract class into an interface?

Make a copy of the abstract class source file.

Change "class" to "interface" in the initial declaration.

Change the name (optionally, depends on what you're doing).

Remove the bodies of any methods that are implemented by the class.

Remove the word "abstract" from the other ones.

Remove all private and protected members.

Remove all constructors.

Remove the keyword "public" from the public members.

If you had any code you removed (implemented methods, private or protected stuff), have your original abstract class implement your interface and leave that stuff there.

(Incomplete) Example:

`Foo` as an abstract class:

public abstact class Foo
{
    private int bar;

    public static final int SOME_CONSTANT = 42;

    public Foo(b) {
        this.bar = b;
    }

    public abstract void doSomething(String s);

    protected int doSomethingElse() {
        return this.bar * 2;
    }
}

`Foo` as an interface:

public interface Foo
{
    int SOME_CONSTANT = 42;

    void doSomething(String s);
}

In my case, as I did have some stuff the old `Foo` did, I'd probably have `AbstractFoo` or something:

public abstact class AbstractFoo implements Foo
{
    private int bar;

    public Foo(b) {
        this.bar = b;
    }

    public abstract void doSomething(String s);

    protected int doSomethingElse() {
        return this.bar * 2;
    }
}

...so that an implementation could use it as a starting point if desired (although with that `private` `bar` in there, it doesn't make a lot of sense).

Problem

I have a java program which uses arraylists - these arraylists store 'variables' where 'variables' is an abstract class. Now, to save memory, I want to use a java library called HugeCollections-VanillaJava- however this library requires an interface to be defined. How do I convert the abstract class into an interface? What rules/restrictions do I have to follow, to correctly perform the conversion? Finally, is it possible for me to use my abstract class with minimal code changes, so that the library that requires an interface, also works correctly? Ideally I would like not to change the abstract class at all...Is this possible?

Original source