Abstract classes and interfaces that must be mastered in Java programming

Abstract classes and interfaces are two important concepts in J--a, both of which are used to achieve polymorphism and code reuse. In this article, we will introduce the characteristics of these two concepts and how to use them to implement inheritance from parent classes while implementing interfaces, as well as multiple inheritance of interfaces.

The old rules, I have sorted out the mind map and put it at the end of the article, you need to pick it up yourself

abstract class

An abstract class in J-a is a class that cannot be instantiated directly and can only be inherited by other classes. Abstract classes usually contain some abstract methods, which have no specific implementation and need to be implemented in subclasses. An abstract class can also contain ordinary methods with concrete implementations.

How to define an abstract class:

public abstract class A----l
{  public abstract void eat(); // 抽象方法 
   public void sleep()
     {  
        System.out.println("A----l is sleeping"); 
      }
}

The above code defines an abstract class A----l, which contains an abstract method eat()and a method with a concrete implementation sleep().

When we create a subclass that inherits from an abstract class, all abstract methods must be implemented. For example:

public class Dog extends A----l {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }
}

The above code defines a A----lsubclass inherited from Dogand implements eat()the method. Because eat()the method is abstract in the parent class, the subclass must implement it.

interface

An interface in J--a is a type that contains abstract methods and constants. An interface has no concrete implementation, only the signatures of the methods are defined. A class can implement one or more interfaces and thus obtain the methods defined in the interfaces.

How the interface is defined:

public inte---ce A----l {
    void eat(); // 抽象方法
    void sleep();
}

The above code defines an interface A----lthat contains two abstract methods eat()and sleep().

When we create a class that implements an interface, all methods defined in the interface must be implemented. For example:

public class Dog implements A----l {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }

    @Override
    public void sleep() {
        System.out.println("Dog is sleeping");
    }
}

The above code defines a A----lclass that implements the interface Dogand implements the two methods defined in the interface.

Inherit the parent class while implementing the interface

A class in J--a can inherit a parent class and implement one or more interfaces at the same time. This allows classes to obtain properties and methods defined in superclasses and interfaces. For example:

public class Husky extends Dog implements A----l {
    @Override
    public void sleep() {
        System.out.println("Husky is sleeping");
    }
}

The above code defines a subclass that both inherits from Dogthe class and implements the interface . Subclasses need to implement all methods defined in the interface, and can also override methods in the parent class.A----lHusky

Interface Multiple Inheritance

Interfaces in J--a can extendsimplement multiple inheritance through keywords. When an interface inherits from one or more other interfaces, it includes all methods and constants defined in the inherited interfaces. For example:

public inte---ce Mammal extends A----l {
    void run();
}

public class Dog implements Mammal {
    @Override
    public void eat() {
        System.out.println("Dog is eating");
    }

    @Override
    public void sleep() {
        System.out.println("Dog is sleeping");
    }

    @Override
    public void run() {
        System.out.println("Dog is running");
    }
}

The above code defines an interface Mammal, inherits from A----lthe interface, and adds a new abstract method run(). DogThe class implements Mammalthe interface and therefore must implement the eat(), , sleep()and run()three methods.

When using interfaces, it is important to note that if a class implements two interfaces with the same method name at the same time, then the subclass must override this method and implement its own logic.

 

Guess you like

Origin blog.csdn.net/C_Small_Cai/article/details/131060701