Force a subclass to implement abstract subclass

I want to create an abstract class in java that forces all its subclasses to implement a SwingWorker (plus the SwingWorker's abstract doInBackground() and done()).

In AbstractClass -

abstract class Task extends SwingWorker<Void, Void>{};

I would expect this to cause the compiler to throw an exception when an extended class doesn't implement it, but it doesn't.

I am also not quite sure how I would go about indicating that I'm overriding this abstract class. Do I redeclare it in the ConcreteClass as follows?

class Task extends SwingWorker<Void, Void>{
  ...
}

or some other way?

Thanks for all your help!

Answers


you need to declare the methods also as abstract, like this:

abstract class Task extends SwingWorker {
    @Override
    protected abstract Void doInBackground();
    @Override
    protected abstract void done();
};

doInBackground() is abstract in SwingWorker, so any concrete subclass of Task should have to provide an implementation.

done(), however, is not abstract in SwingWorker, so no new implementation is needed. If you want to force your subclasses to reimplement it you need to:

abstract class Task extends SwingWorker<Void, Void>{
    @Override
    protected abstract void done();
}

To create the concrete subclass, just extend Task like you would extend any other class, and override the abstract methods with implementations:

class SubTask extends Task {
    @Override
    protected void done(){
        //implementation
    }

    @Override
    public Void doInBackground(){
        //implementation
    }
}

The first piece of code will compile, because class Task is also marked as abstract and compiler accepts that it doesn't implement abstract members of ancestor. But you will not be able to instantiate it (call new Task(); ). The second piece should not compile, because you didn't implement abstract methods. To see examples scroll down this page: SwingWorker


Need Your Help

How to change NavigationBar of NavigationController in every other controller based on requirement?

iphone objective-c xcode uinavigationcontroller uinavigationbar

In my app i have created a Custom UINavigationBar in which i have written different methods for customizing the UInavigationBar like navigation bar with title image, backbutton and signoff button s...

What does this undefined index message mean

php html forms undefined-index

this code calls the values entered into a form and enters them into a database (or at least it's supposed to) every time the page loads it gives "undefined index" messages, and I am struggling to

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.