Differences and similarities between interfaces and abstract classes in Java

Source: Internet
Author: User
Tags abstract inheritance

This article mainly introduces the difference and similarities between interface and abstract class in Java, this article explains the concept of abstract class, the concept of interface, the difference between the interface and abstract class, and so on, the need of friends can refer to the

1. Abstract class:

(1). Concept: Abstract class is an abstraction of a thing, that is, a class pumping. An abstract class is an abstraction of the whole class as a whole, including attributes, behavior. Java abstract classes, like Java interfaces, are used to declare a new type. And as a starting point of the hierarchical structure of a type.

(2). Format:

The code is as follows:

Public abstract class abstractdemo{

/** Property * *

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 the Java language, there are two types of classes, one is a concrete class and the other is an abstract class.

A concrete 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 a specific implementation in the abstract class, subclasses can not make changes;

D: Abstract methods Only declare, there is no concrete implementation. Abstract classes exist for inheritance, and if you define an abstract class without inheriting it, you are creating this class in vain.

E: For a parent class, if one of its methods does not make any sense in the parent class, it must be implemented differently based on the actual requirements of the subclass, so that the method can be declared as an abstract method, at which point the class becomes an abstract abstraction class.

2. Interface:

(1). Concept: Interface is an abstraction of behavior. An interface is an abstraction of a class's local (behavior).

(2). Format:

The code is as follows:

Public interface Test {

/** Abstract Method * *

public abstract void Fun ();

}

(3). Description:

Interfaces can contain variables and methods, but the variables in the interface are implicitly specified as public static final. Methods are implicitly specified as public abstract methods and can only be public abstract methods.

3. Differences and relationships between interfaces and abstract classes:

(1). An abstract class is an abstraction of a thing, that is, a class abstraction, and an interface is an abstraction of the behavior.

(2). An abstract class is an abstraction of the whole class as a whole, including attributes, behavior, but the interface is an abstraction of the local (behavior) of the class.

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

(4). The interface only gives the declaration of the method and does not give the implementation of the method. An abstract class can have a general method of abstract methods. If it is an abstract method, only the declaration of the method. If it is a general method, both the declaration of the method and the realization of the method.

4. Why you use the interface:

(1). There is no interface, there is no guarantee of pluggable. Because Java is a single inheritance.

(2). Any class in a class hierarchy can implement an interface, and if the class implements this interface it will affect all subclasses of the class, but it will not affect all the parent classes of the class.

(3). A class has at most one parent class, but it can implement several interfaces at the same time.

5. Question:

In the Java programming idea, the abstract class is defined as "a class containing abstract methods," but it is later found that if a class does not contain abstract methods, it is also an abstract class if it is modified in abstract. This means that an abstract class does not necessarily have to contain abstract methods. Because if an abstract class does not contain any abstract methods, why design it as an abstract class?

Personal Understanding:

The

Abstract class is intended to be inherited, and, on definition, there can be no abstract method, but there is no grammatical error with the abstract modification, but no real meaning. An abstract method in an abstract class is to have subclasses inherit abstract classes to implement abstract methods in abstract classes that implement different functions according to the needs of subclasses.

Related Article

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.