Chapter:Inheritance - Examples from Swing

From Juneday education
Jump to: navigation, search

Meta information about this chapter

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


The purpose of this chapter is to show some interesting and good uses of inheritance in the Java API. We look at the Swing framework to see what can be achieved using inheritance. The purpose is not to teach the Swing framework, but rather to talk about the abstractions expressed via inheritance, and in particular the "is-a" relationship between e.g. a JFrame is a Container, and all Containers are Components.

We think this chapter will make the students think about the use of inheritance as something interesting and potentially useful, and that they realize that done right, it can be quite handy.

Another example from the Java API in this lecture is PrintStream and how it relates to FilterOutputStream and OutputStream.


After this lecture, the students shall know and understand

  • The hierarchical nature of the Swing framework, and how this relates to inheritance
  • That sometimes the design can be surprising, e.g. that JMenu extends JMenuItem
    • But when you think of it, it allows us to consider menus a specialization of menu items
    • A menu is a special type of menu item, which means that in a menu, some items are submenus
  • That the Swing framework is based on two major abstractions:
    • Component - almost everything is a component
    • Container - Some components are special and can contain other components
  • Subclasses are typically specializations of some abstraction
    • Specializations are often more concrete than their super classes
    • We can go from a very general abstract class down to more and more specialized concrete classes

Instructions to the teacher

Common problems related to this chapter

  • If the students want to know more about Swing, show them the source code of a simple Swing application (available online from the exercise chapter)
    • Stress that this is not a course about Swing
    • Swing is rather old and becoming replaced by JavaFX
  • Feel free to give examples from the JavaFX framework instead
  • Encourage students to investigate the online Java API documentation, to get an understanding on how inheritance is used in the Swing framework, or in the streams API
  • If the students don't understand the purpose of this lecture, explain that it is to show both a well-designed real API using inheritance and to relate it both to client code using the API, and to show that the design matches the way we think about e.g. a GUI
    • A window has components
    • Some components actually are containers for other components
    • You can add a menu to a menu, in order to create a submenu
    • You can add a button to a panel
    • etc

Full frontal - Code up-front!

Just a small piece of code, to get you started!

      // A menu bar that we add to the window
      JMenuBar menuBar=new JMenuBar();
      frame.add(menuBar, BorderLayout.NORTH); // NORTH is at the top

      // First menu for the menu bar, with only one item
      JMenu menu = new JMenu("File");
      JMenuItem quit = new JMenuItem("Quit");
      // Add a menu item to the File menu

      // Second menu for the menu bar, with a submenu as the only item
      JMenu edit = new JMenu("Edit");
      JMenu lines = new JMenu("Lines...");// Submenu to edit
      // The only item of the submenu
      JMenuItem autoIndent = new JMenuItem("Auto indent");
      // Add the item to the submenu
      // Add the submenu to the Edit menu

Inheritance: Examples from Swing

Swing, which is a graphical framework in Java, uses inheritance extensively and is therefor a good example on inheritance. We will look briefly at some of the Swing components and at the hierarchy.

For instance, a menu in Swing is also a menu item. This allows us to add a menu as a menu item to another menu item. Easier put, a menu can have menu items. Some of the menu items can be menus, creating sub-menus.

At the same time, a menu item is a kind of button. So menu items can be clicked on like a button. And also, a menu item is a container, which is why we can add stuff to it in the first place. And all containers are components, which is why we can add containers to containers!

The full inheritance chain for JMenu (which is the Swing class for a menu) is:


Using inheritance like this, can be extremely powerful, as it lets us express stuff like "We have many kinds of Components. Some components are Container:s . An AbstractButton is a kind of Container and a JMenuItem is one kind of AbstractButton. A special kind of JMenuItem is the JMenu. You can add JMenuItems to a JMenu, and you can add a JMenu to a JMenu, since JMenu is a kind of JMenuItem."

Chapters on the topic of Inheritance


English videos

Swedish videos


Further reading

Where to go next

Next page has exercises (if any) for this chapter: Inheritance_-_Examples_from_Swing_-_Exercises

« PreviousBook TOCNext »