• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Java super keyword

January 14, 2015 by javainterviewpoint Leave a Comment

In our previous discussions, we have learnt about this keyword in Java. In this article we will see ‘What is super keyword in Java’. We will walk through all possible scenarios to use ‘super’ keyword in Java programming.

Usage of super keyword

1. super() invokes the constructor of the parent class.
2. super.variable_name refers to the variable in the parent class.
3. super.method_name refers to the method of the parent class.

Let’s discuss those things in detail

1. super() invokes the constructor of the parent class

super() will invoke the constructor of the parent class.Before getting into that we will go through the default behavior of the compiler. Even when you don’t add super() keyword the compiler will add one and will invoke the Parent Class constructor.

class ParentClass
{
	public ParentClass()
	{
		System.out.println("Parent Class default Constructor");
	}
}
public class SubClass extends ParentClass
{
	public SubClass()
	{
		System.out.println("Child Class default Constructor");
	}
	public static void main(String args[])
	{
		SubClass s = new SubClass();
	}
}

Output

Parent Class default Constructor
Child Class default Constructor

Even when we add explicitly also it behaves the same way as it did before.

class ParentClass
{
	public ParentClass()
	{
		System.out.println("Parent Class default Constructor");
	}
}
public class SubClass extends ParentClass
{
	public SubClass()
	{
                super();
		System.out.println("Child Class default Constructor");
	}
	public static void main(String args[])
	{
		SubClass s = new SubClass();
	}
}

Output

Parent Class default Constructor
Child Class default Constructor

You can also call the parameterized constructor of the Parent Class. For example, super(10) will call parameterized constructor of the Parent class.

class ParentClass
{
	public ParentClass()
	{
		System.out.println("Parent Class default Constructor called");
	}
	public ParentClass(int val)
	{
		System.out.println("Parent Class parameterized Constructor, value: "+val);
	}
}
public class SubClass extends ParentClass
{
	public SubClass()
	{
		super();//Has to be the first statement in the constructor
		System.out.println("Child Class default Constructor called");
	}
	public SubClass(int val)
	{
		super(10);
		System.out.println("Child Class parameterized Constructor, value: "+val);
	}
	public static void main(String args[])
	{
		//Calling default constructor
		SubClass s = new SubClass();
		//Calling parameterized constructor
		SubClass s1 = new SubClass(10);
	}
}

Output

Parent Class default Constructor called
Child Class default Constructor called
Parent Class parameterized Constructor, value: 10
Child Class parameterized Constructor, value: 10

2. super.variable_name refers to the variable in the parent class

Let’s look into the below example, here we have the same variable in both parent and subclass

class ParentClass
{
	int val=999;
}
public class SubClass extends ParentClass
{
	int val=123;
	
	public void disp()
	{
		System.out.println("Value is : "+val);
	}
	
	public static void main(String args[])
	{
		SubClass s = new SubClass();
		s.disp();
	}
}

Output

Value is : 123

This will call only the val of the sub class only. Without super keyword, you cannot call the val which is present in the Parent Class.

class ParentClass
{
	int val=999;
}
public class SubClass extends ParentClass
{
	int val=123;
	
	public void disp()
	{
		System.out.println("Value is : "+super.val);
	}
	
	public static void main(String args[])
	{
		SubClass s = new SubClass();
		s.disp();
	}
}

Output

Value is : 999

3. super.method_nae refers to the method of the parent class

When you override the Parent Class method in the Child Class without super keywords support you will not be able to call the Parent Class method. Let’s look into the below example

class ParentClass
{
	public void disp()
	{
		System.out.println("Parent Class method");
	}
}
public class SubClass extends ParentClass
{
	
	public void disp()
	{
		System.out.println("Child Class method");
	}
	
	public void show()
	{
		disp();
	}
	public static void main(String args[])
	{
		SubClass s = new SubClass();
		s.show();
	}
}

Output:

Child Class method

Here we have overridden the Parent Class disp() method in the SubClass and hence SubClass disp() method is called. If we want to call the Parent Class disp() method also means then we have to use the super keyword for it.

class ParentClass
{
	public void disp()
	{
		System.out.println("Parent Class method");
	}
}
public class SubClass extends ParentClass
{
	
	public void disp()
	{
		System.out.println("Child Class method");
	}
	
	public void show()
	{
		//Calling SubClass disp() method
		disp();
		//Calling ParentClass disp() method
		super.disp();
	}
	public static void main(String args[])
	{
		SubClass s = new SubClass();
		s.show();
	}
}

Output

Child Class method
Parent Class method

When there is no method overriding then by default Parent Class disp() method will be called.

class ParentClass
{
	public void disp()
	{
		System.out.println("Parent Class method");
	}
}
public class SubClass extends ParentClass
{
	public void show()
	{
		disp();
	}
	public static void main(String args[])
	{
		SubClass s = new SubClass();
		s.show();
	}
}

Output

Parent Class method

Other interesting articles which you may like …

  • Encapsulation in Java with Example
  • Constructor in Java and Types of Constructors in Java
  • Final Keyword in Java | Java Tutorial
  • Java Static Import
  • Java – How System.out.println() really work?
  • Java Ternary operator
  • Java newInstance() method
  • Java Constructor.newInstance() method Example
  • Parameterized Constructor in Java with Example
  • Sort Objects in a ArrayList using Java Comparator
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Difference between equals() and ==
  • Difference Between Interface and Abstract Class in Java
  • Difference between fail-fast and fail-safe Iterator
  • Difference between Enumeration and Iterator ?
  • Difference between HashMap and Hashtable | HashMap Vs Hashtable

Filed Under: Core Java, Java, OOPs Tagged With: Java, super keyword

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2021 · Java Interview Point - All Rights Are Reserved ·