Skip to main content

Java Super Keyword with Example

Java super keyword is used to access the method or member variables(instance variables) from the parent class.

 Use of Super keyword
  1. super.instance_variable_name is used to refer to the parent class data members(instance variables).
  2. super.method_name is used to access the parent class methods.
  3. super() is used to refer to the parent class constructor.
1.super.instance_variable_name is used to refer to the parent class instance variables.
Let's have a look at the below example, in Parent class and Child class we have the same instance variable called "x" and we have called super.x in the Child class method(showValue() method). from the output, we can conclude that we can access the Parent class instance variables by usingsuper.instance_variable_name.
Example
class Parent {
 int x = 1000;

}

public class Child extends Parent {

 private int x = 2000;

 public void showValue() {
  System.out.println("parent instace variable value:" + super.x);
  System.out.println("child  instace variable value:" + x);
 }

 public static void main(String[] args) {
  Child c = new Child();
  c.showValue();

 }

}
output

2.super.method_name is used to refer to the Parent class methods
when we override the parent class method in the child class we can access the parent class method in child class by using super.method_name. Let's have a look at the below example

Example
 class Parent {
 public void printMethod() {
  System.out.println("parent  method");
 }

}
public class Child extends Parent {

 public void printMethod() {
  System.out.println("child  method");
 }

 public void showValue() {
  printMethod();
  super.printMethod();
 }

 public static void main(String[] args) {
  Child c = new Child();
  c.showValue();

 }

}
output

3.super() is used to refer to the parent class constructor.
super() will call the parent class constructor i.e. whenever we create the child class Object JVM will invoke the constructor of the child class which will internally call the parent class constructor. Let's have a look at the below example
Example
class Parent {

 public Parent() {
  System.out.println("parent class constructor");
 }

}
public class Child extends Parent {

 public Child() {
  super();
  System.out.println("child class constructor");
 }

 public static void main(String[] args) {
  Child c = new Child();

 }

}
output

Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...