Home  >  Article  >  Java  >  Java abstract class and interface instance analysis

Java abstract class and interface instance analysis

王林
王林forward
2023-04-30 11:40:061324browse

1. Abstract class

1.1 Concept of abstract class

We all know that objects are described through classes, but it does not mean that all classes are used to describe objects

There is not enough information in a class to describe a specific object. This is an abstract class.

Keywords of abstract classes: abstract

1.2 Abstract method

First of all, let’s take a look at the class we wrote before:

class Plant {
    String name;
    String source;
    String genus;
    public void trait() {
        System.out.println("父类方法");
    }
}
class Tree extends Plant {
    @Override
    public void trait() {
        System.out.println("子类方法");
    }
}

Since the trait method is an overridden method, there is no need to implement the trait of the parent class at all, so we can change it into an abstract method.

Note: An abstract class can have no abstract method, but the abstract method must be in the abstract class

abstract class Plant {
    String name;
    String source;
    String genus;
    public abstract void trait();
}
class Tree extends Plant {
    @Override
    public void trait() {
        System.out.println("子类方法");
    }
}

Java abstract class and interface instance analysis

1.3 Details of abstract class

1. Abstract classes are intended to be inherited, so they cannot be instantiated. Other than this, they are no different from ordinary classes.

2. There can be no abstract methods in abstract classes, but abstract methods must be in abstract In class

3. When a normal class inherits an abstract class, it needs to override all abstract methods in the abstract class

4. An abstract class can inherit an abstract class, and there is no need to override abstract methods at this time.

5.Final cannot modify abstract classes and abstract methods

6.Abstract methods cannot be modified with private and static

Some people may find it strange: ordinary classes can also be used as parent The class is inherited, why do we need to create another abstract class?

We might as well assume a scenario: you instantiate the parent class object when instantiating the object, as follows:

public static void main(String[] args) {
        Plant tree=new Plant();
        tree.trait();
    }

If the parent class is a normal class, the compiler will not report an error at this time , directly call the method of the parent class

If the parent class is an abstract class, because the abstract class cannot be instantiated, the compiler will directly report an error, so that the problem can be discovered earlier

2 .Interface

2.1 Interface concept

The so-called interface is a public code of conduct. For example, A4 paper, the size of A4 paper is 210mm*297mm, then you can only proceed according to this specification. Production

In Java, the interface can be regarded as the public specification of multiple classes, which is a reference type data

Interface keyword: interface

2.2 Details of the interface

Interfaces also have some things to note:

1. Interfaces are also used to be implemented by other classes and cannot be instantiated.

2. The methods in the interface only It can be an abstract method, and the default is public static modification

3. The member variables of the interface are static constants by default, so they must be initialized

4. The keyword used to implement the interface is implements, class To implement an interface, you still need to rewrite all abstract methods in the interface

5. The interface cannot have constructors and static code blocks

6. The interface inherits the interface using extends

7. When creating an interface, the name of the interface generally starts with the capital letter "I" (recommended, not a hard requirement)

8. Interfaces can also be polymorphic

The most important interface in Java The function is to realize multiple inheritance

A class in Java can only inherit one parent class, but it can implement multiple interfaces

There are similarities between abstract classes and interfaces, but the most common between them is The essential difference is that abstract classes can have ordinary member methods and ordinary member variables, while methods in interfaces can only be abstract methods

After the introduction of abstract classes and interfaces, the next article will introduce some of the more important ones in Java interface

The above is the detailed content of Java abstract class and interface instance analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete