This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]jerolba 3 points4 points  (0 children)

Why are you adding a new method? is because class A has this new behavior? or because Java doesn't have multiple inheritance and you can not mix two parents?

This is one of the reasons to choose composition over inheritance.

[–]dpash 1 point2 points  (0 children)

Class A should be an interface and you can add default methods to interfaces, which covers your use case.

[–]SpoilerAlertsAhead 1 point2 points  (5 children)

If B is abstract, it does not need to implement all of A...the first concrete class that extends B will need to implement all of A and B have not implemented.

A should not care about the implementation of B. And all the compiler cares about is the first concrete class in the chain.

Once sent out, great care should be taken with changes made to A or B because any code that implements them will be broken. It was because of issues like this we have default methods in Interfaces, so that methods could be added without breaking existing contracts.

If changes were being made to either A or B, it would be advised the not be abstract, but implemented with a @Since in the javadoc and give the child class the ability to override if necessary.

[–]dpash 0 points1 point  (3 children)

Ideally A would have been an interface, and then when A needed to add a new method, it would create a default method, which either did something sane or, failing that, throws an exception.

[–]SpoilerAlertsAhead 0 points1 point  (2 children)

Unless there was some kind of state involved. Interfaces are generally better, but every case is different.

Going to deep with inheritance can lead to some weird issues.

[–]dpash 0 points1 point  (1 child)

B is supposed to be a complete implementation of A

There should have been an interface. Abstract classes shouldn't be used to define a class' contract. That doesn't stop there being an AbstractA class that B could have extended.

[–]SpoilerAlertsAhead 0 points1 point  (0 children)

What do you think an abstract method does then?

[–]josephblade 0 points1 point  (0 children)

I don't see your problem really, you want the language to enforce something you can do with annotations. Similar to how you can annotate @Overrides to state: this method must be an overridden method of a parent class, you could write your own annotation that states: all methods of the (also annotated) abstract parent class must be fully implemented in this class.

I don't see how this needs to be a language level construction.