Chapter:Inheritance - Every class extends Object

From Juneday education
Jump to: navigation, search

Meta information about this chapter

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


This chapter discusses the Object class and the implications, such as why we can override methods such as toString(), of this inheritance.


The purpose of this chapter is to use the fact that every class implicitly or explicitly eventually extends Object, in order to explain some of the concepts of inheritance. Another purpose is to inform the students about some of the useful methods inherited from Object (like toString(), equals(), hashCode() etc).


After this lecture, the students shall understand and learn

  • About the class java.lang.Object and that all classes inherit from this class
    • Directly (new classes - either implicitly or explicitly)
    • Indirectly (they either extend a class that extends Object, or they extend a class that has an ancestor who extends Object)
  • That Object is a very high-level abstraction - an abstraction of all Java objects, not of anything in the real world
  • That a reference of type "reference to Object" can be assigned the reference to any type of object, they are all Object types because of the rule above
  • That the type of the reference decides what the legal method calls etc are for an object
    • Object o = new String("Potato - potatoe"); - we can now only use o as if the object it refers to were of class Object, i.e. only call methods on o that are present in Object
  • That it is convenient to have all classes inherit e.g. the toString() method, because we know that every object, regardless of type has that method (because of the rule above)
    • We can choose to change the behavior of e.g. toString() in a class, to make it fit our needs - called overriding
  • What the result of using toString() as is, is - the string looks weird and not so useful
  • That the behavior of the inherited versions of equals() and hashCode() is not what we want - and that we probably want to override those in classes we write
  • That you can, but don't have to, explicitly declare that a class you are writing extends Object
    • Doing so explicitly takes away the possibility to choose a different direct superclass
  • The nature of the methods in Object - that they are very general
    • The methods don't reveal anything about the concrete class of an object - there is no phoneNumber() method in Object
  • That constructors are not inherited
  • That private members are inherited, but the rule about private access permits us to use private members from any other class than where they are declared, so we can't use them directly anyway

Instructions to the teacher

Common problems related to this chapter

  • We think that the biggest problem with teaching inheritance is when to use it. This lecture, however, avoids that by focusing on inheritance by the rule that all classes some way or another extends Object. We also handle this by motivating why this rule exists - it is convenient that all objects ever will have the methods inherited from Object like toString().
  • Students struggle to understand compile-time type (declared type) and runtime type (actual type of the object when the program is executing). We will get back to this in the coming lectures.
    • Hint: Spend some time on the example Object o = new String("Potato - potatoe"); and explain that we can't do String s = o.toLowerCase();, because the type of the reference o is Object and not String - even if the actual object in runtime will be a String. The declared type is like a filter - we are considering the object referred to by o as an Object - and we don't care what the actual type would be when the program is running.
    • A motivation for this rule is quite simple - the compiler can only look at the declared type. If we have a method which has a parameter of type Object, the compiler has no way to know what the actual type will be when the method is called in the future - all references will fit this parameter and be legal.

Full frontal - Code example up front!

This is meant as a code example to get your thoughts going. You don't need to understand it! It is here to sow a seed of syntax in your head, so that you are exposed to some code before you have to read all text etc. You may very well ignore it, if you like to.

public class CustomerDemo {

  public static void main(String[] args) {

    Customer demoCustomer = new Customer("Bob", "");
    String customerTextSummary = demoCustomer.toString();

    // Or even:
    // because println will in the end call toString()
    // for us - and toString() is present in all objects.

    // How did println know that there would be a toString() method
    // to call? It actually accepts an Object, and all objects
    // have a toString() - either inherited from Object - or
    // one they have overridden - changed. That's why!

Inheritance: Every class extends Object

Java is designed so that every class extends the class Object - either directly or indirectly. This means that every object in Java has (by inheritance) all the methods declared in java.lang.Object. An implication of this is that those methods (the public methods) always can be called on any Java object, but be aware that the implementations in Object are very basic and often needs overriding in order to be useful.

As an example, take the toString() method. The version defined in Object simply returns a String consisting of the hash code of the object (in hexadecimal) followed by an @ followed by the qualified class name of the actual object. For this reason, it is common to override toString() in classes you write yourself, so that your objects, should anyone call toString() on them, can return a more descriptive String representation of themselves.

If you write a new class and it's not part of any particular inheritance hierarchy, it will implicitly extend java.lang.Object, even if you don't explicitly declare the class as "extends Object". It is, of course, allowed to explicitly declare that your class extends Object but it isn't needed and not that common.

Looking at the source code for the Java API (the class library included with a JDK installation), we found no class explicitly declaring that it extends Object.

Chapters on the topic of Inheritance


com.ebiz.Customer extends Object and overrides toString()

English videos

Swedish videos

Note that the playlist below doesn't contain the two coding videos above.

Coding videos (included in the English playlist)

  • Live coding I overriding toString(): (eng)
  • Live coding (extra) - Difference between == and boolean equals(Object o): (eng)


Further reading

Where to go next

Next page has exercises for this chapter: Inheritance_-_Every_class_extends_Object_-_Exercises

« PreviousBook TOCNext »