Week 1 - Intro2OOP (3) PDF
Document Details
Uploaded by Deleted User
Tags
Related
Summary
This presentation provides an introduction to Object-Oriented Programming (OOP) concepts. It explains the difference between procedural and object-oriented programming, discusses the role of objects and classes, and briefly touches on common OOP principles like encapsulation and inheritance within the context of programming.
Full Transcript
STIAK1113 Programming 2 Introduction to Object-Oriented Programming Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate pieces that are responsible for certain parts of the solution...
STIAK1113 Programming 2 Introduction to Object-Oriented Programming Problem Solving The key to designing a solution is breaking it down into manageable pieces When writing software, we design separate pieces that are responsible for certain parts of the solution Problem Solving Two popular programming design methods: ▪ Procedural Programming ▪ Object-Oriented Programming (OOP) Procedural Programming Main problem is divided into sub- problems All sub-problem solutions (subtasks) are implemented as procedures(methods) and combined to solves the main problem procedures operate on data items that are separate from the procedures (data items are commonly passed from one procedure to another) The separation of data and the procedures that operates on the data can lead to problems as the program becomes larger and more complex Object-Oriented Programming Object based approach ▪ objects as foundation for problem solving Identify objects from the main problem An OOP program is a collection of objects that interact with each other to solve the main problem (ie: student, lecturer, course etc) Each object is a software entities that contains both data and procedures related to the object Object-Oriented Programming More natural way to solve problem as soft ware objects can be used to represent real-world objects For instance, a program for University Student Information would have different objects to represent student, lecturer, course, schedule etc. Each type of object handles the processing and data management related to that object Procedural vs Object-Oriented Objects Object-oriented programs use objects, which represent real world objects. A real world object is a thing, both tangible (e.g handphone) and intangible (e.g. bank account). A software object represents a real world object by having ▪ State (data/fields/attributes) - descriptive characteristics ▪ Behaviors(procedures/methods/operations) - what it can do (or what can be done to it) Objects Example: Bank Account object The state of a bank account object includes its account number (eg. 184739484-023), its current balance (RM5000), owner’s name (eg. Siti Hajar) , etc. The behaviors associated with a bank account object include the ability to make deposits, withdrawals, check balance etc. Note that the behavior of an object might change its state Classes To create an object , we MUST provide a definition/description for it A class is the blueprint of an object It contains the codes that specifies the states and behaviours of a particular type of object. Each object that is created from a class is called an instance of the class. Multiple objects can be created from the same class during program execution & stored in the main memory Classes vs Objects Classes vs Objects A Dog class & 2 instances/objects of Dog class Class Diagram When designing a class, it is often helpful to draw a Unified Modeling Language (UML) class diagram. Consists of a rectangle divided into three sections, The top section contains the name of the class, the middle section contains the names and data types of the attributes(data), and the bottom section contains the methods. Class Diagram Examples of Employee class diagram 4 UNIQUE CHARACTERISTICS OF OOP Abstraction Encapsulation Inheritance Polymorphism Abstraction Abstraction focus upon the essential characteristics of object, relative to the perspective of the viewer. The process of showing the essential details or features of an object and hiding its complexity. Object in a program is an abstraction of the real-world object. Example: a cat object Encapsulation Process of combining all of an object’s data and methods into a single unit (class) The data are frequently hidden from outside classes while methods are accessible to outside classes Data hiding - the concept that other classes should not alter an object’s attributes—only the object’s methods should have that privilege. An object protects & manages its own information (data & methods) Encapsulation To keep data inaccessible to outside classes, they can be assigned with private access To make methods accessible to outside classes, they can be assigned with public access (Note: some methods can be also be private) In UML class diagram, a minus sign (–) precedes the items that are private & a plus sign (+) precedes those that are public. Encapsulation Employee class diagram with private & public access specifier Inheritance Inheritance allows a new class (a.k.a. child class) to be created by extending an existing class (a.k.a. parent class). The new child class inherits the members of the parent class it extends. Reduces the work involved in creating new class – no need to repeat or rewrite the code from scratch (allow reusability ) Inheritance Classes can be organized into hierarchies Polymorphism The term polymorphism refers to an object’s ability to take different forms Allows you to create methods with the same name but with different implementations in different classes (that are related through inheritance) The ability to call the correct method depending on the type of object that is used to call it. Polymorphism Polymorphism Examples of polymorphism: