Why is a class called an abstraction of an object?

馋奶兔 提交于 2019-12-23 10:49:37

问题


I understand that a class is essentially a blueprint of an object, but the idea that a class is an 'abstraction' of an object is a little hard for me to wrap my head around... If anyone could tell me the real meaning of the statement: "A class is an abstraction of an object", I'd be really grateful.

My confusion is because this statement has been interpreted differently by different people...

Does 'abstraction' mean:

  1. Dealing with the basics of a system, and not the deep intricacies of that system?

or does it mean that:

  1. Only an abstract class can be considered an abstraction of an object?

Thanks in advance, Abhigyan


回答1:


A class is a description of everything that will be in a certain type of object. For instance, a car will have a steering wheel, seats, dashboard, etc and functions such as accelerating, stopping etc. Each individual car is a car object, but the conceptual car is analogous to the class.

Dealing with the basics of a system, and not the deep intricacies of that system?

Somewhat, since a class does not usually describe exactly what goes into each field (for instance, the color of the steering wheel)

Only an abstract class can be considered an abstraction of an object?

No, this is more general that the abstract keyword in Java.

Basically, a class is an abstraction because it describes what is created, whereas an object is created itself.




回答2:


A class can be instantiated into objects. It captures the characteristics that are common to all these objects.




回答3:


A class defines fields & behavior (methods). It is instantiated into objects, which hold concrete values for those fields.

Abstraction as a term is used at many levels -- most commonly in regard of behavior. However in this regard it is being used of value.

We could state more clearly: A class is an abstraction across the possible values of its instances.

Example in pseudocode:

public class Cat {
    String name;
    String color;
}

object Cat ("Missy", "grey");
object Cat ("Whiskers", "orange");
object Cat ("Fluffy", "black");

As we see, class abstracts over values of its instances.



来源:https://stackoverflow.com/questions/41371835/why-is-a-class-called-an-abstraction-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!