concept super in category java

appears as: super
Java SE 11 Programmer I Certification Guide

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide.

  • Using super and this to access objects and constructors
  • In the previous section, I discussed how this refers to the object instance itself. Similarly, super is also an object reference, but super refers to the direct parent or base class of a class. Think of the words my parent, my base: anyone using those terms is always referring to their direct parent or the base class, as shown in figure 10.17.

    Figure 10.17 When a class mentions super, it refers to its direct parent or the base class.

    The keywords super and this are implicit object references. Because static methods belong to a class, not to objects of a class, you can’t use this and super in static methods. Code that tries to do so won’t compile:

    class Employee {
        String name;                           #A
    }
    class Programmer extends Employee {
        String name;                            #B
        static void setNames() {
            this.name = "Programmer";          #C
            super.name = "Employee";           #D
        }
    }

    It’s time to attempt the next Twist in the Tale exercise, using the this and super keywords (answer in the appendix).

    Get Programming with Java

    This is an excerpt from Manning's book Get Programming with Java.

    14.3   Using the keyword super

    In the previous example, each of the subclasses of Book uses the keyword super. When a class extends another class, it automatically inherits access to all public and protected instance data and methods of the super class, including the class constructor. The constructor is the method used to create an object and provide values for the instance data.

    OCA Java SE 8 Programmer I Certification Guide

    This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

    Purpose: If there is no collision with the name of a variable defined in the base class or derived class, the variable can be accessed using both super and this references from a derived class. If there is a collision, the base class variable can be accessed using the super reference.

  • Using super and this to access objects and constructors
  • In the previous section, I discussed how this refers to the object instance itself. Similarly, super is also an object reference, but super refers to the direct parent or base class of a class. Think of the words my parent, my base: anyone using those terms is always referring to their direct parent or the base class, as shown in figure 6.26.

    Figure 6.26. When a class mentions super, it refers to its direct parent or the base class.