# "super" keyword in Java

In 
Published 2022-12-23

This tutorial explains to you how super keyword in Java.

# "super" keyword in Java - theory

Method overriding in Java appears when we declare a method in subclass which is already present in parent class. The main advantage of method overriding in Java is that the class can give its own specific implementation to an inherited method without even modifying the parent class.

# "super" keyword in Java - example

Here it is an example:

  • defining the Shape class:
Shape.java
public class Shape {

    public void drawShape(){
        System.out.println("Draw shape from Shape Class.");
    };
}
  • extend the Shape class and use the super keyword
Triangle.java
public class Triangle extends Shape {

    public void drawShape() {
        System.out.println("Draw shape from Triangle Class.");
        super.drawShape();
    }

}
  • define the main application
MainApp.java
public class MainApp {

    public static void main(String [] args) {
        Triangle triangle = new Triangle();
        triangle.drawShape();
 
    }
}

When you run the main class you will see:

Draw shape from Triangle Class.
Draw shape from Shape Class.

Process finished with exit code 0