• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

java inheritance

Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just wondering is this a good example for inheritance.

Posts: 74037
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you find a twenty‑year old textbook, you will get the impression that inheritance is the be‑all and end‑all of object‑oriented programming. Well, nearly. Nowadays you hear people saying they hardly ever use inheritance. But you will sometimes find it useful, so you do have to know about it.
Try drawing a UML diagram of your three classes; you will find that one class has multiplication and one summation. That is strange. They seem to obey the rule that one sort of calculator IS‑A‑nother sort of calculator, but their toString methods work completely differently. That may breach the Liskov substitution principle that you shouldn't be able to tell whether you are using a superclass object or a subclass object; the result should simply come through. They also have different methods. If you have class Car extends Vehicle and class Taxi extends Car you can implement speedUp() and stop() methods in the Vehicle class and they can remain unchanged in the two other classes. In fact most methods in the Taxi class can be fully implemented in the Vehicle class. You don't have that here. If you say, Calculator calc = new MultiplyingCalculator(...); you can't find a multiply() method without casting calc. There is a thing called type extension and there is a thing called functional extension and I am never quite sure I can remember which does and which doesn't have additional methods, I find the sort with additional method awkward to use because subclass objects need to be cast before you can use their methods. Look for the List interface and a few implementing classes, e.g. Vector, LinkedList, and ArrayList. Look for the ensureCapacity() and trimToSize() methods. Find out where those methods are and where they aren't. Work out how you can use them if you are presented with a List and don't know its exact implementing class. That is a good example of how things can go wrong if you add more methods.
Go through the Throwable class and Exception and look at their subclasses. Go through the method summaries, and you will find that many exception classes declare four constructors and no methods. That is because they can use the methods from their superclasses and have no need ever to declare new methods. I think that is a better example of inheritance.

Afraid I think you have found an example showing the problems of inheritance rather than its good points.
A teeny tiny vulgar attempt to get you to buy our stuff
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic