Chapter:Inheritance - Extending your own classes

From Juneday education
Jump to: navigation, search

Meta information about this chapter -work in progress

Expand using link to the right to see the full content.

Purpose

We've seen so far how every class extends object, as well as how to override (redefine) a method inherited from Object. And we've seen that inheritance is used in classes in the Java API. But how can you use inheritance between your own classes? And why would you do that? The purpose of this lecture is to show the student that you can create your own class hierarchy using inheritance, and what the implications of and reasons for doing so are.

The lecture talks about abstract classes and final classes, and informs the student about not taking inheritance too far (so called class explosion) and we tell the students that we will look at alternatives to inheritance in lectures to come.

Goals

The goal is that after this lecture the student knows and understands

  • That she can extend her own classes, creating a hierarchy of classes
  • That inheritance can lead to class explosions, if not done carefully
  • That a class can be abstract in order to define an API for a set of related classes
    • That abstract classes can't be instantiated - and that they are not meant to be either
  • That a class can be final - which prevents extension
  • That knowing when and why inheritance is the right choice is very hard and advanced - and that there are many conflicting opinions and styles surrounding this topic

Instructions to the teacher

Common problems related to this chapter

  • Learning how to extend one's own classes doesn't mean that inheritance should be used all the time
  • Simple examples on how to extend a class typically focuses on rules and syntax - not on class design
    • Make the students realize that learning about when to use inheritance is a much more advanced topic than how to do it
  • If you talk about re-use of code when doing inheritance, be careful to point out that this is not the main reason for inheritance
    • Sometimes, like when you end up with a class explosion, you actually don't "save" any code, but actually end up writing more code than if something other than inheritance were used (like composition, or using interfaces with the strategy pattern etc)
  • Make sure that the students understand that we are only scratching the surface on inheritance in this introductory course material

Full frontal - code up-front

Here's a small teaser on what's to come:

public class DiplomaticPassport extends Passport {
  @Override
  public String toString() {
    return "Corps Diplomatique " + super.toString();
  }
}

Inheritance: It is possible to extend your own classes

It is possible to create your own class hierarchy, using inheritance (and extends). This chapter will introduce you to how to do that but at the same time show that some of the suggested gains from inheritance may not actually be a gain at all.

Chapters on the topic of Inheritance

By Osteomyoamare (Own work) CC BY 3.0, via Wikimedia Commons

Videos

Note that to date (2018-10-23), there is only one single video for English and one single video for Swedish. More may be added later.

English videos

Swedish videos

Links

Further reading

Where to go next

Next page contains exercises for this chapter: Inheritance_-_Extending_your_own_classes_-_Exercises

« PreviousBook TOCNext »