1

Consider the following two classes:

class A {
    String _name;
    char _type;

    public A(String name, char type) {
        _name = name; _type = type;
    }

    public int method1() {
        do_something...;
    }

    public String method2() {
        make_something...;
    }
}

and

class B extends A {
    public B(String name) {
        super(name, 'a');
    }

    @Override
    public int method1() {
        do_something_else...;
    }

    @Override
    public String method2() {
        String answer = super.method2();
        answer += more_processing...;
    }
}

I am using inheritance here because the classes share many things in common. However, because I am overriding every single methods from the superclass, is it a good idea to use inheritance here?

Mi_Onim
  • 412
  • 2
  • 12
  • What exactly do they have in common? – PM 77-1 Apr 04 '16 at 00:41
  • the fields, and the subclass methods call the superclass methods at times – Mi_Onim Apr 04 '16 at 00:42
  • In this example, inheritance is not actually being used, because `B` is not extending `A`. But if it were, inheritance would make sense if you want `B` to inherit `A`'s behaviors and you just want `B` to customize them, and/or you want to pass `B` objects around using `A` pointers instead of `B` pointers. – Remy Lebeau Apr 04 '16 at 00:44
  • @RemyLebeau sorry I forgot to use the 'extends' keyword. – Mi_Onim Apr 04 '16 at 00:50

5 Answers5

2

That depends on the context. If you are going to use many features of the class (attributes and methods), you must use inheritance and override those few methods you need to change. If you are going to use just few features and need to override almost all methods, you better create a new class. Another case could be if a particular method of the class is very difficult to code or had a very important implementation, again it depends on the context.

mikeescom
  • 123
  • 3
2

Inheritance should be used whenever you can replace an instance of the super-type A with an instance of the sub-type B, without breaking anything. This is also known as the Liskov substitution principle.

Another kind of hacky but still valid case for inheritance is when you need to access otherwise unaccessible methods or properties of the super-type A. In this case, I prefer to not directly let B extend the super-type A but to use an inner class B.ChildOfA that extends the super-type A. This will prevent B from being used as an instance of A where this is not appropriate.

In general, you should prefer composition over inheritance.

To answer your question: It does not depend on whether you overwrite everything but on how your sub-class can or should be used.

If you override everything from A in B and you don't delegate to the super-type A, but both types should still be exchangeable like defined by the Liskov substitution principle, then you also have a third option: You can declare a common super-type I and let both classes inherit it. The newly introduced super-type I can be an interface or a class. Of course, this is only possible if you own the class A, because you need to change it to inherit the new super-type I.

Community
  • 1
  • 1
Stefan Dollase
  • 4,530
  • 3
  • 27
  • 51
1

You'll have to ask yourself the question: Is Class B a subclass of Class A?

Overriding the methods doesn't really matter as long as you understand what inheritance is used for. (i.e Class B is a subtype of Class A but does things a little differently)

nur-sh
  • 203
  • 1
  • 11
1

The crucial question you have to ask is:

"Is B an A?"

If so, then using inheritance is likely to be a good idea.

Although you may not have any methods that have the same implementation in both classes, that could change in the future as you and others working on your codebase could add new methods.

Another question you have to ask is, does each method and it's overridden version differ by WHAT they do, or just by HOW they behave? If it's the latter then again inheritance is probably the way to go.

Where I work we consider it best practice to mostly use interfaces (composition) over inheritance where possible. This is partly because one class can implement multiple interfaces in Java, but can only extend one class using inheritance. There are other reasons but I don't want to go too off-topic. However if the relationship between the classes you've written is more that they just HAVE similar members, rather than have an "is-a" relationship, then you might want to consider putting those shared members (methods and fields) in an interface instead of using inheritance.

1

In this case, where B does not actually inherit anything from A it may make more sense to use a common interface that both A and B implement in their own way. Context is important when making these types of inheritance decisions.

public interface SomeInterface {

    public int method1();

    public String method2();

}

A:

public class A implements SomeInterface {

B:

public class B implements SomeInterface {
Boo Radley
  • 684
  • 1
  • 13
  • 25