Abstraction against an abstract class

according to http://www.cs.cornell.edu/courses/cs211/2006sp/Lectures/L08-abstraction/08_abstraction.html

abstraction occurs in two ways. One is function abstraction, and the other is data abstraction. But where do abstract classes fit in? As far as I can see, abstract classes are a completely different concept, and although the name suggests that it has something to do with OOP principles.

Can someone shed some light on this?

+6
source share
3 answers

These are very different concepts.

Abstraction is similar to the concept of a black box. The entrance goes, the black box does something, the exit goes. No matter what happens in the black box, all you need to know is that it works. A real example of this is the java hash function, all users should know that it hashes the input value, it does not matter for the user how the number is hashed. The black box is an abstraction. The fact is that you don’t have to know how it works, the only way it is.

Abstract classes (at least in Java) are a mixture between interfaces and full OOP classes. An interface defines the methods that any extensible class should have, its agreement in the code that it will correctly implement the interface, and ensures that everything will work properly. An abstract class has these empty methods (conventions), as well as fully implemented methods that can be called.

+5
source

Wiki puts it very accurately

Data abstraction

Data abstraction provides a clear separation between the abstract properties of a data type and the specific details of its implementation. Abstract properties are those that are visible to client code that uses a data type β€” an interface to a data type, and the particular implementation is completely closed and can really change, for example, in order to increase efficiency over time. The idea is that such changes should not have any effect on the client code, since they do not have a difference in abstract behavior. For example, you can define an abstract data type called a lookup table that uniquely associates keys with values ​​and in which values ​​can be obtained by specifying their corresponding keys. Such a lookup table can be implemented in various ways: both a hash table, a binary search tree, and a simple linear list of pairs (key: value). As for the client code, the abstract properties of this type are the same in each case.

Consider, for example, a sample Java fragment to present some common β€œanimal” farms to an abstraction level suitable for modeling simple aspects of their hunger and nutrition. It defines a class of animals to represent the state of the animal and its functions:

public class Animal extends LivingThing { private Location loc; private double energyReserves; public boolean isHungry() { return energyReserves < 2.5; } public void eat(Food f) { // Consume food energyReserves += f.getCalories(); } public void moveTo(Location l) { // Move to new location loc = l; } } 

Using the above definition, you can create objects of type Animal and call their methods as follows:

 thePig = new Animal(); theCow = new Animal(); if (thePig.isHungry()) { thePig.eat(tableScraps); } if (theCow.isHungry()) { theCow.eat(grass); } theCow.moveTo(theBarn); 
+2
source

Objects in general combine data with functionality, and an abstract class is no exception to this. In some cases, the provided abstraction is almost pure data with functions only to provide access to data (for example, collection classes). Other cases are almost the opposite (for example, the abstraction provided by a functor in C ++ is usually a pretty significant function).

Those, of course, are largely extremes - many (most?) Classes fall somewhere in between.

0
source

Source: https://habr.com/ru/post/926265/


All Articles