Home  >  Article  >  Java  >  Learn inheritance and polymorphic features of Java interfaces

Learn inheritance and polymorphic features of Java interfaces

王林
王林Original
2023-12-23 13:00:57611browse

Learn inheritance and polymorphic features of Java interfaces

To master the inheritance and polymorphic features of Java interfaces, specific code examples are required

In the Java language, an interface is an abstract type that defines a set of methods. Through interfaces, a series of methods can be defined and implemented in different classes. In addition to being implemented by classes, interfaces can also inherit other interfaces. Classes that implement interfaces can inherit interfaces through polymorphic features.

Next, we will illustrate the inheritance and polymorphism features of Java interfaces through specific code examples.

First, we define a basic interface Animal, including an abstract method sound():

public interface Animal {
    void sound();
}

Then, we define two sub-interfaces inherited from the Animal interface, namely Dog and Cat , they respectively define their own abstract methods run() and meow():

public interface Dog extends Animal {
    void run();
}

public interface Cat extends Animal {
    void meow();
}

Next, we define two concrete classes that implement the Dog and Cat interfaces, namely GoldenRetriever and PersianCat:

public class GoldenRetriever implements Dog {
    @Override
    public void sound() {
        System.out.println("汪汪汪!");
    }

    @Override
    public void run() {
        System.out.println("狗狗在奔跑!");
    }
}

public class PersianCat implements Cat {
    @Override
    public void sound() {
        System.out.println("喵喵喵!");
    }

    @Override
    public void meow() {
        System.out.println("猫猫在喵喵叫!");
    }
}

Now, we can take advantage of polymorphic features to refer to objects of specific classes that implement the Animal interface through references of the Animal type, but can only access the methods defined in the Animal interface. For example:

public class Main {
    public static void main(String[] args) {
        Animal dog = new GoldenRetriever();
        Animal cat = new PersianCat();

        dog.sound();    // 输出汪汪汪!
        cat.sound();    // 输出喵喵喵!
    }
}

As shown in the code example, by pointing the Dog type reference dog to the GoldenRetriever object, and pointing the Cat type reference cat to the PersianCat object, we can call the corresponding sound( through these two references ) method, and output the barking of the dog and the meowing of the cat respectively.

To summarize, through the inheritance and polymorphic features of interfaces, we can define a set of methods and implement these methods through classes that implement the interface. Using the polymorphic feature, we can refer to the object of the specific class that implements the interface through the reference of the interface type, and call the methods defined in the interface. This flexibility and extensibility are important features of Java interfaces and are crucial to improving code reusability and maintainability.

The above is the detailed content of Learn inheritance and polymorphic features of Java interfaces. 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