You are on page 1of 6
sar202 ‘Chapter 3: What is Object-Oriented Programming? [by Richard Kenneth Eng | Lear How To Program | Medium Chapter 3: What is Object-Oriented Programming? Jun §, 2017: 4 min read € Richard Kenneth Eng bject-oriented programming (or OOP) is a paradigm or pattern of programming whereby the solution to a programming problem is modelled as a collection of collaborating objects. Objects collaborate by sending messages to each other. It is most suitable for managing large, complex problems. htps:/medium.comvlearn-now-to-program/chaptr-3-whal-s-abject-orentoc programming d0abeca7615 16 sirara024 ‘Chapter 3: What is Object-Oriented Programming? [by Richard Kenneth Eng | Learn How To Program | Medium An object is an entity that possesses both state (or properties or attributes) and behaviour. Put another way, an object encapsulates data and the functions that operate on that data. The data is usually hidden from other objects so that the only way to affect the data is through the object’s functions (or methods). i ““ Object T2 ners a rch ge neo veo | | | ae veo? Object T1 ‘An example of an object is a car. A car has attributes (e.g., colour, size, weight, fuel capacity, number of passengers, etc.). A car has behaviour represented by its methods (e.g., start engine, turn left/right, accelerate, stop, turn on wipers, etc.). ‘Aclass is a special kind of object that’s used as a template for creating instances of itself. Think of it like a cookie cutter that produces cookies (or objects). Aclass can inherit the attributes and behaviour of another class (its parent), and it can modify or customize that behaviour (i.e., its methods) for itself. This leads to the concept of polymorphism. Polymorphism means that when an object receives a message, the correct method is called, based on the object’s class. That method may belong to the parent, or it may be one that is customized for this class. iz oa Child object Meinod A — htps:/medium.comvlearn-now-to-program/chaper-3-whal-s-abject-orentoc-programming-d0aBecOa7615 26 srear2021 ‘Chapter 3: What is Object-Oriented Programming? [by Richard Kenneth Eng | Lear How To Program | Medium LO = Method 8 Message invokos Method A. but whicn Method A? Method C Message invokes, ‘Method D, wich belongs to the parent Parent object Here’s an example of polymorphism: Office staffer parent object Receptionaist object How Metnod dower sno telephone ‘ree vstors shut papers: IT ‘ve orders programmer object solve tecnica problems wie sofware Smalltalk allows a class to inherit from only one class, Some OOP languages allow a class to inherit from several classes; this is known as multiple inheritance. Multiple inheritance causes a great deal of complexity, which is why it is generally avoided, We shall not speak of multiple inheritance again. htps:/medium.comvlearn-now-to-programichaptr-3-whals-abject-orentoc programming d0aboca7615 a6 sirara024 ‘Chapter 3: What is Object-Oriented Programming? [by Richard Kenneth Eng | Learn How To Program | Medium While inheritance is an important aspect of OOP, it is not the only way to build up programs. Instead of inheritance, composition or aggregation can be used. A class may include instances of other classes without inheriting anything. It’s a “has a” relationship as in: Class A has a Class B instance as a member. If you used inheritance, then Class B would be a kind of Class A object; this is an “is a kind of” relationship. Let's illustrate this with examples... Acar is a kind of motorized vehicle (the parent class). So is a motorcycle. So is a motor boat. So is an aircraft. Each of these can inherit the attributes and behaviour of a motorized vehicle. But they can also customize the attributes and methods of the parent class for themselves. A car has other objects or classes as part of itself, such n engine, wheels, steering wheel, etc. It does not inherit anything from these classes. hitps:/medium.comvlearn-now-to-program/chaptr-3-whal-s-abject-orentoc programming d0abeca7615 406 searz02 ‘Chapter 3: What is Object-Oriented Programming? [by Richard Kenneth Eng | Lear How To Program | Medium Syntactically, the attributes of an object (the object’s data) are represented by instance variables. Typically, you will create “getter” methods (get the value of an instance variable) and “setter” methods (set or modify the value of an instance variable) for them, since instance variables are hidden from the outside world. In Pharo, instance variables are created in the class definition, for example: instanceVar: "seconds nanos' classVariableName. poolDictionaries: 'ChronologyConstants!' package: 'Kernel-Chronology' In the #Time class (the hash in #Time designates Time as a Smalltalk symbol), there are two instance variables: ‘seconds’ and ‘nanos’. The object’s methods will operate on these variables, which represent the hidden and internal state of the object. By the way, the #Time class derives from, or inherits from, the #Magnitude class. Alternatively, you can say the Magnitude class “subclasses” the Time class. This is typical Smalltalk parlance. ‘An object resembles the conventional module concept that is used in simpler procedural programming languages like C and Pascal. A module contains a data structure and the htps:/medium.comvlearn-now-to-programichaptr-3-whals-abject-orentoc programming d0aboca7615 sreeae21 Chapter: Whatis Object-rented Programming? [by Richord Kenneth Eng | Lear How To Program| Medium functions that operate on the structure. The data, however, is not hidden; anybody can access the data. Most importantly, modules cannot inherit from other modules. Objects are usually much finer-grained than modules. Thus, they are ideal for modelling complex systems. So that’s it. That’s your introduction to Object-Oriented Programming. If you want to study this interesting subject in greater depth, there is a wealth of books and material dedicated to OOP and Smalltalk, for example: + Smalltalk by Example, by Alec Sharp Zagidulin Programming Object Oriented Smalltalk eo htps:/medium.comvlearn-now-to-programichaptr-3-whals-abject-orentoc programming d0aboca7615 oe

You might also like