Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Super Keyword in Java

    In the Java programming language, the keyword "super" is utilized as a reference variable to refer the immediate parent class of an object.

    When you create a subclass instance, an implicit instance of the parent class is also created and is referred to by the super reference variable.
    With the concept of Inheritance, the keyword "super" entered the picture.

Table Of Content

  • Super Keyword in Java
  • Use of super keyword with variables
  • Use of super keyword with methods
  • Use of super keyword with constructors


Usage of super Keyword

  • Use of super keyword with variables
  • Use of super keyword with methods
  • Use of super keyword with constructors





1. Use of super keyword with variables


To access the data member or field of the parent class, we can utilize the "super" keyword. This keyword work when both the parent and child classes have fields with the same name.


Java program uses of super keyword with variables
class Vehicle{  
	int maxSpeed = 90;
}  
class Bike extends Vehicle{  
    int maxSpeed = 140; 
    void display(){
    	System.out.println("Bike Class - Maximum Speed: "+ maxSpeed);
        System.out.println("Vehicle Class - Maximum Speed: "+ super.maxSpeed); //print Vehicle class maxSpeed variable 
    }
}   
class alpha{  
	public static void main(String args[]){  
	Bike bike=new Bike();  
	bike.display();
	}
} 




Output:

Bike Class - Maximum Speed: 140 
Vehicle Class - Maximum Speed: 90 

2. Use of super keyword with methods


This is used to invoke the parent class method. So, whenever a parent and child class have methods with the same name, we use the super keyword to resolve ambiguity.


class Vehicle{  
	int maxSpeed = 90;
    void SpeedDisplay(){
    	System.out.println("Vehicle Class - Maximum Speed: "+ maxSpeed);
    }
}  
class Bike extends Vehicle{  
    int maxSpeed = 140;
    void SpeedDisplay(){
    	System.out.println("Bike Class - Maximum Speed: "+ maxSpeed);
    }
    void display(){
    	SpeedDisplay();
    	super.SpeedDisplay(); // Vehicle class method get print
    }
}   
class alpha{  
	public static void main(String args[]){  
	Bike bike=new Bike();  
	bike.display();
	}
}




Output:

Bike Class - Maximum Speed: 140 
Vehicle Class - Maximum Speed: 90  

3. Use of super keyword with constructors


The "super" keyword has the ability to access the constructor of the parent class. Another important distinction is that, depending on the situation,'super' can invoke both parametric and non-parametric constructors.


class Vehicle{  
	int maxSpeed = 90;
	Vehicle(){
         System.out.println("Vehicle Class - Maximum Speed: "+ maxSpeed);
    }
}  
class Bike extends Vehicle{  
    int maxSpeed = 140;
    Bike(){
      super();
      System.out.println("Bike Class - Maximum Speed: "+ maxSpeed);
    }
}   
class alpha{  
	public static void main(String args[]){  
	  Bike bike=new Bike();  
	}
} 




Output:

Bike Class - Maximum Speed: 140 
Vehicle Class - Maximum Speed: 90  

Usage of super Keyword

  • Since the superclass has no knowledge of any subclass, any initialization it has to undertake is distinct from and possibly prerequisite to any initialization carried out by the subclass, hence the call to super() must be the first sentence in the Derived(Student) Class constructor. It must therefore finish its execution first.
  • In Java, when you create a subclass and if you don't explicitly call any of the superclass's constructors, the compiler automatically adds a call to the no-argument constructor of its superclass.This implies that the compiler will call the superclass's constructor on your behalf if you don't write any code for it.The superclass must have a no-argument constructor in order for this automatic insertion to occur. A compile-time error will appear if the superclass only has constructors with parameters and lacks a no-argument constructor.In these case , you must use the super keyword to explicitly call one of the constructors of the superclass from the subclass.
  • If a subclass constructor explicitly or implicitly invokes a constructor of its superclass, you might think that a whole chain of constructors is called, all the way back to the constructor of Object. This is, in fact, the case. It is known as constructor chaining.
Java Final keyword Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Difference between Early and Late Binding in Java
Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.