Can we avoid inheritance without using final keyword?

Viewed 622

I just want to know that can i have 2 classes A and B. I don't want to allow class B to extends class A. What technique should i apply in class A so class B cannot inherit class A. Don't want to make class A final. Any other solution instead of making class A final?

6 Answers

In fact, the practice that I try to follow, and that Josh Bloch recommends, in his Effective Java book, is exactly the inverse rule of the one you've been told: Unless you have thought about inheritance, designed your class to be inherited, and documented how your class must be inherited, you should always disable inheritance.

I would recommend reading this chapter of Effective Java (you won't regret buying it), and showing it to the person who told you about this rule.

The most obvious reason to disallow inheritance is immutability. An immutable object is simple to use (only one state), can be cached, shared between many objects, and is inherently thread-safe. If the class is inheritable, anyone can extend the class and make it mutable by adding mutable attributes. https://stackoverflow.com/a/10464466/5010396

This is not possible in "nice ways". The java language allows you to either have the final keyword on your class definition, or to not have it.

As pointed out by others: you can make all constructors private, then subclassing becomes practically impossible, as the subclass constructors have no super class constructor to call.

In case you need to instantiate A, you could still have a factory method, like:

public class A {
  private A() { ... }
  private A(String foo) { ... }

  public static A newInstance(String foo) { return new A(foo); }

for example.

But keep in mind: code is written for your human readers. If your intent is to have a final class, then the correct answer is to use that keyword final.

Plus: making your class final allows the JIT to do a few more things, as it doesn't have to worry about polymorphism at any point (so it can directly inline method code, without any additional checks). So using final can result in slightly improved performance. On the other hand, it limits your ability to unit test things (for example: standard Mockito can't mock final classes).

You can mark the constructor of A class as private. :)

PS: If you also want to avoid reflection attacks then throw some Error from constructor and also mark it private.

I don't understand the specific use case of your requirement but this could work.

If you are open to make changes in B

In B's every constructor check for is it is an instance of A then throw an error.

if(A.class.isAssignableFrom(B.class)) {
        System.out.println(true);
        throw new IllegalStateException();
    }
    else
        System.out.println(false);

You have an option to restrict in constructor like this.

if (this.getClass() != MyClass.class) {
    throw new RuntimeException("Subclasses not allowed");
}

For further details check the post.

https://stackoverflow.com/a/451229/6742601

You can do one of the following to avoid inheritance without using final keyword:

  • Use private constructor(s).
  • Mark every method final so children can't override them.
  • Throw Runtime exception in the constructor if want to limit inheritance for some unwanted children (although v.rude) e.g
    if (this.getClass() != FavoriteChild.class) throw new RuntimeException("Not Allowed")
Related