Object Oriented Programming: Specifying attributes and behaviors

in #programming6 years ago

Specifying attributes and Behaviors

We now have now added object-oriented terminology in our arsenal of definitions. We defined objects as instances of classes, where it is an object of its own set of data and behaviors.

What are these data and behaviors associated with each object?


1.Data describes objects
Data represents roughly the individual characteristics of an object. The class of an object defines a specific characteristics that are shared by all instances of the defined class.
On the other hand, each instances would have different data values for each shared characteristics. For example, the length of the tail of a dog of the following classes are of different values:


We can say that the class of dogs have a tail length attribute. Note, however that attribute doesn't necessarily have to be unique. For example, twins can have the same genetic code, but they are different instances of a single class - human.
Attributes are frequently referred to as properties. Some authors suggest that the two terms have different meanings:

  • attributes are settable
  • properties are read only

Fruit Inventory Application
We may want to know what orchard the orange came from, when it was picked, and how much it weights. We may also want to keep track of where each basket is stored. We might also have color attribute for our fruits (e.g. apple), and barrels might come in different sizes.

Depending on how detailed our design needs to be, we can also specify the type for each attribute.

  • they are often primitive data types: integer, floating-point, string, byte or boolean
  • they can also represent data structures: lists, trees, graphs or other classes

This is one area where the design stage can overlap with the programming stage. Most of the times we don't have to worry with this at the design stage, as implementation-specific details are chosen during the programming stage.

2.Behaviors are actions
Behaviors are actions that occur on an object. A behavior that can be applied to a specific class of objects is called a method. In programming, method are like functions, but it has access to all the data associated with that objects.
Like functions, methods can accept parameters and return values.

Numbers is a simple example of an object. With methods that includes the arithmetic operations addition and multiplication.

  1. parameters are a list of objects that need to be passed into the method that is being called. For our add method, we could have the following parameters, x and y:
  2. return values are the the result of the method

    if we invoke the method to values x=1, y=2 then the returned value is z = 3

Fruit Inventory Application


An action that can be associated with oranges in our application is the pick action. Pick would place the orange in a basket by updating the basket attribute on the orange, and by adding the orange to the oranges list on the Basket.

Basket can have a sell action. When a basket is sold, our inventory system might update some data on as-yet unspecified objects for accounting and profit calculations.

We can also add a discard method in case our basket of oranges might go bad.

Adding models and methods to individual objects allows us to create a system of interacting objects.

  • Each object in the system is a member of a certain class
  • These classes specify what types of data the object can hold and what methods can be invoked on it.

Object-oriented analysis and design is all about figuring out what those objects are and how they should interact.



Disclaimer: this is a summary of section 1.3 from the book "Python 3 - Object Oriented Programming: Dusty Phillips", the content apart from rephrasing is identical, most of the equations are from the book and the same examples are treated. All of the equation images were screenshot from generated latex form.

Thank you for reading ...


1524729086676.png

Sort:  

You got a 25.00% upvote from @stef courtesy of @sinbad989!

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.033
BTC 62916.93
ETH 3028.97
USDT 1.00
SBD 3.67