Classes and objects

A class is a blueprint, a description about something. It represents a well-defined idea that explains the existence of an entity. It can be thought of as a template that contains lists of two things: attributes and behaviors. An attribute is the state of a class, while the behavior refers to what a class can perform. Let's take a look at some examples:

A car class can have the following attributes and behaviors:

  • Its attributes include color, doors, license number plate, and steering wheel
  • Its behaviors include the fact that it runs on the road and that it turns left or right when the steering wheel moves

A person class can have following attributes and behaviors:

  • Its attributes include name, age, height, and weight
  • Its behaviors include the fact that a person communicates by talking, sleeps when tired, and can move around

Cars, people, fruit, and tables, we interact with these objects and more on a daily basis. All these entities are tangible; things that can be touched physically. In the world of technology, however, there are many entities that are part of our daily life but cannot be touched physically. An example of this would be a bank account. Each bank account has attributes, which include a unique ID, an account type, and a current balance. The behaviors that a bank account can carry out include allowing a customer to deposit or withdraw money.

These are all different examples of classes. If the class is a description or a blueprint, what is the object? The object is an instance of a class. An object is an entity that contains the attributes and behaviors that a class describes. A car class, for example, defines the color, model, or license number plate, but the car object provides a value of these properties. Multiple objects can be created from one class. For example, a bank can open many accounts for different customers, knowing that all accounts share similar attributes. Similarly, car manufacturers can produce a number of cars using the same blueprint. In the real world, there are many objects of the same kind, which behave similarly, although they might have different statistics or characteristics. A car might be red or green, it might have two doors or four, but it will always reduce its speed when the brakes are applied, and it will always turn either left or right when the steering wheel moves. Similarly, a person varies in height and age, but person can behave by communicating with each other, moving from one place to another and much more.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.251.57