The role of extends in java means inheritance. In Java, an existing class is inherited through the keyword extends. The inherited class is called the parent class [super class, base class]. The new class Classes are called subclasses [derived classes], and multiple inheritance is not allowed in Java.
#Inheritance is the key to understanding object-oriented programming. In Java, an existing class is inherited through the keyword extends. The inherited class is called the parent class (super class, base class), and the new class is called a subclass (derived class). Multiple inheritance is not allowed in Java.
class Animal{ void eat(){ System.out.println("Animal eat"); } void sleep(){ System.out.println("Animal sleep"); } void breathe(){ System.out.println("Animal breathe"); } } class Fish extends Animal{ } public class TestNew { public static void main(String[] args) { // TODO Auto-generated method stub Animal an = new Animal(); Fish fn = new Fish(); an.breathe(); fn.breathe(); } }
Executed in eclipse: Animal breathe! <br>Animal breathe! Each class in the
.java file will be in the folder bin Generate a corresponding .class file. The execution result shows that the derived class inherits all methods of the parent class.
Override
class Animal{ void eat(){ System.out.println("Animal eat"); } void sleep(){ System.out.println("Animal sleep"); } void breathe(){ System.out.println("Animal breathe"); } } class Fish extends Animal{ void breathe(){ System.out.println("Fish breathe"); } } public class TestNew { public static void main(String[] args) { // TODO Auto-generated method stub Animal an = new Animal(); Fish fn = new Fish(); an.breathe(); fn.breathe(); } }
Execution result:
Animal breathe<br>Fish breathe
Define a parent in the subclass A method with the same class name, return type, and parameter type is called method override. Method overriding occurs between subclasses and parent classes. In addition, super can be used to provide access to the parent class.
Recommended tutorial: "java tutorial"
The above is the detailed content of What does extends mean in java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
