A deep understanding of Java interfaces and abstract classes, and a deep understanding of java Abstraction

Source: Internet
Author: User

A deep understanding of Java interfaces and abstract classes, and a deep understanding of java Abstraction

For object-oriented programming, abstraction is one of its major features. In Java, OOP abstraction can be embodied in two forms: interfaces and abstract classes. There are too many similarities and differences between the two. Many people think that they can be used in different ways when they are new to the system, but they do not. Today, let's take a look at the interfaces and abstract classes in Java. The following is the directory outline of this article:

I. abstract class

Ii. Interface

Iii. Differences between abstract classes and interfaces

If there are any mistakes, please forgive me and welcome criticism and correction. Thank you very much.

I. abstract class

Before learning about abstract classes, let's take a look at abstract methods. An abstract method is a special method: it has only declarations, but not specific implementations. The Declaration format of the abstract method is:

abstract void fun();

Abstract methods must be modified with abstract keywords. If a class contains an abstract method, this class is called an abstract class. The abstract class must be modified with the abstract keyword before the class. Because abstract classes contain no specific implementation methods, you cannot use abstract classes to create objects.

Note the following: in JAVA programming ideology, the abstract class is defined as a class that contains abstract methods. However, if a class does not contain abstract methods, abstract classes are used for modification. That is to say, abstract classes do not necessarily contain abstract methods. I personally think this is a tough question, because if an abstract class does not contain any abstract methods, why should it be designed as an abstract class? So remember this concept for the moment. You don't have to go into the details.

 

[Public] abstract class ClassName {

Abstract void fun ();

}

It can be seen from this that the abstract class exists for inheritance. If you define an abstract class but do not inherit it, it is equivalent to creating this abstract class in vain, because you cannot use it to do anything. For a parent class, if a method of the class has no meaning in the parent class, it must be implemented according to the actual requirements of the subclass, then you can declare this method as the abstract method. In this case, this class becomes the abstract class.

Classes that contain abstract methods are called abstract classes, but they do not mean that abstract classes can only have abstract methods. They can also have member variables and common member methods like normal classes. Note: There are three main differences between abstract classes and common classes:

1) The abstract method must be public or protected (because if it is private, it cannot be inherited by the quilt class, And the subclass cannot implement this method). By default, the abstract method is public.

2) abstract classes cannot be used to create objects;

3) if a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass does not implement the abstract method of the parent class, the subclass must also be defined as the abstract class.

In other aspects, the abstract class is no different from the ordinary class.

Ii. Interface

An interface is called an interface. in software engineering, an interface refers to a method or function that can be called by others. From here, we can understand the original intention of the Java language designer, which is an abstraction of behavior. In Java, the form of an interface is as follows:

[public] interface InterfaceName { }

The interface can contain variables and methods. Note that the variables in the interface are implicitly specified as the public static final variable (and can only be the public static final variable). If you use private modification, a compilation error is returned ), the method is implicitly specified as a public abstract method and can only be a public abstract method (a compilation error is reported when other keywords such as private, protected, static, and final are used ), in addition, all methods in the interface cannot be implemented. That is to say, methods in the interface must be abstract methods. The difference between an interface and an abstract class can be seen from this. An interface is an extremely abstract type, which is more "abstract" than an abstract class and generally does not define variables in the interface.

The implements keyword is required for a class to follow a specific set of interfaces. The specific format is as follows:

class ClassName implements Interface1,Interface2,[....]{}

It can be seen that a class can follow multiple specific interfaces. If a non-abstract class follows an interface, all the methods in the interface must be implemented. Abstract classes that follow an interface do not implement abstract methods in the interface.

Iii. Differences between abstract classes and interfaces

1. Syntax differences

1) abstract classes can provide implementation details of member methods, while interfaces can only have public abstract methods;

2) The member variables in the abstract class can be of various types, while the member variables in the interface can only be of the public static final type;

3) the interface cannot contain static code blocks and static methods, while the abstract class can have static code blocks and static methods;

4) A class can inherit only one abstract class, but a class can implement multiple interfaces.

2. differences at the design level

1) An abstract class is an abstraction of a thing, that is, class abstraction, while an interface is an abstraction of behavior. Abstract classes abstract the entire class, including attributes and behaviors, but interfaces abstract the local (behavior) of the class. For example, airplanes and birds are different things, but they all share the same thing, that is, they all fly. During the design, the aircraft can be designed as an Airplane class, and the Bird can be designed as a Bird class. However, the flight feature cannot be designed as a class, therefore, it is only a behavior feature and is not an abstract description of a class of things. In this case, you can design the flight as an interface Fly, including the method fly (). Then Airplane and Bird implement the Fly interface according to their own needs. Then we can directly inherit Airplane from different types of aircraft, such as fighter planes and civil aircraft. It is similar to birds. Different types of birds can directly inherit the Bird class. It can be seen from this that inheritance is a "not" relationship, while interface implementation is "not. If a class inherits an abstract class, the subclass must be the class of the abstract class, and the interface implementation has no or no relationship, for example, whether a bird can fly (or whether it is capable of flying), this interface can be implemented if it can fly. If it cannot fly, this interface will not be implemented.

2) the design level is different. abstract classes are the parent classes of many sub-classes. They are a template-based design. The interface is a behavior specification, and it is a radiation design. What is template design? In the simplest example, you have used templates in the ppt. If template A is used to design the public parts of ppt B, ppt C, ppt B, and ppt C, it is template, if they need to be changed in the public part, you only need to modify template A, and do not need to modify the ppt B and C. Radiation design, for example, a certain alarm is installed on an elevator. Once an alarm is updated, it must be all updated. That is to say, to add a new method to an abstract class, you can directly add the specific implementation to the abstract class, And the subclass can not be changed. The interface cannot be used. If the interface is changed, all classes that implement this interface must be modified accordingly.

Next, let's take a look at the most widely circulated online example: the door and alarm example: the door has two actions: open () and close, in this case, we can define this abstract concept through abstract classes and interfaces:

 

Abstract class Door {

Public abstract void open ();

Public abstract void close ();

}

Or:

 

Interface Door {

Public abstract void open ();

Public abstract void close ();

}

But now, if we need the function of alarm (), how can we implement it? The following two ideas are provided:

1) Put these three functions in the abstract class, but in this way, all subclasses inherited from this abstract class have the alarm function, but some do not necessarily have the alarm function;

2) place these three functions in the interface. To use the alarm function, you need to implement open () and close () in this interface (), maybe this class does not have the open () and close () functions at all, such as fire alarms.

From this we can see that Door's open (), close (), and alarm () are essentially two different categories of behavior, open () and close () it belongs to the inherent behavior characteristics of the door, while alarm () is an extended additional behavior. Therefore, the best solution is to independently design the alarm as an interface, including alarm () behavior, Door design as a separate abstract class, including open and close actions. Design an Alarm Door to inherit the Door class and implement the Alarm interface.

 

Interface Alram {

Void alarm ();

}

 

Abstract class Door {

Void open ();

Void close ();

}

 

Class AlarmDoor extends Door implements Alarm {

Void oepn (){

//....

}

Void close (){

//....

}

Void alarm (){

//....

}

}

  • Original article: http://www.cnblogs.com/dolphin0520/p/3811437.html

If you have any questions during Java learning or want to obtain some learning resources, you are welcome to join the group's Java learning exchange QQ group: 495273252

Head of Java

No.: javatuanzhang

Sharing Java technical expertise on a daily basis

Long-pressed QR code recognition

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.