Super Keyword in Java

Last updated on Dec 29 2022
Prabhas Ramanathan

The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Table of Contents

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

java 123

 

1) super is used to refer immediate parent class instance variable.

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.

1. class Animal{ 
2. String color="white"; 
3. } 
4. class Dog extends Animal{ 
5. String color="black"; 
6. void printColor(){ 
7. System.out.println(color);//prints color of Dog class 
8. System.out.println(super.color);//prints color of Animal class 
9. } 
10. } 
11. class TestSuper1{ 
12. public static void main(String args[]){ 
13. Dog d=new Dog(); 
14. d.printColor(); 
15. }}

 

Output:
black
white

In the above example, Animal and Dog both classes have a common property color. If we print color property, it will print the color of current class by default. To access the parent property, we need to use super keyword.

2) super can be used to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

1. class Animal{ 
2. void eat(){System.out.println("eating...");} 
3. } 
4. class Dog extends Animal{ 
5. void eat(){System.out.println("eating bread...");} 
6. void bark(){System.out.println("barking...");} 
7. void work(){ 
8. super.eat(); 
9. bark(); 
10. } 
11. } 
12. class TestSuper2{ 
13. public static void main(String args[]){ 
14. Dog d=new Dog(); 
15. d.work(); 
16. }}

 

Output:
eating…
barking…

In the above example Animal and Dog both classes have eat() method if we call eat() method from Dog class, it will call the eat() method of Dog class by default because priority is given to local.

To call the parent class method, we need to use super keyword.

3) super is used to invoke parent class constructor.

The super keyword can also be used to invoke the parent class constructor. Let’s see a simple example:

1. class Animal{ 
2. Animal(){System.out.println("animal is created");} 
3. } 
4. class Dog extends Animal{ 
5. Dog(){ 
6. super(); 
7. System.out.println("dog is created"); 
8. } 
9. } 
10. class TestSuper3{ 
11. public static void main(String args[]){ 
12. Dog d=new Dog(); 
13. }}

 

Output:
animal is created
dog is created

Note: super() is added in each class constructor automatically by compiler if there is no super() or this().

java 124

As we know well that default constructor is provided by compiler automatically if there is no constructor. But, it also adds super() as the first statement.

Another example of super keyword where super() is provided by the compiler implicitly.

1. class Animal{ 
2. Animal(){System.out.println("animal is created");} 
3. } 
4. class Dog extends Animal{ 
5. Dog(){ 
6. System.out.println("dog is created"); 
7. } 
8. } 
9. class TestSuper4{ 
10. public static void main(String args[]){ 
11. Dog d=new Dog(); 
12. }}

 

Output:
animal is created
dog is created

super example: real use

Let’s see the real use of super keyword. Here, Emp class inherits Person class so all the properties of Person will be inherited to Emp by default. To initialize all the property, we are using parent class constructor from child class. In such way, we are reusing the parent class constructor.

1. class Person{ 
2. int id; 
3. String name; 
4. Person(int id,String name){ 
5. this.id=id; 
6. this.name=name; 
7. } 
8. } 
9. class Emp extends Person{ 
10. float salary; 
11. Emp(int id,String name,float salary){ 
12. super(id,name);//reusing parent constructor 
13. this.salary=salary; 
14. } 
15. void display(){System.out.println(id+" "+name+" "+salary);} 
16. } 
17. class TestSuper5{ 
18. public static void main(String[] args){ 
19. Emp e1=new Emp(1,"ankit",45000f); 
20. e1.display(); 
21. }}

 

Output:
1 ankit 45000

 

So, this brings us to the end of blog. This Tecklearn ‘Super Keyword in Java’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Super Keyword in Java"

Leave a Message

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