Home > Java > JavaBase > body text

What does extends mean in java

hzc
Release: 2020-07-03 17:57:27
Original
23321 people have browsed it

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.

What does extends mean 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();  
    }  
}
Copy after login

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();  
    }  
}
Copy after login

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!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!