abstraction vs abstract class

2020-06-16 02:32发布

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

abstraction comes in two flavors. One is function abstraction and the other is data abstraction. But where do abstract classes fit in? As far as i see, abstract classes are a totally different concept and even though the name suggests that it has something to do with OOP principles.

Could someone please shed some light on this ?

标签: oop
2条回答
▲ chillily
2楼-- · 2020-06-16 03:16

Objects in general combine data with functionality, and an abstract class is no exception to that. In some cases, the abstraction provided is almost pure data, with functions only to provide access to the data (e.g., collection classes). Other cases are nearly the opposite (e.g., the abstraction provided by a functor in C++ is typically pretty much a function).

Those, of course, are pretty much the extremes -- many (most?) classes fall somewhere between them.

查看更多
戒情不戒烟
3楼-- · 2020-06-16 03:20

These are very different concepts.

Abstraction is similar to the concept of a black box. Input goes in, black box does something, output comes out. It doesn't matter what happens in the black box, all you have to know is that it works. A real life example of this is java's hash function, all the user has to know is that it hashes the input value, it doesn't matter to the user how the number gets hashed. The black box is the abstraction. The point is you don't have to know how it works, just that it does.

Abstract classes (at least in Java) are a mixture between interfaces and full OOP classes. An interface defines methods that any extending class must have, its an agreement in the code that it will implement the interface properly and assures everything will work as expected. An abstract class has these empty methods(agreements) and also has fully implemented methods that can be called.

查看更多
登录 后发表回答