JAVA interface and abstract class, JAVA interface abstract class

Source: Internet
Author: User
Tags define abstract

JAVA interface and abstract class, JAVA interface abstract class

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.

I. abstract class

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.


1 package implements act_interface; 2 3 public abstract class implements act_one {4 public abstract void one (); // The abstract method is a special method: it has only declarations, but no specific implementation. 5}

 In the book "JAVA programming ideology", abstract classes are defined as "classes containing abstract methods". However, it is found that if a class does not contain abstract methods, abstract classes are modified only by abstract. 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?

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.


1 package abstract_interface;2 3 public interface Interface_one {4     public void one();5 }


We define an int a = 1 in it; view the status of;

It is found that a has changed to the static final type;

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.

1 package abstract_interface;2 3 public abstract class Test implements Interface_one{4     5 }


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

  • Abstract classes and interfaces cannot be directly instantiated. to instantiate an abstract class, the variable must point to the subclass object that implements all abstract methods, and the interface variable must point to the class object that implements all interface methods.
  • Abstract classes must be inherited by quilt classes, and interfaces must be implemented by classes.
  • The interface can only be used as a method declaration. The abstract class can be used as a method declaration or method implementation.
  • The variables defined in the interface can only be public static constants, and the variables in the abstract class are common variables.
  • Abstract methods in an abstract class must be implemented by all quilt classes. If not all child classes can implement abstract methods of the parent class, the Child classes can only be abstract classes. Similarly, if an interface cannot be fully implemented, the class can only be an abstract class.
  • Abstract METHODS can only be declared and cannot be implemented. Abstract void abc (); cannot be written as abstract void abc (){}.
  • Abstract classes can have no abstract methods.
  • If an abstract method exists in a class, this class can only be an abstract class.
  • Abstract methods must be implemented, so they cannot be static or private.
  • An interface can inherit interfaces and inherit more interfaces, but a class can only inherit from one interface.
  • Abstract classes have their advantages, especially for public implementation code. Abstract classes can ensure implementation hierarchies and avoid code duplication. However, even when using image extraction classes, do not ignore the principle of defining behavior models through interfaces. From a practical point of view, if you rely on abstract classes to define behavior, it often leads to overly complex inheritance relationships, and defining behavior through interfaces can effectively separate behavior and implementation, it facilitates code maintenance and modification.


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.

In short, it is:

  The interface is the abstraction of actions, and the abstract class is the abstraction of the source.

The abstract class indicates what this object is. The interface indicates what the object can do. For example, men and women (if they are classes ......), Their abstract class is human. It means they are all people. People can eat, dogs can also eat, you can define "eat" as an interface, and then let these classes implement it.

Therefore, in advanced languages, a class can only inherit one class (abstract class) (just as humans cannot be both biological and non-biological ), however, multiple interfaces (meal and walking interfaces) can be implemented ).

Abstract classes have far more functions than interfaces. However, it is costly to define abstract classes. Because in advanced languages (also in actual design), each class can only inherit one class. In this class, you must inherit or compile all the commonalities of all its sub-classes. Although the interface is much weaker in function, it only describes an action. In addition, you can implement multiple interfaces in a class at the same time. This will reduce the difficulty in the design phase.

We can use an example to deepen our impression:

Door and alarm example: the door has two actions: open () and close (). At this time, we can define this abstract concept through abstract classes and interfaces:

Abstract class:

1 package abstract_interface;2 3 public abstract class Abstract_door {4     public abstract void close();5     public abstract void open();6 }



1 package abstract_interface;2 3 public interface Interface_door {4     public void close();5     public void open();6 }


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.

 1 package abstract_interface; 2  3 public abstract class Abstract_door { 4     public abstract void close(); 5     public abstract void open(); 6 } 7  8  9 10 11 package abstract_interface;12 13 public interface Interface_alarm {14     public void alarm();15 }16 17 18 19 package abstract_interface;20 21 public class Test2 extends Abstract_door implements Interface_alarm{22 23     @Override24     public void alarm() {25         // TODO Auto-generated method stub26         27     }28 29     @Override30     public void close() {31         // TODO Auto-generated method stub32         33     }34 35     @Override36     public void open() {37         // TODO Auto-generated method stub38         39     }40 41 }


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: 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.