Introduction to Object Oriented Programming PDF
Document Details

Uploaded by EnergyEfficientAllusion7267
Tags
Related
Summary
These are lecture notes about object-oriented programming. They cover topics such as the object-oriented programming paradigm, classes, and object concepts. The notes also discuss abstraction and encapsulation in OOP.
Full Transcript
Introduction to Object Oriented Programming In this lecture, the following topics are covered: The Object Oriented Programming paradigm The class and object concepts The abstraction concept The encapsulation concept The benefits of Object Oriented Programming 1. The Object Oriented Progr...
Introduction to Object Oriented Programming In this lecture, the following topics are covered: The Object Oriented Programming paradigm The class and object concepts The abstraction concept The encapsulation concept The benefits of Object Oriented Programming 1. The Object Oriented Programming paradigm The Object Oriented Programming (OOP) is a programming paradigm where concepts in the program are represented by “objects.” An object is an entity in the real world that has a state and exhibit behavior. For example, any real-life entity like a pen, a laptop, a mobile, a table, a chair, a car, etc. is an object. All these objects are either physical (tangible) or logical (intangible). Contrary to procedural programming, an object bundles both its data (which determines its state) and its procedures (which determines its behavior) in a coherent way. A key difference between OO and procedural programming is that OO uses local data stored in objects, whereas procedural programming uses global shared data that the various procedures can access directly. This has significant implications from a maintenance viewpoint. 6 من1 الصفحة In object-oriented programming (OOP), an application consists of a number of objects that ask services from each other. OO programming is the most popular programming paradigm currently in use. Some examples of object-oriented programming languages are Eiffel, Smalltalk, C++, and Java. 2. Characteristics of Object-Oriented Programming The following five basic characteristics represent a pure approach to object- oriented programming: a. Everything is an object. it stores data and performs operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (car, building, bank account, etc.) and represent it as an object in your program. b. A program is a number of objects telling each other what to do by sending messages. c. Each object has its own memory made up of other objects. you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects. d. Every object has a type. Each object is an instance of a class. e. All objects of a particular type can receive the same messages. 3. The Class and Object Concepts Objects of the same type are defined using a common class. A class is a template, blueprint, or contract that defines what an object’s data fields (state) and operations (behavior) will be. 6 من2 الصفحة An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. Writing an object-oriented program is largely a matter of designing classes and writing definitions for those classes in any OO based programming language (Java for instance). Designing a class is a matter of specifying all of the attributes (data variables) and behaviors (operations) that are characteristic of that type of object. A class is used to introduce a new data type in OOP, the Car type for example. A significant difference between the class and object is that the class is a logical concept while the object is a physical component (although, it could be a logical as well). o For example, to define the class of circle, the radius variable is used to represent the state of circle objects. Here the radius variable is the property that characterizes any circle. The current values of the radius represent different objects of the class circle. 6 من3 الصفحة In other words, we will have circles of different sizes based on the current value of each radius. o To define the behavior of circle objects, you may define operations named getArea () and getPerimeter (). These operations can be invoked on the circle object but not on the circle class. o Another example is the rectangle class. In this case, the variables width and height are used to characterize any rectangle and they together represent the state of any rectangle. The current values of the width and height represent different objects of the class rectangle. In other words, we will have rectangles of different sizes (dimensions) based on the current value of each width and height for each rectangle. o To define the behavior of rectangle objects, you may define operations named getArea () and getPerimeter (). These operations can be invoked on the rectangle object but not on the rectangle class. 4. Abstraction and Encapsulation The abstraction In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details. For example, when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc. We are not concerned about how the actual start/stop mechanism or accelerate/brake process works internally. 6 من4 الصفحة What we are concerned about is the “abstract” view of these operations that will help us to drive the car forward and reach our destination. This is a simple example of abstraction. Abstraction reduces the programming efforts and thereby the complexity. An end-user using the application need not be concerned about how a particular feature is implemented. He/she can just use the features as required. The encapsulation Encapsulation refers to combining data and associated functions as a single unit (the class). Encapsulation is the technique whereby the object's state is hidden from the outer world and a set of public methods for accessing this state are exposed. When each object keeps its state private inside a class, we can say that encapsulation was achieved. This is why encapsulation is also referenced as the information hiding mechanism. Encapsulation is all about implementation. Its main purpose is to hide the internal working of objects from the outside world so that you can change it later without impacting outside clients. 5. Benefits of Object oriented programming The following are some of the benefits of the Object Oriented Programming: Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear. Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system. 6 من5 الصفحة Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods (functions). This is due to the information hiding principle. Extensibility: adding new features can be solved by introducing a few new objects and modifying some existing ones; Maintainability: objects can be maintained separately, making locating and fixing problems easier. Re-usability: objects can be reused in different programs. 6 من6 الصفحة