abstract class editing Abstract classes are often used to characterize the abstract concepts of analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. Classes that are typically decorated in a programming statement are abstract classes. In C + +, a class containing a purely virtual function is called an abstract class, it cannot generate an object, and in Java, a class containing an abstract method is called an abstract class, and an object cannot be generated. An abstract class is incomplete and can only be used as a base class. In an object-oriented approach, abstract classes are primarily used for type concealment and for roles that act as global variables. Directory
- 1 Conceptual Understanding
- ? characteristics of abstract classes in C #
- 2 Comparison Differences
- ? compare with specific classes
- ? Comparison with interfaces
- 3 Application Requirements
- ? C + +
- ? C #
- ? Java
- 4 Application Examples
- ? C + +
- ? C #
- ? Java
- 5 use of meaning
Conceptual understandingEditIn object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects, and if a class does not contain enough information to depict a specific object, such a class is an abstract class. [1] Abstract classes are often used to characterize the problem domain analysis, design of the abstract concept, is a series of seemingly different, but essentially the same specific concept of abstraction. [1] For example, in a graphical editing software analysis and design process, you will find that the problem domain exists in the circle, the triangle such a number of specific concepts, they are different, but they are all part of the concept of shape, the concept of shape in the problem domain is not directly exist, it is an abstract concept. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain. [1] The characteristics of abstract classes in C # Abstract classes have the following characteristics:
- Abstract classes cannot be instantiated.
- Abstract classes can contain abstract methods and abstract accessors.
- Abstract classes cannot be decorated with the sealed modifier, because the meanings of the two modifiers are reversed. Classes with the sealed modifier cannot inherit, whereas the abstract modifier requires that the class be inherited.
- A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstraction accessor.
Compare differencesEditCompare with specific classes
- Abstract classes cannot be instantiated directly, and using the new operator with an abstract class can cause compile-time errors. Although some variables and values at compile-time types can be abstract, such variables and values must either be null or contain references to instances of non-abstract classes (this non-abstract class derives from an abstract class).
- An abstract class is allowed (but not required) to contain abstract members.
- Abstract classes cannot be sealed.
Compared to an interface, an abstract class represents a specific definition of a method that may already have some methods in the class, but the interface can only define the interface (method name, parameter list, return type) of each method, and does not care about the specifics. Interfaces are reference types, and the similarities to abstract classes are three points:
- cannot be instantiated;
- Contains a method declaration that is not implemented;
- Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members). [2]
Abstract classes are closely related to interfaces. However, interfaces are more abstract than abstract classes, which are mainly reflected in their differences:
-
- abstract class can exist non-abstract methods, interface can not, and its method is only a declaration must be public to decorate the method without concrete implementation. The member variables in the
- abstract class can be decorated with different modifiers, and the member variables in the interface can default to static constants (static final). The
- abstract class is an abstraction of an object, whereas an interface is a behavior specification.
Abstract classes can have non-abstract methods, but the interface can only have abstract methods to declare the existence of the method and not to implement its class is called the extraction class (abstract class), it is used to create a class that embodies some basic behavior, and for the class declaration method, but not in the class implementation of the class. An instance of the abstract class cannot be created. However, you can create a variable whose type is a drawing class and point it to an instance of a specific subclass. You cannot have a smoke-like constructor or a static method of pumping. The subclasses of the Abstract class provide implementations for all the extraction methods in their parent class, otherwise they are also smoke-like classes. Instead, implement the method in the subclass. Other classes that know their behavior can implement these methods in the class. Interface (interface) is a variant of the extraction class. In an interface, all methods are drawn. Multiple inheritance can be obtained by implementing such an interface. All the methods in the interface are drawn, none of them have a program body. An interface can only define static final member variables. The implementation of an interface is similar to a subclass, except that the implementation class cannot inherit the behavior from the interface definition. When a class implements a special interface, it defines the method (which is given by the program body) to all such interfaces. It can then invoke the interface on any image of the class that implements the interface. Because of the extraction class, it allows you to use the interface name as the type of the reference variable. The usual dynamic binder will take effect. A reference can be converted to an interface type or converted from an interface type, and the instanceof operator can be used to determine whether the class of an object implements the interface application requirementsEditC + + standard C + + does not have the abstract keyword, the use of pure virtual class to achieve similar functions, see the term "virtual class." When implementing an interface, it is often necessary to write an abstract class to implement the common methods required by some subclasses of the interface, and then, when writing each sub-class, to inherit the abstract class to use, eliminating the need to implement common methods in each of the problems. C#
- Abstract classes should be used primarily for closely related objects, and interfaces are best suited to provide common functionality for unrelated classes.
- The interface focuses on the can-do relationship type, while the abstract class is biased towards the is-a-type relationship.
- An interface defines the behavior of an object, and an abstract class defines the properties of an object.
- You can create an abstract class if you anticipate that a version problem will occur. For example, to create dogs, chickens (Chicken), and Ducks (Duck), you should consider abstracting animals (Animal) to deal with the possibility of future pig and horse cows. Adding a new member to an interface would force the requirement to modify all derived classes and recompile, so the problem with versioning is best implemented as an abstract class.
- A non-abstract class derived from an abstract class must include all inherited abstract methods and the implementation of the abstract accessor.
- The new keyword cannot be used for abstract classes, nor can it be sealed, because abstract classes cannot be instantiated.
- You cannot use the static or virtual modifier in an abstract method declaration. [3]
Java
- Abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface.
- In abstract class, you can have your own data members, or you can have non-ABSTARCT member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.
- The abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.
- Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
- The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.
- The variables in the abstract class are friendly by default, and their values can be redefined in the subclass or re-assigned.
- The methods in the interface are public,abstract type by default. [1]
Application examplesEditC + + to make a class an abstract class, at least one must have a pure virtual function. Classes that contain at least one pure virtual function are considered abstract classes. The pure virtual function is in the following form:
1 |
virtualreturntypefunction()=0; |
For example, Class A has two pure virtual functions, lock (), Unlock (), and a virtual destructor:
|
classA{public:virtualvoidlock(void)=0;virtualvoidunlock(void)=0;virtual~A(void);} |
Initialize the function lock () and unlock () to 0 so that they become pure virtual functions, and there are no 0 of these starters, they are just virtual functions.
|
classB:publicA{protected:pthread_mutex_tx;public:B(void);~B(void);virtualvoidlock(void){ead_mutex_lock(x);}virtualvoidunlock(void){ead_mutex_unlock(x);}} |
Abstract classes are useful for providing the principles that schemas, blueprints, and descendant classes follow, and if the semantics of the blueprint are followed, the behavior of the descendant classes may be expected by the abstract class provider and the consumer. By using abstract classes, C + + programmers can provide specifications for C + + components that guide the implementation of the component in its construction. The C # abstract class provides multiple derived classes that share a common definition of a base class that can provide either an abstract method or a non-abstract method. If a derived class does not implement all of the abstract methods, the derived class must also be declared as an abstract class. In addition, the implementation of abstract methods is implemented by the overriding method. [3] The definition method is:
////// Define abstract class /// abstractpublicclassAnimal {// Define static field staticprotectedint_id; // Define attribute publicabstractstaticintId // Cannot use static or virtual modifier {get; set;} // define method in abstract method declaration publicabstractvoidEat (); // Define indexer publicstringthis [inti] {get; // The body must be declared because it is not marked as abstract, extern, or partialset;} ////// Implement abstract class /// publicclassDog: Animal { publicstaticoverrideintId {get {return_id;} set {_id = value;}} publicoverridevoidEat () {Console.Write ("DogEats.")}}
[3] Java assumes that there is an abstract concept of door in the problem domain, the door has the ability to execute two actions open and close, at which point the abstract class or interface can be used to define a type that represents that abstraction. The definitions are as follows:
Define door using the abstract class method:
|
abstractclassDoor{abstractvoidopen(); abstractvoidclose();} |
Define door using the interface method:
|
interfaceDoor{voidopen(); voidclose();} |
[4] Other specific door types can be extends using the abstract class method defined door or implements using interface method defined door. In this view, there is no big difference between using abstract class and interface. But if demand door also have the function of alarm, can draw the difference. Since open, close and alarm belong to two different concepts, they should be defined separately in an abstract class that represents both concepts, according to the ISP principle. The understanding of the problem area is that Alarmdoor is inherently door in concept, and it has the function of alerting. It is also possible to complete the behavior defined in the alarm concept, so the alarm concept can be defined by the interface method. As shown below:
|
abstractclassDoor{ abstractvoidopen(); abstractvoidclose();}interfaceAlarm{ voidalarm();}classAlarmDoorextendsDoorimplementsAlarm{ voidopen(){…} voidclose(){…} voidalarm(){…}} |
This kind of realization basically can clearly reflect the understanding of the problem domain, and reveal the design intention correctly. [4] Use of meaningEditAbstract classes are primarily used for type concealment in object-oriented methods. Constructs an abstract description of a fixed set of behaviors, but this group of behaviors can have any possible concrete implementation. This abstract description is an abstract class, and any possible concrete implementation of this group is represented by all possible derived classes. The module can manipulate an abstract body. Because the module relies on a fixed abstraction, it can be modified, and the behavior of this module can be extended by deriving from this abstraction. Abstract classes are the key to achieving a core principle OCP (open-closed Principle) for object-oriented design. [1]
JAVA Abstract class