Purpose of having abstract child by extending concrete parent
Sometimes, I came across some class design as follow.
abstract class animal {
public abstract void speak();
}
class dog extends animal开发者_JAVA百科 {
@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?
In case you want to create a base class that forces people to override speak
, but inherits Dog
.
I agree with SLaks, and think that this would be a real life 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 in the case where you want the make a new child class implement the speak
method and the dog
class may have other methods that the new child class would not have to implement.
Knowing more about your exact situation would help in determining if there is a better design for this scenario.
精彩评论