On the similarities and differences between interface and abstract class in Java

Source: Internet
Author: User

Just learning Java, always feel that the interface and abstract class is very similar, but it is not specific what the difference. Today calm down, turn over the book, check the information, do a summary. Let's start with two examples to see what the similarities and differences between interface and abstract class are in "shape".

1, first look at the abstract class:

Abstract class person{    private  String name;     Private Person (String N)    {        name=n;    }      Public Abstract String getdescription ();      Public String getName ()    {        return  name;}    }

The main features of abstract class are two: First, declare the class when adding the abstract keyword. Second, there is an abstract method.

Abstract method: only declaration, no function body. For example, the above example, the description of people (person) can not be generalize, for students, for teachers, for doctors ... So in this case it can be defined as an abstract method, and an abstract method can (must) be implemented in a subclass that inherits it. Abstract classes can have fields (attributes) or complete methods, except for abstract methods, which are no different from ordinary classes. It is important to note that because abstract classes are incomplete and nondeterministic, they cannot be instantiated and can only be inherited as base classes.

2. Look at the interface again

Interface usb{     int numbers=2;   the auto-defined type is static final     void tokeyboard ();   automatically defined as public type     void tomouse ();   automatically defined as public type }

interfaces can also be defined as public types, but as with classes, the name and interface name of the file must always be guaranteed. If it is not public, the interface is only intended for classes of the same package. Although interface replaced the class keyword, I still have a question, is the interface a class? Later read the discovery:

The interface and the similarity belong to the reference data type and are tied.

3. Summarize the differences between abstract classes and interfaces

(1) Abstract class and interface, are the embodiment of abstract thought, abstract class is the abstraction of the class, interface is the "function can be realized" abstraction, that is, through the interface, I can achieve a certain function, not born, but can learn. For example, in the abstract computer This objective thing, can be designed as computer class, we can also design a USB interface, let him connect a mouse and keyboard, so that has the input function. And the computer's innate functions, such as display display information, CPU calculation results, internal circuit chip, etc., can be encapsulated in the computer class.

(2) interfaces are more abstract than abstract classes. This can be seen from the form. The method in the interface must be an abstract method, but the method of the abstract class can be complete (with the method body).

(3) Abstract class is still a class Ah, Java is not support multiple inheritance, although it does reduce the complexity of programming, but can only inherit a class may not be able to meet the actual development needs. Then the interface (interface) compensates for this flaw in some degree, because it can be implemented by multiple classes (implements).

Some of the content reference books: "Thinking in Java", "Java Core technology Volume One"

Reference Blog: http://www.cnblogs.com/dolphin0520/p/3811437.html

If there is any mistake, please criticize it.

On the similarities and differences between interface and abstract class in Java

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.