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(); }
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.
A concrete class is complete in itself and can extend and can be extended by any class.
An abstract class can not extend a concrete class.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With