Home > Java > javaTutorial > body text

super keyword in Java

PHPz
Release: 2023-09-16 22:57:03
forward
1256 people have browsed it

super keyword in Java

  • #The super variable refers to the direct parent class instance.
  • Super variables can call direct parent class methods.
  • super() acts as the direct parent class constructor and should be the first line in the child class constructor.

Use the super keyword when calling the superclass version of an overridden method.

Example h2>

Live Demonstration

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}
class Dog extends Animal {
   public void move() {
      super.move(); // invokes the super class method
      System.out.println("Dogs can walk and run");
   }
}
public class TestDog {
   public static void main(String args[]) {
      Animal b = new Dog(); // Animal reference but Dog object
      b.move(); // runs the method in Dog class
   }
}
Copy after login

Output

This will produce the following results -

Animals can move
Dogs can walk and run
Copy after login

The above is the detailed content of super keyword in Java. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template