Yahoo India Web Search

Search results

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

    • Characteristics of Super Keyword in Java
    • Use of Super Keyword in Java
    • Use of Super with Variables
    • Use of Super with Methods
    • Use of Super with Constructors
    • Advantages of Using Java Super Keyword

    In Java, super keyword is used to refer to the parent class of a subclass. Here are some of its key characteristics: 1. super is used to call a superclass constructor:When a subclass is created, its constructor must call the constructor of its parent class. This is done using the super() keyword, which calls the constructor of the parent class. 2. ...

    It is majorly used in the following contexts as mentioned below: 1. Use of super with Variables 2. Use of super with Methods 3. Use of super with Constructors

    This scenario occurs when a derived class and base class have the same data members. In that case, there is a possibility of ambiguity r the JVM. We can understand it more clearly using the following example: Example In the above example, both the base class and subclass have a member maxSpeed. We could access the maxSpeed of the base class in subc...

    This is used when we want to call the parent class method. So whenever a parent and child class have the same-named methods then to resolve ambiguity we use the super keyword. This code snippet helps to understand the said usage of the super keyword. Example In the above example, we have seen that if we only call methodmessage() then, the current c...

    The super keyword can also be used to access the parent class constructor. One more important thing is that ‘super’ can call both parametric as well as non-parametric constructors depending on the situation. Following is the code snippet to explain the above concept: Example 1 In the above example, we have called the superclass constructor using th...

    The super keyword in Javaprovides many advantages in object-oriented programming are as follows: 1. Enables reuse of code: Using the super keyword allows subclasses to inherit functionality from their parent classes, which promotes the reuse of code and reduces duplication. 2. Supports polymorphism: Because subclasses can override methods and acces...

    • 12 min
    • 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(); } }
  2. Learn how to use the super keyword in Java to call superclass methods and access superclass constructors. See an example of how to use super to call the animalSound method in a subclass of Animal.

  3. Sample Code. This section provides you a program that demonstrates the usage of the super keyword. In the given program, you have two classes namely Sub_class and Super_class, both have a method named display () with different implementations, and a variable named num with different values.

  4. People also ask

  5. Jun 10, 2024 · super and this keyword super() as well as this() keyword both are used to make constructor calls. super() is used to call Base class's constructor(i.e, Parent's class) while this() is used to call the current class's constructor.