Java basics-The relationship between abstract classes and interfaces

Source: Internet
Author: User

In object-oriented concepts, we know that all objects are depicted by classes, but not all classes can be used to depict objects, and if a class does not contain enough information to depict a specific object, such a class should be defined as an abstract class.

Abstract classOverview

For example, we want to describe "animal", it has age, volume, will be called, can run. But there is not enough character (compared to a cat, a dog, they are animals, but have their own characteristics), we do not have the only one can represent animals (of course, this is relative to cats, dogs, in biology, animals have a way to define), then we can use abstract class to describe it. When we use a class to describe "cat", this class can inherit the abstract class of "animal" and then describe it in detail. Subclasses of abstract classes must implement abstract methods in classes, such as running and barking in animal classes, in subclasses (cats, dogs) must implement how they run and how they are called. Abstract methods have no method body and are decorated with an abstract.

Let's introduce the abstract class in a systematic way:

Characteristics

A. Abstract methods and abstract classes are represented by the keyword abstract.

B. Abstract classes cannot be instantiated

C. A class with an abstract method must be an abstract class (or interface); Abstract classes do not necessarily have abstract methods.

D. If a class inherits an abstract class, the class is either itself an abstract class, or the class overrides all the abstract methods of the parent class.

Use

Enforces that subclasses must complete certain functions.

Member features

A. member variables--can have member variables, or can have constants

B. Construction method-a constructor method that initializes the parent class data before the subclass accesses the parent class.

C. member methods-either abstract or non-abstract methods can be used.

I. The abstract method is to require subclasses to do certain things.

Ii. non-abstract methods to improve code reusability, the quilt class inherits.

Note:

Cannot coexist with final, private, static.

Abstract classes are defined in the following way:

Public abstract class AbstractClass             //There is at least one abstract method {public int A;  General data member public abstract void Method1 ();  Abstract methods, subclasses of abstract classes must implement abstract methods in an abstract class in a class public abstract void Method2 (); public void method3 ();      Non-abstract method         publicvoid method3 () {     ...      The default behavior that can be given to a non-abstract method method in an abstract class is the concrete implementation of the method}}

InterfaceDefined

is a special kind of abstract class. is more abstract than abstract class, because the methods inside it are abstract.

Format

Class name implements Interface name

{

}

Characteristics

A. The interface cannot be instantiated.

B. Methods in the interface:

A) either quilt class rewrite

b) Either the subclass is also an abstract class.

Member features

Member variables--only constants in the interface.

Because the member variable of the interface has a default modifier. Recommendation: Always give the modifier yourself, public static final

Constructor Method--there is no construction method in the interface.

If no class inherits the parent class, the class inherits the object class

Member methods-the methods in the interface are abstract.

Because the member method in the interface has a default modifier. Recommendation: Always give yourself a modifier, public abstract

Thought features:

1. Rules of external exposure.

2. function extension of the program

3. Reduction of coupling

4. Used for multiple implementations.

Interface (interface) is defined in the following way:

Interface interface{public static final int i; The interface cannot have ordinary data members, only can have static data members that cannot be modified, static represents the global, final represents non-modifiable, and can be implicitly declared static and final without static final decoration.  However, it is recommended that you always give the modifier public static final public abstract void method1 ();  The method in the interface must be an abstract method, so it can be used without an abstract modification, but it is recommended that you always give the modifier public the public method2 Void (); The default behavior of a method cannot be given in an interface, that is, there is no concrete implementation of the method.                   (note here and omit abstract.)}class Zi implements interface{publicvoid method1 () {... Rewrite the abstract method in the interface} public void Method2 () {...//Note that the abstract method is fully Part rewrite (the method in the interface is all abstract)}}//In another case, the subclass is also an abstract class, at which point you can not rewrite the methods in the interface/*abstract class Zi implements interface{public abs                tract void method1 (); No rewrite public void method2 ()//rewrite method2 () {Sys         Tem.out.println ("Rewrite method2 ()"); }}*/

comparison of abstract classes and interfaces



Abstract class

Interface

Member features

Member variables

Variable or constant

Constant

Default modifier public static final

Construction method

Yes

No

Member Methods

Abstract method or non-abstract method

Abstract methods

Default modifier public abstract

Design concept

Idea

is inherited, defining the common content of the entire inheritance system

is implemented, defining the extended content of the entire system

Reflected

is a

Like a

Relationship characteristics

Classes and Classes

Inheritance relationship, can only be single-inheritance, multi-layer inheritance

Classes and Interfaces

The implementation of the relationship, can be implemented alone, or more. You can also inherit a class and implement multiple interfaces at the same time.

Interfaces and interfaces

inheritance, which can be either single-inheritance or multiple-inheritance.

Add:

Further understanding, about the introduction of abstract classes in Java, the purpose of the interface , Degree Niang has the Master's reply as follows:

1, from the hierarchical structure of the class, the abstract class is at the top of the hierarchy, but in the actual design, generally speaking, abstract classes should be in the back to appear. Why? In fact, abstract class acquisition is a bit like the extraction of mathematics common divisor: ax+bx,x is abstract class, if you do not have the previous formula, how do you know if x is common divisor? At this point, it is also in line with the process of understanding the world, first concrete and then abstract. So in the design process if you get a lot of concrete concepts and find their commonalities from them, the set of commonalities is that abstract classes should be right.

2, interface on the surface, and abstract class very similar, but the usage is completely different. Its basic function is to bring together some unrelated classes (concepts) to form a new, centrally manipulated "new class". A typical example is "driver". Who can be a driver? Anyone can, just pick up the driver's license. So whether you are a student, white-collar, blue-collar or boss, as long as a driver's license.

Interface Driverlicence {        

When I define the "car" class, I can specify "driver".

Class Car {       setdriver (driverlicence driver);}

At this time, car objects do not care what the driver is doing, the only thing they have in common is to get a driver's license (all implemented the Driverlicence interface). This should be the most powerful part of the interface is also the abstract class can not be compared.

Summary

Abstract classes are common divisor that extract specific classes, and interfaces are intended to "hash" some unrelated classes into a common group, and thus may be a bit more practical. The appearance of the two is to reduce the amount of code, improve efficiency, on the other hand, referring to the previous inheritance and polymorphism, making the program conform to the characteristics of object-oriented thinking, to modify the closure, open to expansion.

Java basics-The relationship between abstract classes and interfaces

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.