super keyword – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 27 Dec 2020 17:05:47 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg super keyword – Programmerbay https://programmerbay.com 32 32 What are Three Uses of Super Keyword ? https://programmerbay.com/what-are-three-uses-of-super-keyword/ https://programmerbay.com/what-are-three-uses-of-super-keyword/#respond Tue, 11 Aug 2020 16:17:58 +0000 https://www.programmerbay.com/?p=4526 What is Super keyword ?
  1. In the Inheritance concept, when a subclass object calls its instance member function which can consist of implicit reference variables this or super.
  2. The difference between this and super is that this reference variable is of subclass type, whereas super reference variable is of superclass type.
  3. Call to super is made first prior to the constructor of the subclass. In other words, the constructor of the parent class always executes before the child class constructor.
  4. If the subclass constructor does not explicitly invoke its superclass constructor then the compiler automatically puts the super statement in the constructor of the subclass.

What are the Three Uses of Super Keyword ?

  • If a child class overrides one of its parent class’s methods, you can invoke that overridden method through the use of the keyword super. In other words, Whenever there is a method having the same name in both parent and child class. You can invoke the method of a parent class by putting the super keyword in front of the name of the overridden method which would refer to the parent’s class method.
  • Super keyword helps in resolving the name conflict between member variables of superclass and subclass having the same name.
  • Passing values through constructor while creating an object of the subclass, it is also essential to provide values to superclass variables too. For this, we can call super to pass the values to the superclass constructor.

Java program to show the usage of Super keyword

Program:

class A
{
  int z;
    A (int a)
  {
    z = a;
  } public void f1 ()
  {
  }

}

class B extends A
{
  int z;
    B (int a, int b)
  {
    super (a);    //call for super class constructor
    z = b;
  } public void f1 ()
  {
    super.f1 ();    //call for super class method } 
    public void f2 ()
    {
      super.z = 4;   //call for super class variable 
  }
      
  } 
  public class Example
  {
    public static void main (String[]args)
    {
      B obj = new B ();
      B obj = new B (5, 3);
        obj.f1 ();
        obj.f2 ();
  }
      
  }

 

]]>
https://programmerbay.com/what-are-three-uses-of-super-keyword/feed/ 0
Difference between this and super keywords in Java? https://programmerbay.com/difference-between-this-and-super-keywords-in-java/ https://programmerbay.com/difference-between-this-and-super-keywords-in-java/#respond Thu, 16 Jan 2020 16:49:31 +0000 https://programmerbay.com/?p=5886 Java provides two useful implicit reference variables ‘this’ and ‘super’. Both are used to resemble the reference variable pointing to the superclass or subclass.

Both keywords are frequently used and if you want to read them thoroughly links are provided here. ( for ‘this’ keyword and for ‘super’ keyword).

The basic difference between this and super is that this reference variable is of subclass type, whereas super reference variable is of superclass type

Difference between this and super keywords in Java

'this' keyword'super' keyword
this keyword is an implicit reference variable ( that holds physical address of an object) that is used to refer current class objectssuper keyword is an implicit reference variable ( that holds physical address of an object) that is used to refer child class's immediate parent class objects
It invokes the constructor of subclass It invokes the constructor of superclass
In the case of the overridden methods( a method which is present in both parent and child with the same signature but with meaningful implementation), 'this' keyword is used to invoke the method of subclass classIn the case of the overridden method( a method which is present in both parent and subclass with the same signature but with meaningful implementation), 'super' keyword is used to invoke the method of superclass
It solves the problem of variable hiding. Using 'this' keyword, a compiler always refers to instance variable of the current classUsing super keyword, compiler always refer to immediate parent class instance variable ( in case of a variable having the same name in parent and subclass)
It can be used to return and pass as an argument in context of current class objectIt can be used to return and pass as an argument in context of parent class object

This keyword

In Java, this keyword is used to refer to the current class object.

There are various situations where sometimes is important to mention the object that actually invoked a particular method, constructor and initialization of instance variable. It can be used to invoke current class constructor, method, instance variable and many more.

Example of ‘this’ keyword

class sample1{
int m;
int n;

sample1()
{
m=5;
n=10;
}

void display()
{
System.out.println(" Value of M = "+m+" Value of N = "+n);
}
}

class sample extends sample1{
int x;
int y;
sample(int a,int b)
{
x=a;
y=b;

}
void callchecker()
{
this.display();
}
@Override
void display()
{
System.out.println(" Value of x = "+x+" Value of Y = "+y);
}
}
public class ThisKeyword {
public static void main(String[] args) {
sample obj = new sample(10,20);
obj.callchecker();
}

}

Super keyword

In Java, super keyword is used to refer to the parent class object. It deals with various situation, but the most importantly, it helps in resolving the name conflict between member variables of superclass and subclass having same name.

Example of ‘super’ keyword

class A 
{ 
int z; 
A(int a) {
 z=a; 
}
 public void f1() { } 
}
 class B extends A
 {
 int z;
 B(int a, int b) 
{
 super(a); //call for super class constructor
 z=b; 
} 
public void f1() 
{
 super.f1(); //call for super class method 
} 
public void f2() {
 super.z=4; //call for super class variable 
} } 
public class Example { 
public static void main(String[] args)
 { 
B obj = new B(); 
B obj = new B(5,3); 
obj.f1(); 
obj.f2(); 
} }

]]>
https://programmerbay.com/difference-between-this-and-super-keywords-in-java/feed/ 0