Home > Java > javaTutorial > How to define and use abstract classes and interfaces in java

How to define and use abstract classes and interfaces in java

王林
Release: 2023-05-03 21:13:05
forward
1562 people have browsed it

1. Abstract class

1. What is an abstract class?

For example, in life we ​​all classify dogs and cats as animals, but when we only talk about animals , we don’t know if it’s a cat or a dog or something else. So animals are so-called abstract classes, and cats and dogs are concrete classes. Therefore, in Java, a method without a method body should be defined as an abstract class, and if a class has an abstract method, it must be an abstract class.

2. Characteristics of abstract classes:

  • Abstract classes and abstract methods must be modified with the abstract keyword.

  • Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be abstract classes.

  • Abstract classes cannot be instantiated. If they need to be instantiated, refer to the polymorphic form and instantiate through subclasses.

  • If a subclass inherits an abstract class, you need to rewrite all abstract methods in the abstract class, or it must be an abstract class.

3. Member characteristics:

  • Member variables: can be variables or constants.

  • Construction method: There is a construction method, but it cannot be instantiated, so it needs to be done through a polymorphic subclass; the purpose is to access the parent class for data initialization for the subclass.

  • Member method: It can be an abstract class: it limits certain behaviors that the subclass must complete; it can also be a non-abstract class, which can improve the reusability of the code.

2. Interface

1. What is the interface?

Interface is a public specification standard, which is an additional condition required to express a class, so it can be used as long as it meets the specification standard. The interface in Java is mainly reflected in the abstraction of behavior.

2. Characteristics of the interface

  • needs to be modified with the keyword interface.

  • The implementation interface of the class needs to be represented by implements

  • The interface cannot be instantiated. If you want to instantiate it, refer to the polymorphic form. Instantiation is called interface polymorphism. Therefore, the main forms of polymorphism are: concrete class polymorphism, abstract polymorphism, and interface polymorphism.

  • Interface implementation class: Either override the abstract method in the interface, or be an abstract class.

3. Component members of the interface

  • 1. Member variables: can only be constants, modified by 'public static final' by default .

  • 2. Construction method: The interface has no construction method, because the interface mainly abstracts behavior and has no concrete existence.

  • Note: If a class does not have a parent class, it will inherit the Object class by default.

  • 3. Member methods:

Abstract method: The default method of the interface is an abstract method. The default is public static abstract for modification.

Default method: The default method is the method used when some methods need to be added to the implementation class. It needs to be modified with defaul in the interface. It does not need to be rewritten in the implementation class, but When rewriting, you need to delete default.

How to define and use abstract classes and interfaces in java

Static method: Static methods can only be added to the interface and cannot be called by the implementation class. They can only be called by the interface name and be static modification.

How to define and use abstract classes and interfaces in java

Private method: Private methods are mainly used in interfaces and are modified with the private keyword. By writing the same code segment in private In the method, the reusability and simplicity of the code are improved. Static private methods can only be modified with static methods, while non-static methods can be modified with both non-static and static methods.

How to define and use abstract classes and interfaces in java

4. The relationship between class and abstraction:

How to define and use abstract classes and interfaces in java

5. The difference between abstract class and interface:

Abstract classes are mainly abstractions for things, interfaces are mainly abstractions for behaviors.

The above is the detailed content of How to define and use abstract classes and interfaces in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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