Java Class Inheritance
Written by Mike James   
Article Index
Java Class Inheritance
Super and Object
Is Inheritance Bad?


Is Inheritance Bad?

When object oriented programming was first invented one of its main claims to be a better system of programming was inheritance.

It allowed you to reuse code in a way that was much better than copy and paste reuse because inheritance is a "live" connection between classes. For example, if you change the way the Point class is implemented those changes are inherited by the PointColor class. If you used copy and paste to create the PointColor class you would have to remember to edit it with the changes you made to Point.

Unfortunately over time programmers discovered that inheritance was a two edged sword. Any changes you make to Point propagate to PointColor and all of the classes that inherit from it or PointColor or any of its child classes. If this works then great. If it doesn't work then a change in the base class i.e. Point could stop derived classes working. As you can imagine the resulting bugs can be difficult to put right.

For this and many other reasons inheritance is not though of as the cure all it once was.

Today the motto is "prefer composition over inheritance". What this means is that you can often change your view slightly from IS A to HAS A. For example, PointColor IS A Point with a Color but you could also say PointColor HAS A Point and a Color. In this case you would create an instance of Point within the PointColor class - i.e. no inheritance. Creating objects that have other objects as properties is called composition or aggregation and it is a powerful but sometimes problematic technique. 

These are all advanced topics and we need to return to the theory of using classes and objects later. For the moment concentrate on the practice of creating and using them. Most of the time you will be writing programs that have very shallow class hierarchies i.e. each class inherits from just Object or perhaps one other class. It seems that inheritance is something that is better when used sparingly. 

I guess the answer to the question "is inheritance bad?" is no, but it can be used in bad ways. 

To Summarize

  • Objects sould keep their internal workings to themselves - they should encapsulate their workings. An object should offer the rest of the world a set of methods that allows the outside to work with it. For example, set and get methods rather than direct access to variables.
  • NetBeans will generate getters and setters for any variables you define in your class you select.  
  • Constructors are used to initialize instances and they are called automatically when ever an instance is created.
  • You can overload a function by creating versions with different signatures - i.e. parameters. Which version of the function is called depends on the parameters you supply.
  • Object often have relationships which can be summed up by use fo the phrase "IS A". So a point with color IS A point but with some extra properties and methods.
  • A class A, the child or derived class,  that inherits from class B the parent or base class, has all of the properties and methods of B.
  • You can add methods and properties to augment those inherited and you can override inherited methods with new methods.
  • NetBeans will automatically generate code that overrides any of the base classes methods for you.
  • You can call any base class methods using super.method().
  • You can call the base class' constructor within the child's constructor using super();
  • Inheritance was thought to be a cure all solution but today it is used with caution and only when there are clear benefits.


Modern Java
With NetBeans And Swing



  1. Why Java?
  2. Getting started with Java
  3. Introducing Java - Swing Objects
  4. Writing Code
  5. Command Line Programs
  6. User Interface - More Swing
  7. Working With Class
  8. Java Class Inheritance
  9. Java Data Types - Numeric Data
  10. Java Data Types - Arrays And Strings
  11. Building a Java GUI - Containers
  12. Advanced OOP - Type, Casting, Packages
  13. Value And Reference 
  14. Java Lambdas, SAMs And Events


To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

C book



or email your comment to: