Home  >  Article  >  Java  >  The difference between java interface and abstract class

The difference between java interface and abstract class

(*-*)浩
(*-*)浩Original
2019-11-11 11:18:023358browse

The difference between java interface and abstract class

Interface

Concept

An interface is a collection that encapsulates certain functions. To abstract a type of behavior, use interface modification. (Recommended learning: java course)

public interface InfClass {
    public void say();
}

Abstract class

Concept

In short , a class containing an abstract method is an abstract class. An abstract method only has a method body and no implementation. It is modified with abstract.

abstract class AbsClass {
    abstract void say();
}

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, but the difference is:

All methods in the interface are implicitly abstract. An abstract class can contain both abstract and non-abstract methods.

A class can implement many interfaces, but it can only inherit one abstract class

A class does not need to implement all methods declared by abstract classes and interfaces , of course, in this case, the class must also be declared abstract.

Abstract classes can implement interfaces without providing implementations of interface methods.

Variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.

Member functions in Java interfaces are public by default. Member functions of an abstract class can be private, protected or public.

Interfaces are absolutely abstract and cannot be instantiated, and abstract classes cannot be instantiated.

If a class implements an interface, it must implement all methods of the interface, and abstract classes do not necessarily

The above is the detailed content of The difference between java interface and abstract class. 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