Why do abstract methods have to be implemented by the first concrete class, and not one further down the chain?
I'm curious as to why abstract methods MUST be overridden by the first concrete implementing class, and not one further down the hierarchy change.
I'm not suggesting I want to do this, but I'm curious as to why it has to be the first class
Consider this example
abstract class Upper
{
abstract void doSomething();
}
class Middle extends Upper
{
void doSome开发者_开发问答thing()
{
// I'm forced to be implemented here
}
}
abstract class Lower extends Middle
{
}
class Bottom extends Lower
{
void doSomething()
{
// I'm valid, but I'm too far down the hierarchy
}
}
By definition a normal class must implement all abstract methods. If you would declare Middle abstract, then you would not have to implement the methods in Middle.
A normal class can be instantiated, whereas a abstract class cannot. Think about what would happen if you try to call the methods that are not implemented in the class.
concrete classes are concrete.
Which means they should be able to be initialized and used.
So, if you don't implement the methods, how would it be properly used ? it will be incomplete and thus not concrete.
You could make another abstract class inherit an abstract class.
Just make Middle
an abstract class, and it's fine.
If you want to keep Middle
as a concrete class but also remove the doSomething
from it, please explain what you'd want this code to do:
Upper x = new Middle();
x.doSomething();
If Middle is a concrete class, an instantiated version that didn't define an abstract method wouldn't be callable (as it has no definition). This would inherently make Middle an abstract class as well.
Because you cannot have abstract methods in a class that is not abstract. If you don't want need any of the methods, you don't need to extend that class.
Instead, just have class Lower extend Upper
.
If you were to wish to still have Lower
inherit both the abstract methods and inherit from Middle
, simply declare Middle
as abstract.
You're forced to implement doSomething
in Middle
, because Middle is a concrete class. That means that Middle
must provide an implementation of all abstract methods from its superclass as well as an implementation of any interface methods from interfaces that it implements.
If you marked Middle
as an abstract class, you can delegate the implementation of doSomething
to Bottom
.
For more information about abstract classes, see this link from the Java Language Specification.
In your example without the declaration of doSomething in Middle there'd be no code to run. Therefore the class would have to be abstract.
It isn't really a question of must. An abstract class is just a class with one or more methods without implementations.
It's more like saying a biological human male must have a Y chromosome. It isn't a law; it's just part of the definition.
In your example, if you don't want to implement doSomething
in Middle
, then you really don't want Middle
to be concrete. That is, it would have a method that has no definition. So it's abstract.
You could, of course, implement it as an empty function that does nothing. That's pretty close to what you're asking about, I believe.
Really it's just a question of terminology. If there are unimplemented members, then the type's abstract. It's that simple.
精彩评论