Skip to content
Advertisement

Abstract class extends concrete class

I earlier learned that abstract class can extend concrete class. Though I don’t see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract. Why? Can you provide with use case where it is useful? I am trying to learn design patterns and I do not want to miss anything.

Here is example:

public class Foo 
{
    public void test()
    {
    }
}

public abstract class Bar extends Foo
{
   @Override
   public abstract void test();
}

Advertisement

Answer

This becomes useful if I have a set of classes that I want a default implementation of test() for (so they can extend from Foo), and a subset of those classes that I want to force to provide their own implementation (in which case making it abstract in the subclass would enforce this.)

Of course, the alternative way in this example would be to declare test() abstract in the top level class rather than in the subclass, and that’s what you’d usually do – but there are cases where satisfying the is-a relationship of inheritance means that it occasionally makes more sense from a design perspective doing it this way around. It’s rare, but you do sometimes see it.

As an aside, though a special case, remember that all classes implicitly extend Object unless otherwise specified. So if you include this case, abstract classes extending concrete classes isn’t so unusual after all!

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement