The abstraction in object-oriented programming is the use of only those characteristics of the object that represent it with sufficient accuracy in this system. The basic idea is to present an object with a minimum set of fields and methods, and with sufficient accuracy for the problem being solved.

And here's another info

This is how abstraction from OOP works: we brought important data and methods to the base class, and their differences were left in their classes.

Worked with an abstract class and interface, but I don’t really understand the definition, especially from Wikipedia:

using only those characteristics of the object that with sufficient accuracy represent it in this system, what?))

From this:

This is how abstraction from OOP works: we brought important data and methods to the base class, and their differences were left in their classes.

I understood that we stuff a lot of what into an abstract class, and each successor class takes what it needs, correct it if it’s not right.

Abstraction is the basis of object-oriented programming and allows you to work with objects, without going into their particular implementation.

You can give an example, it is unclear how it allows you to work with objects, without going into particular implementation, I thank in advance.

  • Abstraction contains commonality, implementation - a particular. If two classes have methods / fields that are the same, then they can be moved to an abstract class so that only unique methods / fields remain in the classes. - Constantin Naumov

3 answers 3

Abstractions are needed for decomposition, that is, to reduce complexity.

Allocation of abstractions - a standard technique, not only in programming, it is inherent in any kind of human activity, which requires the systematization of information due to its abundance.

Abstractions have levels and they inherit each other.

Each level of abstraction is an object model of a certain degree of detail; this model lacks details that are insignificant for this level.

Examples can be found at every corner, for example, the nomenclature in biology.

enter image description here

Each level is characterized by a certain group of attributes, the levels are connected hierarchically. All attributes of the ancestor group are inherited by the descendant.

When we talk about a species , for example Дat or Dog , we are interested in the properties of the species or the fact that it is determined by (genotype) and indirectly phenotype, for example. how an animal "says" - "Woof" or "Meow" (specifically took an example from the already given answers). But at this level we are not interested in the signs of an individual, for example, the nicknames of pets (tuzik and murzik), they are at a different level of abstraction.

And when we are interested in the type (chord, to which dogs and cats belong) we are not interested in the specific signs of dogs and cats and we don’t know anything about them at this ā€œlevel of abstractionā€, and we don’t care about the nicknames.

    Here you can resort to classic examples from books on Java. Suppose we want to have a description (abstraction) of the animal in the program. But not all the variety of properties of the animal, but only a sufficient abstraction for our program. Suppose it suffices for us to know that an animal can "speak." Now we describe our abstraction.

     public abstract class Animal { public abstract String talk(); } 

    Here is a minimally sufficient description of the abstraction of the animal in our program. It is enough for us that the animal makes a sound. Now we can flesh out animals

     public class Dog extends Animal { @Override public String talk() { return "Гав!"; } } public class Cat extends Animal { @Overide public String talk() { return "ŠœŃŃƒ!"; } } 

    We can give other properties to Dog and Cat objects. And if we work in the program with these classes, we can operate with these properties. In case we work with Animal abstraction, we only know that the animal can "speak."

    Ie, if we want to arrange a roll call of all animals in our program, we don’t need to know what kind of animals we have, we just need to use the Animal abstraction.

     public class AnimalTalks { public void doTalks (List<Animal> allAnimals) { for (Animal animal : allAnimals) { animal.talk(); } } } 

    Something like this.

      Sometimes it is better to discuss abstraction with specific examples.

      For example, consider two specific types: an array and a single-linked list . Elements (for definiteness - numbers) in the array are located in RAM in a row. Operations such as getting an item by index are performed in O (1) constant time, and such as insertion take time proportional to the length of the O (N) array.

      List items are placed on the heap and require storing not only the numbers themselves, but also references to the next node. The insertion to the top of the list is performed in O (1) constant time, and the item is retrieved by index in O (N) time.

      For some algorithms, characteristics such as the speed of extracting numbers by index are decisive. For example, Hoare's quick sort on an array is performed in O (N Ɨ log N) time, while on a simply connected list the same algorithm will give O (N 2 Ɨ log N) due to the overhead of extracting an arbitrary element.

      For other algorithms, the speed of work on different structures will be identical. For example, if we want to calculate the sum of the numbers in the collection, then for the array and for the list this operation will take O (N) time.

      Unfortunately, if we do not abstract from the details of the implementation, the code for calculating the sum for the list and the array will be different. We would still like to, if possible, write the code once for different cases, if it does not hit on performance.

      In the case of an array and a list, we can abstract away the implementation details and use the Iterator design pattern. We will say that both the array and the list are representatives of an abstract type, a collection or a composite object ( Iterable ), which allows iterating over its elements.

      The array and list will implement the collection method to submit an iterator here , in Java it is called an iterator . The iterator itself is also an abstract type that has hasNext , next and remove methods.

      As a result, we can write a universal method for calculating the sum of the elements of an integral collection:

       int sum(Iterable<int> iterable) { int result = 0; Iterator<int> iterator = iterable.iterator(); while (iterator.hasNext()) result += iterator.next(); return result; } 

      This example is still not perfect, but it demonstrates the advantages of abstract data types.

      It is important here that the array iterator and the list iterator are different classes that work in their own way, but they have a common interface, sufficient for our tasks.