Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); } 
like image 605
Heisenberg Avatar asked Jan 07 '14 11:01

Heisenberg


People also ask

Can abstract class extend a concrete class?

A concrete class is a conventional term used to distinguish a class from an abstract class. And an abstract class cannot be instantiated, only extended. An abstract class can extend another abstract class. And any concrete subclasses must ensure that all abstract methods are implemented.

Can a concrete class be extended?

A concrete class is complete in itself and can extend and can be extended by any class.

Can an abstract class extend a concrete class C#?

An abstract class can not extend a concrete class.

Can I extend a concrete class java?

A concrete class is a class that has an implementation for all of its methods. They cannot have any unimplemented methods. It can also extend an abstract class or implement an interface as long as it implements all their methods. It is a complete class and can be instantiated.


1 Answers

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!

like image 114
Michael Berry Avatar answered Oct 14 '22 21:10

Michael Berry