Spread the love

Super Keyword in Java Simplified

Spread the love

Super Keyword
Super Keyword

Super Keyword in Java Simplified:

Java defines a special keyword called super for use in the definition of instance methods.The keyword super is for use in subclass.Super refers to the object that contains the method.It also remembers that it belongs to the super class of that class.
Super can be used in the context of :
  • Super class’s instance variables
  • Super class’s methods
  • Super class’s Constructors
The derived or child class may contain additional and modified versions of super class’s method,variables etc.Super class does not know about any of these changes(additions and modifications).It can only used to refer to methods and variables in the super class.

Super class’s methods:

Lets say that the derived class that we are writing contains an instance method named doSomething(). Consider the method call statement is super.doSomething() in the child class.It signifies that the method belong to super class.So it tries to execute a method called doSomething() from Super class.
If super class does not have a method called soSomething()(addition in child rather than modification), the compiler will throw an syntax error.

If we have a method defined in a super class and a sub class extends the super class, then the sub class inherits the method by the rule of inheritance and it can be used by the object created from sub class.Method inheritance enables us to define and use methods repeatedly in subclasses without defining it again. Now if the object wants to respond to the same method in a different way(different from parent’s class method),we need to override the method definition(defined in super class).When the method is called the redefined method in child class gets executed instead of the method defined in Super class.

class Super{
//variable decleration
int x;
Super(int x){this.x=x;}
//method definitaion
void display(){
System.out.println("Super x "+x);
class Sub extends Super{
int y;
Sub(int x,int y){
void display(){
System.out.println("Sub x "+x);
System.out.println("Sub y "+y);
public class SuperTest{
public static void main(String[] args)
Sub sb=new Sub(10,20);

output of the code:
$javac SuperTest.java
$java -Xmx128M -Xms16M SuperTest
Sub x 10
Sub y 20

READ  Class CharArrayWriter in Java
READ  CharArrayReader Class in Java

Advantages of Super keyword:  

  • We can access to things in the super class that are hidden by things in the sub classes. Suppose we say Super.X it signifies to an instance variable named x defined in Super class.
  • If a class contains an instance variable with the same name as an instance variable in the super class,then an object of that class will actually contains two variables with the same name. One defined as part of the class itself and one defined as part of the super class.The variable defined in the sub class does not replace the variable of the same name defined in the super class.The variable from super can still be accessed with the keyword called super.
  • If we write a method in a subclass that has the same signature and name as of super class,the method from super class is hidden in the same way.We say that the method in the subclass overrides the method from the super class. Using super keyword we can still access and work with the super class method.
  • Re usability of code is the major advantage of super keyword. The major use of super is to overrides a method with new method that extends the behavior of the inherited method,instead replacing the behavior completely.The new method can use super to call the method from super class and then it can add additional functionalities.

Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis of instructions,...


Recent Posts

Learn Quickly About DevOps Tools in a Fast and Efficient Manner

Introduction to DevOps tools DevOps tools are a collection of...

How to Collaborate between Development and Operations Team Fast and Effectively

Collaborate between Development and Operations Teams are the key...

Learn How to Use Jacoco Java code Coverage

Introduction to Jacoco Jacoco provides a wide range of coverage(analysis...


Spread the love