An in-depth discussion of Java abstract classes and interfaces

Source: Internet
Author: User

In the Java language, abstract classes and Interfaces (interface) are two forms of abstract thought. Beginners are easy to confuse the two, so Java interview in the abstract class and interface of the difference between the surface of the question also often appear.

This article will delve into the Java abstract classes and interfaces in the following ways

I. Abstract class syntax and specifications

Two. Syntax and specification of the interface

Three. The differences between abstract classes and interfaces are described in terms of semantic details and design levels.

This article from I consult the information and the combination of their own understanding, if there is no understanding of the place, welcome you treatise.

I. Abstract class

Abstract classes are defined using the abstract class, and if a class is defined using abstract, then this class is an abstraction class.

The syntax is as follows:

abstract Class classname{}

Abstract classes are designed to be inherited, and the difference between abstract and ordinary classes is as follows:

1. Abstract classes cannot be used to create objects.

If a class is defined as an abstract class such as public class a{}, use such as Class A A = new A (), and the method class tries to create an A object, the compilation will error.

2. Subclasses of an abstract class must implement an abstract method of the parent class, or the subclass must also be defined as an abstract class

3. Abstract methods cannot be defined as private, because abstract methods must be implemented

In addition to these two aspects, abstract classes and ordinary classes have the same characteristics.

  Special Note:

Abstract classes generally have an abstract approach, but this is not necessary. That is, a method within an abstract class can be non-abstract.

Conversely, if a class has an abstract method, then this class must be an abstract class.

In addition: abstract class inside the abstract method if you do not specify access rights, then the default permissions, that is, the package visible permissions, is not the defaults to public permissions, this is the same as the normal class mechanism.

This requires special attention, someone in the blog said that the default access to abstract methods is pubulic, through my certification, this is wrong .

Two. Interface

The interface is declared as interface, with the following syntax:

interface interfacename{}

The interface is an abstraction of the height of the embodiment, like the following features:

1. All aspects of the interface are public abstract by default, and can only be public abstract so the method is defined in the interface, only the method name and the return type need to be defined.
A similar definition using the public int methodname{} or public abstract void methodname{} is actually superfluous.

   Attempts to use protected or private, final, static and other methods in the modification of the interface, will be compiled error prompts.

2. The variable names can also be defined in the interface, and they will default to public static final, except that the variable names are not decorated with other decorated names.

In the interface such as private int a = 1; A statement such as protected int b = 2 will prompt a compilation error.

Typically do not define variables inside the interface

3. The class implementing the interface must implement all the methods of the interface, unless the implementation class is also abstract.

4. You cannot use new InterfaceName to create an interface object.

Three. The difference between abstract classes and Interfaces 1. Semantic details

Abstract class is essentially, he is also a class (class), in addition to some of the above special conventions, he and the specific class of the agreement. but interfaces are not classes (an interface are not a class), interfaces and abstract class semantics differ as follows:

1) The abstract class can have a constructor. And the interface can not

2) Abstract classes can have non-abstract methods, and all the methods in the interface are public abstract, all the methods in the interface are abstract.

3) A member variable that can define various access rights within an abstract class. But if you define member variables in an interface, they can only be public final static.

4) Abstract classes can define static blocks of code, and interfaces may not

5) Subclasses can inherit only one abstract class, but an implementation class of an interface can implement multiple interfaces

2. Design Concept Selection

1) Abstract classes embody the abstraction of things. It is an abstraction of an entire class that can contain the abstraction of a behavior, an attribute. But an interface is an abstraction of behavior.

Both cats and dogs are animals, and they all have some common characteristics. So you can abstract a animal abstract class, the cat and the dog are animal subclasses. They all have the common characteristics of some animals. Monkeys are also animal. Mango trees do not belong to animals, so he cannot inherit animal

Both cats and monkeys can climb trees, but they do not climb trees the same way. Climbing is an abstraction of behavior. At this point, the crawl should be defined as an interface Climb. The inside defines the behavior Climb ().

Cats and monkeys do not climb the same tree, in their respective implementation of the climb interface, inside the implementation of their own tree climbing climb () can be. The dog does not have to climb the tree, so he cannot achieve the interface of climbing trees.

And if CLIMB is defined as an abstract class, then the cat and the monkey are Climb subclasses! It's obviously inappropriate.

2) Abstract class embodies the idea of the template design, the subclass inherits from the parent class can have the default behavior, later if the parent class adds a new non-private or default method, his subclass will automatically have to change the new behavior, the subclass code does not need any changes.

And the interface more embodiment of a specification, it is a kind of radiation design idea. Once the interface is published, the cost of modifying it will be significant. For example, if a new method is added to the interface, all its implementation classes must increase the implementation of this method.

The interface is similar to the master in the system. While abstract classes are similar to intermediate products in systems, they define specific behaviors and have more abstract behavior that they cannot publish as final products.

    

  

  

An in-depth discussion of Java abstract classes and interfaces

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.