The purpose of having an abstract child by expanding a specific parent

Sometimes I came across some class, as shown below.

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 the abstract_dog class? Why do we “transform” the non-abstract word method into abstract words again?

+6
source share
2 answers

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

+4
source

I agree with SLaks and I think that this will be a real situation:

 abstract class animal { public abstract void speak(); } class dog extends animal { @Override public void speak() { // Dog says 'bark' } } abstract class abstract_dog extends dog { @Override public abstract void speak(); } class poodle extends abstract_dog { @Override public void speak() { // poodle says 'yip yip' } } class great_dane extends abstract_dog { @Override public void speak() { // great dane says 'ruff ruff' } } 

I think you would use this if you want the new child class to implement the speak method, and the dog class could have other methods that the new child class should not implement.
Knowing more about your specific situation will help determine if there is a better design for this scenario.

0
source

Source: https://habr.com/ru/post/895912/


All Articles