Home  >  Article  >  Java  >  What does extends mean in java

What does extends mean in java

hzc
hzcOriginal
2020-07-03 17:57:2723152browse

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

Executed in eclipse:
Animal breathe!
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
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!

Statement:
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