ComputersSoftware

Abstract class and method: description, characteristics and features. What is the difference between the abstract class and the interface

Anyone who has at least a minimal knowledge of programming, wondered why there are abstract base classes. What is their purpose? How to create an abstract class? What are the advantages they give in practice? Why are they needed in programming and what features of their implementation must be taken into account?

Abstract classes

So in programming is called a unit of program code, on the basis of which objects can not be created. It should be noted that the heirs of this class may not be abstract. Therefore, the creation of objects on their basis is quite possible. If we consider an example from Java, then we construct such a model: we can describe constructors in class A. But we can not use them. The way out of this situation is to declare a variable as a reference. And it is necessary to clearly state that it points to an object of class A. But how can this be done? The cast will help us in this. It should be noted that the parent class is always considered more universal than the child. In many programming languages, the cast is used automatically. But if there is no confidence in the availability of such a mechanism, then you must explicitly specify a reference to the object.

Abstract Methods

What are they? Abstract methods are those that do not have an implementation in the class under consideration. So, after the parentheses (in which the arguments used in them are listed) put non-opening curly (after which the block of the method description is placed), but only a semicolon. Thus, in this case there is no information about what he is doing. Here you can ask the question: "Why is the method without implementation?" After all, it can not be used! Of course, this is not possible in the class where it was described. But if we consider this situation from the point of view of the descendant, then it can be called. And it will function according to the specific description available in this class.

Features of abstract methods

There are limitations in their use. Consider this in the example of the Java programming language. There is such a requirement: if there is at least one such method in the class, then it must necessarily be abstract. And in which cases is it appropriate to use such constructions? Let's look at this in the case of pet classes. To begin with, it should be understood that there is no sound that they all publish. Therefore, in the most general class, the implementation of the sound method is not specified. But for him a body is created, which is separated by blocks. And already in the future, when a dog, cat or hamster class is created, a separate method for it will be prescribed, within which all the features will be revealed.

Interface

In programming, many tools have been created whose purpose is to help a person make a product of good quality. And in the framework of the topic under consideration, attention should be paid to this aspect, so that everyone knows the difference between the abstract class and the interface. So, the peculiarity of the latter is that it can not have properties, and methods do not have bodies. Exploring the difference between the abstract class and the interface, one should note one more feature. So, the first one is inherited, whereas the second one is realized. To an inexperienced person this may seem insignificant. But the fact is that only one class can be inherited, and you can implement as many as you want. This is how the interface differs from the abstract class. Java has other features that it can only describe abstract public methods and static constants. But interfaces can also be inherited. In this case, it is necessary to indicate that the used operators are public using public.

Multiple interface inheritance

This function is not available for classes in many programming languages (for example, in the Java that we are considering). This is due to a number of problems. As such, indicate the ambiguity that arise when inheriting. This was the reason for the introduction of multiple inheritance interfaces. This partially solves the existing problems, but not all. So, thanks to the principles of encapsulation and polymorphism, interfaces can be successfully implemented in different classes by different methods (although they will have the same name). But do not forget about the existing problems. So, when working with multiple interfaces, you will always have to specify who we mean. Also, there will be problems when trying to work with two methods of the same name, in which case they can not be used. Therefore, for such cases it is necessary to take care of the availability of a common implementation.

Abstract data

This concept is key in programming. In this case, abstraction is understood as an independent consideration and separation of the interface and its implementation. Let's look at an example. We have a TV, which is an object or module. It has an interface. As such, there are controls for it, as well as sound and image playback. The more advanced the interface is used, the more convenient the TV is in the process of use. We can switch programs by clicking on any buttons. In this case, there is no need to think about the physical processes in the TV. They are taken care of by specialists who collect the device. We are only interested in properties as users. Returning to programming, we can say that they are by this analogy are the interface and implementation. Also, if a person is confident in his knowledge, he can "take off" the cover from our hypothetical TV and improve it. Although this can lead to local successes, in most cases the end result is negative. Therefore, programming prohibits access and hides internal components for abstract data. The principle of this mechanism provides for ways to prevent intentional or accidental changes in internal components. And when they talk about abstract data types, they mean their aggregate with a multitude of operations that can be performed on them.

Objects

We have already examined the abstract class and interface in general and in general. Now let's a little detail our knowledge. First, we'll pay attention to the objects. By them in the general case is understood a part of the surrounding reality that exists in space and time. But it is rather difficult to formally define objects in the framework of programming (from a philosophical and theoretical point of view). Therefore, this is done due to its properties. As an example, the following can be cited: an object can be uniquely identified (has a unique name), it is in a certain state and uses a certain model of behavior.

Inheritance

Since the programming is based on building a hierarchical tree of relationships between objects, the question is raised that the properties of the parent code libraries should be transferred to the underlying types. Inheritance is understood as a relationship when one repeats the behavior and structure of something else. In ordinary life, this can be found everywhere. This was also transferred to programming with respect to abstract elements. As a comparison, we can cite the fact that there is no such organism as a bird. This is an abstract name. As a specific specimen, you bring a seagull, a nightingale or a sparrow. Due to inheritance, we can use class libraries, as well as develop them for a particular program. Such a tool allows us to create new objects, simultaneously adding or changing the properties of an already created one. The heir gets all the methods and fields of his ancestor and, if necessary, acquires his own attributes. Due to the fact that there are abstract elements and the principle of heritage, there are ample opportunities for modification in the framework of object-oriented programming. In addition, it allows you to flexibly approach the solution of the tasks. When you inherit, data and functions are passed to the new class.

Polymorphism

It is necessary in order to apply the same functions to solve different types of tasks. In this case, there is provision for the possibility of performing various actions, the content of which will depend on the type of object with which the work is carried out. Polymorphism is understood as the property of elements that have one common parent, perform similar problems in semantic meaning in various ways. We draw an analogy with animals. There is such an action - to run. We all understand how it happens. But with different animals (lion, elephant, chimpanzee, crocodile, tortoise) this action will be performed differently. In this case, the developer is responsible for their movement, which, if necessary, can cause a specific subprogram for a specific case, developing an algorithm and successfully implementing it. This is done using a set of methods. In this case, in each separate class, the programmer indicates which object has which properties are inherent. Changing something in the descendants, you can achieve certain, necessary in this case actions. Such an influence is called object polymorphism. In general, an abstract class is an extremely useful programming tool.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.