Is it really necessary to implement all the methods to a subclass(inherited from a abstract class), if there is another subclass of that abstract class has already implemented those abstract methods?
abstract class A {
abstract void method();
abstract void anothermethod();
}
class B extends A {
void method() {}
void anothermethod() {}
}
class C extends A { // is this class definition is legal?
void sample() {}
}
No, it’s not legal. You’ve got a concrete class (
Cis not declared abstract) extending an abstract class, but without providing implementations for its methods. It’s not really clear why you think this should or could be legal – and you should consider whether this has wider ramifications for your understanding of inheritance in general than just this specific case.Cis entirely separate fromB. They could have entirely different state – for example, B might implementmethod()using some state which is only present in an instance ofB. It’s important to understand that an instance ofCis not an instance ofB.If you want it to inherit its behaviour, you should make
CsubclassBinstead ofA.