Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between interface syntax and abstract classes and interfaces in different versions of Java

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "different versions of Java interface syntax and abstract classes and interfaces what is the difference", detailed content, clear steps, details handled properly, I hope that this "Java different versions of interface syntax and abstract classes and interfaces what is the difference" article can help you solve doubts, the following editor's ideas slowly in-depth, together to learn new knowledge.

What is an interface?

When it comes to interfaces, USB must be no stranger to you.

Interface is a kind of standard and specification.

Note: once the interface is established, both the user and the implementer must follow the standard.

Syntax of the interface: (JDK7.0)

(1) keyword: interface

(2) Syntax: interface API name {}

(3) the corresponding .class file is generated after the interface is compiled.

(4) an interface cannot create an object, but you can declare a reference interface name.

(5) there is no construction method in the interface.

(6) all properties in the interface are public static constants.

(default is: public static final)

(7) all methods in the interface are public abstract methods.

(modified by: public abstract by default)

Note: the default access permission for methods in the interface is public

The default access permission for methods in the class is default

Implementation class of the interface:

Syntax:

Class class name implements interface name {}

Note: the keyword of the implementation is implements

(1) if the implementation class does not want to be an abstract class, it must override all methods in the interface.

(2) when implementing methods in the overridden interface in the class, the access permission must be public.

Use:

(1) in the reference of the interface type, only the objects corresponding to the implementation class can be stored, reflecting the application of polymorphism.

(2) grammar:

Interface name reference name = new implementation class name (argument)

Note: an interface can define multiple implementation classes

Inheritance of the interface: (very important)

The relationship between interface and interface is multi-inheritance.

(1) an interface can inherit multiple parent interfaces at the same time.

(2) grammar:

Interface interface name extends parent interface name 1, parent interface name 2 {}

The relationship between class and interface is multi-implementation.

(1) A class can implement multiple interfaces at the same time

(2) grammar:

Class class name implements interface name 1, interface name 2 {}

(3) A class implements multiple interfaces at the same time. If you do not want to become an abstract class, you need to implement all the methods in all interfaces.

(including methods in the parent interface of the implemented interface)

While a class implements multiple interfaces, it can also inherit a class.

(1) if a class inherits a parent class and implements multiple interfaces at the same time, it must inherit before implementing

(2) grammar:

Class class name extends parent class name implements parent interface 1, parent interface 2 {}

Note: you must first extends, then implements

Existence between classes: single inheritance-extends

Existence between classes and interfaces: multiple implementations-implements

Existence between interfaces: multiple inheritance-extends

Impact of the interface:

1. Because the relationship between interfaces is multi-inheritance, and the relationship between classes and interfaces is multi-implementation, it makes polymorphism more diversified and complex.

two。 If both parties of the cast must pass as long as one of them is an interface type, the operation can be divided into the following two situations:

a. If the actual object type stored in the converted reference is one of the types to be converted, run through the

b. If the actual object type stored in the converted reference is not one of the ones to be converted, it compiles, but runs with an error message:

Java.lang.ClassCastException (type conversion exception)

Syntax of the interface: (JDK8.0)

(1) default method:

A. Public default returns the value type method name (formal parameter list) {

/ / the implementation part of the method

}

b. Note: the method in the interface is represented by default modification. This method is allowed to have the method implementation part.

c. The default method access is public

d. The default method is allowed to be overridden, but the access modifier is public when overriding

(2) static method:

A. Public static returns the value type method name (formal parameter list) {

/ / the implementation part of the method

}

b. The access permission for static methods in the interface is public

Syntax of the interface: (JDK9.0)-(private method)

(1) starting from 9.0 in the interface, the method can be modified by private.

(2) grammar:

Private returns the value type method name (formal parameter list) {

/ / implementation of the method

}

(3) Private methods are used inside the interface, and the common logic of other methods is usually extracted to reduce code redundancy.

The classified constant interface of the interface:

There are only properties and no methods in the interface, and there are few applications.

Empty interface:

Commonly referred to as a tagged interface, there are no attributes or abstractions in the interface

For example: apply when IO objects are serialized

Functional interface:

There is one and only one abstract method in the interface (static and default are not concerned), which is widely used in Lambda expressions.

What is the difference between abstract classes and interfaces? (keep in mind)

After reading this, the article "what is the difference between interface syntax and abstract classes and interfaces in different versions of Java" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report