Yahoo India Web Search

Search results

  1. Dec 15, 2023 · In java, super keyword is used to access methods of the parent class while this is used to access methods of the current class. this keyword is a reserved keyword in java i.e, we can't use it as an identifier.

    • 12 min
  2. 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.

  3. The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name.

    • Method overriding. class Animal { // overridden method public void display(){ System.out.println("I am an animal"); } } class Dog extends Animal { // overriding method @Override public void display(){ System.out.println("I am a dog"); } public void printMessage(){ display(); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.printMessage(); } }
    • super to Call Superclass Method. class Animal { // overridden method public void display(){ System.out.println("I am an animal"); } } class Dog extends Animal { // overriding method @Override public void display(){ System.out.println("I am a dog"); } public void printMessage(){ // this calls overriding method display(); // this calls overridden method super.display(); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.printMessage(); } }
    • Access superclass attribute. class Animal { protected String type="animal"; } class Dog extends Animal { public String type="mammal"; public void printType() { System.out.println("I am a " + type); System.out.println("I am an " + super.type); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.printType(); } }
    • Use of super() class Animal { // default or no-arg constructor of class Animal Animal() { System.out.println("I am an animal"); } } class Dog extends Animal { // default or no-arg constructor of class Dog Dog() { // calling default constructor of the superclass super(); System.out.println("I am a dog"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); } }
  4. Jan 8, 2024 · In this quick tutorial, we’ll take a look at the super Java keyword. Simply put, we can use the super keyword to access the parent class. Let’s explore the applications of the core keyword in the language.

  5. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below. super(values); Sample Code. The program given in this section demonstrates how to use the super keyword to invoke the parametrized constructor of the superclass.

  6. People also ask

  7. Jun 10, 2024 · super is used to refer super-classs instance as well as static members. super is also used to invoke super-class’s method or constructor. super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.