Java Interface and abstraction

Source: Internet
Author: User

This article refers to a resource statement:

http://blog.csdn.net/xw13106209/article/details/6923556

Http://www.cnblogs.com/dolphin0520/p/3811437.html

Http://www.tutorialspoint.com/java/java_abstraction.htm

1. Abstract class

Keywords: abstract

Definitions: Classes that contain abstract methods can also have specific methods.

Abstract method: Only the declaration of methods, no body. End, without {}. The abstract method of abstract classes is to force subclasses to be implemented.

Abstract classes exist for inheritance. If a parent class has a method that implements it in the parent class without any meaning, it must be implemented differently depending on the requirements of the subclass, then this method can be declared as an abstract method, and the parent class is marked as an abstract class.

Do not write a specific class to inherit another specific class, this design will have a lot of problems.

The difference between an abstract class and a normal class:

A: The abstract method must be public or protected

B: Abstract classes cannot be used to create objects (because there are abstract methods that cannot be implemented)

C: If a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass cannot be implemented, then the subclass is also abstract.

2. Interface

Set an interface: public interface name{}

Only methods can be defined in an interface, and there cannot be implementations of methods. That is, the methods in the interface are abstract methods and public abstract methods. The variables in the interface are public static final variables.

3. Differences

(1) Generally speaking:

You can think of an interface as one of the abstract classes, but with many more constraints.

A: There cannot be static code blocks and static methods in the interface, abstract classes can have.

B: The Implementation Details (body) of the method cannot be provided in the interface, and the concrete method of the abstract class can provide the body.

C: Member variables in an interface can only be public stratic final

But there are also great benefits, because

A class can inherit only one abstract class, but it is possible to refer to (implement) multiple interfaces, with, (comma) connections

(2)

An abstract class is an abstraction of an entire class including properties, behavior, but an interface is an abstraction of the behavior of a class.

If a class inherits an abstract class, then the subclass must belong to the kind of abstract class (for example, civil airplanes belong to airplanes, swallows belong to birds). But the interface is a relationship with this behavior, such as airplanes and birds can refer to the "Fly" this interface to achieve the "fly" behavior. So, inheritance (extend) is "not" the relationship, the interface is "there is no" relationship.

For example, we can use airplanes, birds as an abstract class, "Fly" as an interface, so that the entire two abstract classes to achieve "fly" this interface, then they have "fly" this behavior. Let civil aircraft inherit the plane this abstract class, the civil aircraft can fly and contains the basic plane of the aircraft belong to, similarly, the Swallow is the same.

(3)

For abstract classes, if you need to add a new method, you can directly change the class in the abstract. However, for interfaces, if the interface changes, all classes that implement this interface must make corresponding changes. Because if a new method is added to the interface, all classes that implement the method cannot be compiled, and each class must implement this method.

How do you implement abstract methods of abstract classes in subclasses? How to implement an interface in a class that references an interface?? Need to add an example

Java Interface and abstraction

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.