Sometimes, I came across some class design as follow.
abstract class animal {
public abstract void speak();
}
class dog extends animal {
@Override
public void speak() {
// Do something.
}
}
abstract class abstract_dog extends dog {
@Override
public abstract void speak();
}
I was wondering, what is the purpose of having an abstract_dog
class? Why we "transform" the non-abstract speak method into abstract speak again?
An abstract class can not extend a concrete class.
Yes, it is entirely possible. Being made abstract does not prevent it from extending from a concrete class.
Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
A concrete class is a subclass of an abstract class, which implements all its abstract method. Abstract methods cannot have body. Abstract class can have static fields and static method, like other classes. An abstract class cannot be declared as final.
In case you want to create a base class that forces people to override speak
, but inherits Dog
.
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