0

Let's assume I have this class:

public class Person {
    private int age;

    public int getAge() {
        return age;
    }
}

I have 4 classes that extend Person. These classes don't have any attributes in common, except age, which is inherited from Person.

Is there any disadvantage of using inheritance, except for the different architecture or understandability?

Anderson Vieira
  • 8,919
  • 2
  • 37
  • 48
Goldbones
  • 1,407
  • 3
  • 21
  • 55
  • 11
    One disadvantage is that you can't extend any other class, since Java doesn't support multiple inheritance. Maybe is better implements an interface with a method getAge() – Héctor Apr 08 '15 at 11:45
  • This might change the performace. But since you seem to be a beginner at programming, you should not worry about performance. – Sibbo Apr 08 '15 at 11:46
  • @Sibbo: Can you explain this a bit more? Does accessing super-class fields or method take more time compare to a field/method directly on the class? Is it in any way significant? – Thilo Apr 08 '15 at 11:48
  • possible duplicate of [java: inheritance](http://stackoverflow.com/questions/2135669/java-inheritance) – Alex Salauyou Apr 08 '15 at 11:48
  • @Thilo Fields should not be different, since they are statically dispatched. So they are resolved at compile time. But the methods are dynamically dispatched, so this might lead to an additional memory access plus addition for the vtable access. Depends on the JVM, since it might optimise single calls by making them static, if the compiler can resolve the method that is called. – Sibbo Apr 08 '15 at 11:51
  • 2
    One should keep in mind that "true inheritance" means more than just putting `extends Whatever` in your class definition. For example, you might want to read about the Liskov substitution principle to guide you when using inheritance. See http://stackoverflow.com/questions/56860/what-is-the-liskov-substitution-principle – GhostCat Apr 08 '15 at 11:51
  • @Sibbo, when I´m asking for disadvantage it´s for that purpose. Sometimes an easy question has a difficult answer. – Goldbones Apr 08 '15 at 11:52
  • @Goldbones Then I'll create an answer from this. – Sibbo Apr 08 '15 at 11:54
  • While inheritance vs composition is a valid design concern don't let it distract you so much that you ignore other concerns. For example: age is something that changes over time. Consider storing a birthdate and calculating age. – candied_orange Apr 08 '15 at 11:55
  • @CandiedOrange, age is not a concret variable, as person is not a concret class. It is just a example for my realistic code;) – Goldbones Apr 08 '15 at 11:59
  • If you are so sure its not a 'concret variable' and person is not a 'concret' class then you've already decided to go with inheritance. .http://en.wikipedia.org/wiki/Composition_over_inheritance – candied_orange Apr 08 '15 at 12:11

2 Answers2

1

I don't see any disadvantage on your scenario.

It will even be good for cases where you want to generically handle any Person object -- for example, if you have many elements from different subclasses but just want to perform a computation on their ages.

But remember that Java does not support multiple inheritance. In that case, it may be good to declare Person as an interface with a method getAge() and allow your subclasses to extend other classes.

Bruno Toffolo
  • 1,504
  • 19
  • 24
1

In this case I think there are only upsides to using inheritance. You may only need age for all the subclasses now, but if you need more in the future it is way easier to use inheritance.

Black Magic
  • 2,706
  • 5
  • 35
  • 58