02 July 2023

Super Keyword In Java - learngreen.net

  The "super" key-word in Java is used to refer to the superclass or parent class of a subclass. It lets in you to access and name methods, variables, and constructors from the superclass within the subclass.

1. Accessing superclass variables:- In case a subclass contains a variable with the equal become aware of as a variable in its superclass, you'll utilize the "super" key-word to distinguish between them and get to the superclass variable.


2. Invoking superclass strategies:- If a subclass overrides a strategy from its superclass, you'll utilize the "super" keyword to invoke the superclass method.


This is beneficial when you pick to add some extra performance to the overridden technique whilst nonetheless making use of the unique conduct of the superclass method.


3. Calling superclass constructors:- In a subclass constructor, you can use the "super" key-word to call a constructor from the superclass

This allows you to reuse the initialization code from the superclass and operate any extra subclass-specific initialization.


By the use of the "super" keyword, you can effectively leverage the functionality and assets supplied via the superclass in the subclass. It promotes code reuse and inheritance, enabling you to construct greater modular and extensible Java programs.


Super Keyword In Java


 



 /* Example A.
  * Accessing variable having same
  * name in Super and Nested Class
  * using Super keyword.
  */
 package Interview;
 public class MyClass {
 int a = 5;  
 //Instance variable a with initial value of 5
 public class MyClass1 extends MyClass {
 // Nested class MyClass1 extending MyClass	 
 int a = 8; 
 // Instance variable a with initial value of 8        
 void display() {
 // Method to display values of a	 
 System.out.println(a);
 //Print the value of a in MyClass1 (8)
 System.out.println(super.a);
 //Print the value of a in the superclass MyClass(5)
         }
     }
 public static void main(String[] args) {
 MyClass obj = new MyClass();
 //Create an object of the outer class MyClass
 MyClass1 obj1 = obj.new MyClass1();
 /* Create an object of the nested
  * class MyClass1 using the outer class object
  */
 obj1.display();
 //Call the display()method
 //Output-> 8
 //         5
     }
 }

 



 //Example B
 
 package Interview;
 public class ClassMain {
 public void print() {
 //create print method	 
 System.out.println("ClassMain print method");
}
 
 public class sub extends ClassMain {
 //create nested class sub	 
 public void print() {
 //method override	 
 super.print();
 //Invoking ClassMain method
 System.out.println("sub class print method");
 }
 }

 public static void main(String[] args) {
 ClassMain obj = new ClassMain();
 //create object of ClassMain
 sub obj1 = obj.new sub();
 
 /* create object of nested
  * sub class using object of
  * super class(ClassMain)
  * 
  */
 obj1.print();
 //Call print method
 //Output-> ClassMain print method
 //         sub class print method

	}

}

 


 /* Example C
  * Super keyword is used
  * if we want to call 
  * the constructor in subclass
  * (Page)
  */
 package Interview;
 public class Book {
 Book(){
 //Create constructor 
 System.out.println("Main class constructor");
 }
 public class Page extends Book {
 Page() {
 //Create constructor 
 super();
 //Called Main class(Book)constructor
 System.out.println("Subclass constructor");
	 }
 }
 public static void main(String[] args) {
 Book obj = new Book();
 Page obj1 = obj.new Page();
 //Output->
 /* Main class constructor
    Main class constructor
    Subclass constructor
  */
  }
 }

 



No comments:

Post a Comment