Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of having abstract child by extending concrete parent

Tags:

java

oop

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?

like image 695
Cheok Yan Cheng Avatar asked Aug 25 '11 19:08

Cheok Yan Cheng


People also ask

Can an abstract class extend a concrete class?

An abstract class can not extend a concrete class.

Can an abstract class be the child of a concrete class?

Yes, it is entirely possible. Being made abstract does not prevent it from extending from a concrete class.

Can abstract class have concrete methods?

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.

How do abstract and concrete classes differ from each other?

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.


1 Answers

In case you want to create a base class that forces people to override speak, but inherits Dog.

like image 52
SLaks Avatar answered Sep 21 '22 16:09

SLaks