Yahoo India Web Search

Search results

  1. Dec 15, 2023 · The super keyword in Java is a reference variable that is used to refer to parent class when we’re working with objects. You need to know the basics of Inheritance and Polymorphism to understand the Java super keyword. The Keyword “super” came into the picture with the concept of Inheritance.

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

  3. 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.

  4. Sep 22, 2010 · super() is used to call the immediate parent. super() can be used with instance members, i.e., instance variables and instance methods. super() can be used within a constructor to call the constructor of the parent class. OK, now let’s practically implement these points of super().

    • 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(); } }
  5. Sep 14, 2021 · 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.

  6. People also ask

  7. What is super in Java? Super is a keyword of Java which refers to the immediate parent of a class and is used inside the subclass method definition for calling a method defined in the superclass. A superclass having methods as private cannot be called. Only the methods which are public and protected can be called by the keyword super.