Object Oriented Programming By albro
hive-169321·@albro·
0.000 HBDObject Oriented Programming By albro
<center>  </center> <p>With the help of object-oriented programming, we can better model the complex needs that exist in the real world. In this post, I will explain the <b>basics of object oriented programming</b>. First, I will review the concepts and components, then its 4 principles with real examples.</p> <p><b>Object Oriented Programming (OOP)</b> is a programming method that is modeled on our real world. If you look around you, everything is an object! With the help of this method, we model all the execution of the program as objects and then use them.</p> <h3>What is object oriented programming?</h3> <p>In the first programming languages, we wrote the entire program in consecutive codes. After that, procedural methods came into use, where we divide the code into different pieces, each of which performs a specific operation. Then we call a <a href="https://peakd.com/hive-169321/@albro/concept-of-programming-function-and">programming function</a> wherever we need to execute them.</p> <p>If we want to do a project according to real world requirements, we will have very long codes. Let me continue my explanation with a simple example.</p> <p>Suppose we want to implement a university management system. Students, professors and university employees will be part of this system. You probably agree that these three data types are all human, but each has different characteristics or access to the system.</p> <p>In procedural mode we probably define three data types that are completely separate from each other. Now if we want to apply a small change in one of these common features, we have to repeat this change 3 times!</p> <p>Next, you'll see how much easier it is to implement and apply system changes (maintainability) with object-oriented programming.</p> <p><strong>OOP Concepts</strong></p> <p>Four concepts or words are frequently used in object-oriented programming. So it is better to agree on these 4 at the beginning. These concepts are:</p> <ol> <li>Class</li> <li>Object</li> <li>Property, which is sometimes called Attribute.</li> <li>Behavior, which we also call Method.</li> </ol> <p>In very simple terms, a class is a general template (map) for our data type in the system. Each class has properties and behaviors that can be defined for all data of that type. Whenever we create a data from this pattern and determine its characteristics, we have an object of that class.</p> <p>The operation of creating an object from a class is called <i>instantiation</i> (Create instance).</p> <center>  </center> <p><strong>Class, method and property in programming</strong></p> <p>In the example of the university system, we have a very comprehensive data type called "human". Imagine the human concept in the real world. This entity has a general structure made up of a set of properties and behaviors. for example:</p> <ul> <li>Age, skin color, height and weight are some of the human properties.</li> <li>Walking, seeing, eating, hearing and talking are human behaviors. These are behaviors (methods) similar to functions that can be executed (or call) on objects of this class type.</li> </ul> <p><i>Whenever we define certain properties according to the "human" (class) model, an object is made from it. All the objects of this class have similar behaviors but will differ greatly from each other due to the difference in their properties.</i></p> <p>Can you model an animal type (eg cat) as a class? Try to do this and if you have any questions, ask in the comments section of the post.</p> <h3>Basics of OOP</h3> <p>So far, we got acquainted with the concepts, or rather, the components of a program based on object-oriented programming. I will give more examples to help you fully understand these definitions.</p> <p>Object-oriented programming has certain principles. That is, in addition to defining all the entities of the program based on classes, it is necessary to observe certain principles.</p> <p>Of course, if we don't follow these principles or don't use them correctly, our program will still be an <i>object-oriented program</i>, but it won't have the necessary quality. These principles are used a lot in writing programs and understanding them will help you a lot.</p> <h3>Abstraction in programming</h3> <p>Abstract means to limit the details to address the main process. Let me start with two examples from everyday life:</p> <ul> <li>Many of us as drivers have sat behind a car and turned it on, but not all of us know the mechanism of starting and how alternators work.</li> <li>Until now, we have worked with microwaves, coffee makers or washing machines, but we don't know exactly how they work.</li> </ul> <p>We end up expecting what we want to be done right and we have no control over how it is done! This is the meaning of abstraction. As consumers of a device, we don't need to know what interactions take place after a click is pressed to get the result. For us, only the result is important.</p> <p>When defining behaviors (methods) and properties in object-oriented programming, we must pay attention to what level of abstraction the consumer of this class needs. What parts of the algorithm should we hide from the consumer?</p> <p><i>Note that we use abstraction to achieve simplicity of use. That is, our goal is to make it easier to work with classes and methods.</i></p> <h3>Encapsulation in OOP</h3> <p>Of course, every program is made up of different objects. <strong>Encapsulation</strong> principle tells us that everything should protect its properties. That is, it is better that other objects don't have direct access to the properties of other objects.</p> <center>  </center> <p>Suppose we want to implement an object-oriented chess game. The game of chess consists of pieces, each of which has different properties and behaviors. that's mean:</p> <ul> <li>Each piece has a specific color, shape, name (type of piece) and location on the game board.</li> <li>Each piece can move on the game board in different ways according to its type.</li> </ul> <p>In a very simple implementation, we specify the location of the piece with a variable (property in the class) of type number. Also, because all pieces have the same behaviors and properties, we consider a main class for game pieces.</p> <p>You might say that not all beads have the same behavior. Because the movement of the horse on the game board is very different from the movement of the rook. With the help of the next 2 principles in object-oriented programming, we can implement these minor differences.</p> <p>For now, we focus on the fact that all beads have a position attribute (eg <code>position</code>) and a move behavior called <code>move()</code>. If the players (which are themselves another object) have direct access to the <code>position</code> value, we can't have any restrictions on how the pieces move.</p> <p>For example, the user changes the position of his bead from <code>x</code> to <code>y</code>. Maybe this move is not allowed according to our game rules. So we should not allow players to directly access the position of the piece. In this case, the user calls the <code>move()</code> method and declares the desired position, if the position of the desired house is according to the rules, the piece moves.</p> <p><i><strong>Encapsulation can be defined for properties, methods, and even classes.</strong></i></p> <h3>Inheritance of classes in OOP</h3> <p>With the help of Inheritance, we can extend classes and add new features and behaviors to it. In the following 2 examples, I have illustrated a very simple example of inheritance in the real world:</p> <ul> <li>All cars are some kind of "car". They all have doors and windows and move. But cars are also divided into smaller categories. For example, they can be different in terms of fuel consumption, in which case the performance of some parts will be different. Another category is based on very different features (such as a sunroof or automatic transmission).</li> <li>All animals are some kind of "animal". They walk and eat. Meanwhile, horses walk on four legs, worms crawl, and fish use fins. The food of each of these is different.</li> </ul> <p>If you look carefully, all cars have color and wheel features. But gas cars have a gas capsule and gas system in addition to other cars. The same is true for animals.</p> <p>In <strong>object-oriented programming</strong>, when many properties and methods are shared between two (or more) classes, there is usually a larger class that contains the shared items and the classes inherit the subset from it.</p> <p>Similar to the real world where every child inherits almost all the characteristics of its parents, it is the same in programming. Some inherited characteristics in a child may be strengthened or weakened, and a child also has non-shared characteristics with its parents. This case is also established in the inheritance of object-oriented programming.</p> <h3>Inheritance in programming</h3> <p>Consider the example of an object-oriented university management program. In this program, the professor, student, and employee are all of the human class, but they have differences in their characteristics and behaviors; For example:</p> <ul> <li>The teacher can define the course grade, but the other two types cannot.</li> <li>The student can take the course, but this has no meaning for the other two types.</li> <li>An employee has something called attendance hours that students don't have.</li> <li>The student has a student number, but the professor and the employee have a personnel code that may have a different structure.</li> </ul> <p>In this case, in <strong>object-oriented programming</strong>, we create a generic human-type class that has common methods and properties; That is, it walks, talks and has a name, national code and date of birth. Then teacher and student classes inherit from this class to implement their special abilities.</p> <p><i>Depending on the system requirements, the professor is probably some kind of employee in the university. To better understand the concept of inheritance in programming, try to find examples of it in your daily life and generalize them.</i></p> <h3>Polymorphism Concept</h3> <p><strong>Polymorphism</strong> allows us to have different implementations of the method. <strong>We have two types of polymorphism in object oriented programming</strong>. which we will get to know briefly in this section.</p> <p><strong><i>Polymorphism with Method Overriding</i></strong>: Suppose we have a class for geometric shapes that has a <code>draw()</code> method. The rectangle, pentagon, and circle classes inherit from this class and because their drawing methods are different, they <strong>rewrite</strong> this method.</p> <center>  </center> <p><strong><i>Polymorphism with Method Overloading</i></strong>: In this case, there is no inheritance in object-oriented programming. Also, the name of the method and its output type (signature) do not change. We just have a few similar methods with different arguments (input parameters).</p> <p>Suppose we want to implement the circle class in such a way that if <code>draw()</code> is called, it will draw a circle with a random radius, and if the size is specified in the input (<code>draw(x)</code>), it will draw a circle with the desired radius. Our pseudocode looks something like this:</p> <pre><code>public class Circle(){ // attribute // some codes public void draw(){ // draw with default radius } public void draw(int r){ // draw with radius = r } } </code></pre> <h3>Summary of object oriented programming concepts</h3> <p>Object-oriented programming is very broad and sometimes confusing! But because its modeling is similar to the real world, understanding the concepts of object orientation becomes simple.</p> <p>In this post, we learned the basics of object-oriented programming in a simple language. We have understood the concept of class, object, attribute and method and examined it with 4 basic principles.</p> <p>I hope this post helped you understand the concepts better.</p>
👍 bgmoha, pars.team, elector, goldfoot, botito, tobor, hadaly, dotmatrix, curabot, chomps, freysa, lunapark, weebo, otomo, quicktrade, buffybot, hypnobot, psybot, chatbot, psychobot, misery, freebot, cresus, honeybot, dtake, quicktrades, droida, macchiata, bitcoinportugal, bilginhilal, manisha.jain9, chessbrotherspro, vjap55, samostically, eniolw, misterlangdon, larsito, doudoer, eumorrell, iamchessguy, jrjaime, rosmarly, oabreuf24, fernandoylet, nazom, raca75, philipp87, palabras1, jrevilla, adrianalara, wlffreitas, lk666, hivebuzz, cwow2, lizanomadsoul, manncpt, jnmarteau, steemitboard, marivic10, arcange, achimmertens, shainemata, laruche, rohansuares, walterjay, pboulet, ykretz, ydaiznfts, mengao, lemouth, steemstem-trig, steemstem, dna-replication, minnowbooster, howo, aboutcoolscience, robotics101, stemsocial, metabs, edb, aidefr, fragmentarion, francostem, temitayo-pelumi, lamouthe, dhimmel, sustainablyyours, helo, alexander.alexis, kenadis, meanbees, deholt, crowdwitness, curie, samminator, splash-of-angs63, sandracarrascal, madridbg, plicc8, valth, oluwatobiloba, abigail-dantes, bhoa, enzor, sco, geopolis, alexdory, melvin7, de-stem, stem.witness, noelyss, techslut, tsoldovieri, intrepidphotos, croctopus, doctor-cog-diss, neumannsalva, empath, ahmadmangazap, princessmewmew, manic.calm, iansart, stayoutoftherz, sunsea, brotherhood, yixn, federacion45, mamalikh13, swayzilla, gaottantacinque, maxdevalue, takowi, talentclub, cribbio, jemmarti, cloh76, modernzorker, gasaeightyfive, qberry, juancar347, jacuzzi, qwerrie, mariaalmeida, jayna, trenz, oscarina, thelittlebank, roamingsparrow, revo, greddyforce, the100, bitrocker2020, dynamicrypto, traderhive, promo-mentors, aabcent, zyx066, braaiboy, vietthuy, mvd, dante31, futurethinker, the-grandmaster, rt395, tfeldman, aicu, hiveonboard, kylealex, arunava, meritocracy, the-burn, therealwolf, merlin7, steveconnor, smartsteem, fineartnow, armandosodano, steemcryptosicko, baltai, aries90, spirit09, thecryptodrive, musicvoter2, cliffagreen, holoferncro, irgendwo, pandasquad, cnfund, dcrops, rocky1, dandesign86, brianoflondon, bflanagin, podping, neneandy, cheese4ead, apokruphos, kgakakillerg, metroair, meno, kgswallet, punchline, justyy, detlev, steemwizards, r00sj3, diabonua, tanzil2024, therising, steemean, sarashew, xeldal, mcsvi, adol, enki, killerbot, xxeldal, forykw, sanderjansenart, mafufuma, carilinger, kggymlife, grapthar, atheistrepublic, dondido, lightpaintershub, cryptictruth, jerrybanfield, lordvader, stem-espanol, iamphysical, tomastonyperez, elvigia, josedelacruz, fran.frey, lorenzor, analealsuarez, azulear, miguelangel2801, erickyoussif, andrick, uche-nna, aleestra, giulyfarci52, psicoluigi, mammasitta, dandays, eric-boucher, robertbira, imcore, nicole-st, flatman, podanrj, ennyta, endopediatria, stea90, bluefinstudios, georgehive, iammegankylie, gamersclassified, lud, roelandp, eturnerx,