blog posts

Learn Super Keyword In Java (In Simple Language)

The super keyword in Java is used under subclasses to access members of the superclass (attributes, constructs, and methods).

Before we learn about the super keyword, be sure to read about the inheritance in Java that we described in the previous tutorials.

Use the Super Keyword In Java

1- To call superclass methods that are below the class.

2- To access the features (fields) of the superclass if both the superclass and the subclass have the same name.

3- To call the superclass constructor without arguments (default) or the parametric constructor instead of the subclass constructor.

Let’s understand each of these applications.

1- Access to superclassed overridden methods

If methods with the same name are defined in both the superclass and the subclass, the subclass method ignores the superclass method, which is called method overriding.

Example 1: method overriding

  1. class Animal {
  2. // overridden method
  3. public void display () {
  4. System.out.println (“I am an animal”);
  5. }
  6. }
  7. class Dog extends Animal {
  8. // overriding method
  9. @Override
  10. public void display () {
  11. System.out.println (“I am a dog”);
  12. }
  13. public void printMessage () {
  14. display ();
  15. }
  16. }
  17. class Main {
  18. public static void main (String [] args) {
  19. Dog dog1 = new Dog ();
  20. dog1.printMessage ();
  21. }
  22. }

Output

I am a dog

In this example, by creating a dog1 object from the Dog class, we can call the printMessage () method, which executes the display () method.

Because display () is defined in both classes, the Dog subclass method ignores the Animal superclass method. Hence, the display () is called below the class.

C: \ Users \ Mr \ Desktop \ java-overriding-example.png

What happens if the superclass call method is called?

If you need to call the display method in the Animal superclass, use super.display ().

Example 2: Calling a superclass method

  1. class Animal {
  2. // overridden method
  3. public void display () {
  4. System.out.println (“I am an animal”);
  5. }
  6. }
  7. class Dog extends Animal {
  8. // overriding method
  9. @Override
  10. public void display () {
  11. System.out.println (“I am a dog”);
  12. }
  13. public void printMessage () {
  14. // this calls overriding method
  15. display ();
  16. // this calls overridden method
  17. super.display ();
  18. }
  19. }
  20. class Main {
  21. public static void main (String [] args) {
  22. Dog dog1 = new Dog ();
  23. dog1.printMessage ();
  24. }
  25. }

Output

I am a dog

I am an animal

Here, how the app works above is shown.

C: \ Users \ Mr \ Desktop \ call-superclass-method.png

2- Access to superclass methods

Superclass and subclass can have methods of the same name. We use the super keyword to access the superclass method.

Example 3: Access the Superclass feature

  1. class Animal {
  2. protected String type = ”animal”;
  3. }
  4. class Dog extends Animal {
  5. public String type = ”mammal”;
  6. public void printType () {
  7. System.out.println (“I am a” + type);
  8. System.out.println (“I am an” + super.type);
  9. }
  10. }
  11. class Main {
  12. public static void main (String [] args) {
  13. Dog dog1 = new Dog ();
  14. dog1.printType ();
  15. }
  16. }

When we run the program, the output will be equal to:

I am a mammal

I am an animal

In this example, we have defined a similar type field in both the Animal superclass and the Dog subclass.

Then we created a dog1 object from the Dog class and called the printType () method using this object.

Inside the function, printType ()

  • type refers to a property under the Dog class.
  • super.type refers to the Animal superclass feature.

Prints System.out.println (“I am a” + type); The phrase I am a mammal here

And

System.out.println (“I am an” + super.type); The phrase I am an animal

Prints.

3- Use super () to access the superclass builder

As we all know, when an object of class is created, the default constructor is automatically called.

To call the superclass constructor, we use super () instead of the subclass constructor. (Special shape of the super keyword)

Example 4: Using super ()

  1. class Animal {
  2. // default or no-arg constructor of class Animal
  3. Animal () {
  4. System.out.println (“I am an animal”);
  5. }
  6. }
  7. class Dog extends Animal {
  8. // default or no-arg constructor of class Dog
  9. Dog () {
  10. // calling default constructor of the superclass
  11. super ();
  12. System.out.println (“I am a dog”);
  13. }
  14. }
  15. class Main {
  16. public static void main (String [] args) {
  17. Dog dog1 = new Dog ();
  18. }
  19. }

Output

I am an animal

I am a dog

When the dog1 object is created from the Dog class, it automatically invokes the default constructor of the class without arguments.

Inside the subclass constructor, it calls the superclass constructor () statement and executes the commands inside it. Here we see the output of I am an animal.

C: \ Users \ Mr \ Desktop \ super () - example.png

The program stream then returns to the subclass constructor and executes the remaining commands.

Therefore, I am a dog is published.

Use of super () is not mandatory. Even if super () is not used in the subclass constructor, the compiler automatically calls the default superclass constructor.

If the compiler automatically calls super (), why use extra code?

If we want to call the parametric constructor (the constructor that takes the argument) of the superclass instead of the subclass constructor.

The super parameter () must always be the first sentence in the body of the subclass constructor, otherwise we get a compiler error.

Example 5: Calling a parameter constructor using super ()

  1. class Animal {
  2. // default or no-arg constructor
  3. Animal () {
  4. System.out.println (“I am an animal”);
  5. }
  6. // parameterized constructor
  7. Animal (String type) {
  8. System.out.println (“Type:” + type);
  9. }
  10. }
  11. class Dog extends Animal {
  12. // default constructor
  13. Dog () {
  14. // calling parameterized constructor of the superclass
  15. super (“Animal”);
  16. System.out.println (“I am a dog”);
  17. }
  18. }
  19. class Main {
  20. public static void main (String [] args) {
  21. Dog dog1 = new Dog ();
  22. }
  23. }

Output

Type: Animal

I am a dog

The compiler can automatically call an argument-free constructor. However, it cannot call the parameter constructor.

If the parameter constructor is called, it must be explicitly defined inside the subclass.

C: \ Users \ Mr \ Desktop \ parameterized-super-example.png

Note that in the example above, we explicitly called the super (“Animal” parameter constructor. In this case, the compiler does not call the default superclass constructor.