Table of Contents
Example h2>
Output
Home Java javaTutorial super keyword in Java

super keyword in Java

Sep 16, 2023 pm 10:57 PM
inheritance super (super class) override

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the code hierarchical relationship problem in C++ development How to solve the code hierarchical relationship problem in C++ development Aug 22, 2023 am 11:22 AM

How to solve the code hierarchical relationship problem in C++ development. When developing complex C++ programs, a common problem is the management of code hierarchical relationships. Incorrect hierarchies can make code difficult to read, maintain, and extend. In order to solve this problem, we can adopt the following strategies. First, we can use a suitable directory structure to organize the code files. A good directory structure can arrange code files in a more orderly manner, making it easier for us to quickly locate or modify related code during the development process. Generally, it is recommended to

Solutions to common code reuse problems in C++ Solutions to common code reuse problems in C++ Oct 09, 2023 pm 01:50 PM

Solutions to common code reuse problems in C++ In C++ programming, code reuse is an important technology that can improve development efficiency and code maintainability. However, we often encounter some common code reuse problems, such as repeated code fragments, complex inheritance relationships, etc. This article will introduce several common methods to solve these problems and provide specific code examples. Function encapsulation Function encapsulation is a common code reuse method. By encapsulating a piece of code into a function, it can be called multiple times in other places to avoid writing the same code repeatedly.

Abstract classes in Java Abstract classes in Java Sep 22, 2023 am 11:53 AM

A class that contains the abstract keyword in its declaration is called an abstract class. An abstract class may or may not contain abstract methods, i.e. methods without a body (publicvoidget();) However, if a class has at least one abstract method, the class must be declared abstract. If a class is declared abstract, it cannot be instantiated. To use an abstract class, you must inherit it from another class and provide implementations of the abstract methods in it. If you inherit from an abstract class, you provide implementations for all abstract methods in it. Examples This section provides you with examples of abstract classes. To create an abstract class, just use the abstract keyword before the class keyword in the class declaration. /*Filename:Emplo

How to do object-oriented programming in C++? How to do object-oriented programming in C++? Aug 27, 2023 am 08:34 AM

How to do object-oriented programming in C++? Object-Oriented Programming (OOP) is a very common and important software development paradigm. C++ is a multi-paradigm programming language that includes support for object-oriented programming. In C++, through the concepts of class and object, we can easily implement object-oriented programming. First, we need to define a class. Class is a custom

How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? Sep 06, 2023 pm 01:40 PM

How to achieve better code abstraction through forced inheritance of proxy final classes in Java programming? Introduction: In Java programming, we often face situations where we need to extend existing classes. However, sometimes the classes we encounter are declared final, that is, they cannot be inherited. So, how to achieve code abstraction and reuse in this case? This article will introduce a method of delegating final classes through forced inheritance to achieve better code abstraction while maintaining code security. Limitations of final classes in Java

super keyword in Java super keyword in Java Sep 16, 2023 pm 10:57 PM

The super variable refers to the direct parent class instance. Super variables can call direct parent class methods. super() acts as a direct parent class constructor and should be on the first line in a child class constructor. When calling the superclass version of an overridden method, use the super keyword. Example live demonstration classAnimal{ publicvoidmove(){ System.out.println("Animalscanmove"); }}cl

What are the rules for method coverage and exception handling in Java? What are the rules for method coverage and exception handling in Java? Sep 06, 2023 pm 06:29 PM

When overriding a superclass method, you need to follow certain rules if the method throws an exception. Should throw the same exception or a subtype If a superclass method throws a certain exception, the method in the subclass should throw the same exception or its subtype. Example In the following example, the superclass's readFile() method throws an IOException, while the subclass's readFile() method throws a FileNotFoundException. Since the FileNotFoundException exception is a subtype of IOException, the program compiles and executes without any errors. importjava.io.File;

How does PHP use object-oriented thinking to solve problems? How does PHP use object-oriented thinking to solve problems? Jul 01, 2023 pm 02:05 PM

As a popular server-side programming language, PHP is widely used in the field of web development. It has the characteristics of object-oriented programming, allowing developers to use object-oriented thinking to solve problems. This article will explore how PHP uses object-oriented thinking to solve problems. Object-oriented programming is a software development method that breaks down a problem into a series of objects that have properties and methods that interact and collaborate with each other to complete a task. In PHP, we can use classes and objects to implement object-oriented programming. First, we need to define

See all articles