Differences and Similarities between interfaces and abstract classes in Java

Source: Internet
Author: User

Differences and Similarities between interfaces and abstract classes in Java

This article mainly introduces the differences and similarities between interfaces and abstract classes in Java. This article describes the concepts of abstract classes, interfaces, differences and connections between interfaces and abstract classes, for more information, see

1. abstract class:

(1). concept: an abstract class is an abstraction of a thing, that is, a class extraction .. Abstract classes abstract the entire class, including attributes and behaviors. Java Abstract classes, like Java interfaces, are used to declare a new type. And as the starting point of a type level structure.

(2). format:

The Code is as follows:

Public abstract class abstractDemo {

/** Attributes */

Private String name;

/** Method */

Public abstract void fun ();

}

(3). Description:

A: abstract classes do not necessarily have abstract methods, but classes with abstract methods must be defined as abstract classes.

B: In Java, there are two types of classes: one is a specific class and the other is an abstract class.

A specific class can be instantiated, and an abstract class cannot be instantiated.

C: for abstract classes, if you need to add new methods, you can directly add specific implementations to the abstract classes without changing the subclass;

D: The abstract method has only declarations and has no specific implementation. Abstract classes exist for inheritance. If you define an abstract class but do not inherit it, it is equivalent to creating this class in white.

E: for a parent class, if its implementation of a method in the parent class has no significance, it must be implemented according to the actual needs of the Child class, then you can declare this method as the abstract method. In this case, this class becomes the abstract class.

2. interface:

(1). concept: an interface is an abstraction of behavior. An interface is used to abstract the local (behavior) of a class.

(2). format:

The Code is as follows:

Public interface test {

/** Abstract Method */

Public abstract void fun ();

}

(3). Description:

The interface can contain variables and methods. However, the variables in the interface are implicitly specified as public static final. The method is implicitly specified as the public abstract method and can only be the public abstract method.

3. Differences and connections between interfaces and abstract classes:

(1). abstract classes are the abstraction of a thing, that is, class abstraction, while interfaces are the abstraction of behavior.

(2) abstract classes abstract the entire class, including attributes and behaviors, but interfaces abstract the local (behavior) of the class.

(3). inheritance is a "not" relationship, while interface implementation is "not. If a class inherits an abstract class, the subclass must be the class of the abstract class, and the interface implementation has no or no relationship.

(4) The interface only provides the declaration of the method, and does not provide the Implementation of the method. Abstract classes can have abstract methods. For an abstract method, only the declaration of the method is required. For general methods, there are both method declarations and implementation methods.

4. Reasons for using the interface:

(1). There is no interface, and insertion is not guaranteed. Because Java is a single inheritance.

(2 ). any class in a class level structure can implement an interface. If this class implements this interface, it will affect all the child classes of this class, but will not affect all the parent classes of this class.

(3) A class can have at most one parent class, but several interfaces can be implemented at the same time.

5. Problem:

In the book "JAVA programming ideology", abstract classes are defined as "classes containing abstract methods". However, it is found that if a class does not contain abstract methods, abstract classes are modified only by abstract. That is to say, abstract classes do not necessarily contain abstract methods. Because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class?

Personal Understanding:

Abstract classes are used for inheritance. In terms of definition, there can be no abstract methods, but there is no syntax error in abstract modification, but there is no practical significance. Abstract classes allow subclasses to inherit abstract classes to implement abstract methods in abstract classes. Different functions are implemented based on the requirements of sub-classes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.