Object - Oriented Programming ( OOP ) Dr , SERHANE Oussama o.serhane@esi - sba.dz Ecole Nationale Supérieure d’Informatique de Sidi Bel Abbes (ESI - SBA ) Second Year CPI 2022/2023 © all right reserved Esi - sba What is this course about? Chapter 01 : OOP General Introduction and Motivation Chapter 02 : Introduction to Java Programming Language Chapter 03 : Oriented Object Approach: Class and Object Chapter 04 : Oriented Object Approach: heritage, Polymorphism and Interface. Chapter 05 : Collections Chapter 06 : Exception handling and Management Chapter 07 : Graphical interface Chapter 08 : Streams and Files Object - Oriented Programming Esi - sba 2022/2023 2 Object - Oriented Programming Esi - sba 2022 / 2023 3 Chapter 03 : Oriented Object Approach - Class & Object Object - Oriented Programming Esi - sba 2022/2023 4 OOP Concept Class Object Inheritance Polymorphism Encapsulation Abstraction Object - Oriented Programming Esi - sba 2022/2023 5 Chapter 03 Outlines: Introduction to OOP What is a Class Class diagram in UML Class in java What is an Object Object diagram in UML Object in java Object Association in Java Summary Object - Oriented Programming Esi - sba 2022 / 2023 6 Introduction • Procedural programming is about writing functions that perform operations on the data, while OOP is about creating objects that contain both data and methods • OOP is a way of modeling software where everything is represented as an Object • Object/Objects could be any real - world or even a logical entity like a printing machine, a car, or a person. • Each object should have its own data and operations (methods) to perform on data. • Each object can communicate with other objects as and when required. It is similar to the real world Object - Oriented Programming Esi - sba 2022 / 2023 7 • Solving a problem in an object - oriented language, there is no need to ask how the problem will be divided into functions, but how it will be divided into objects • Thinking in terms of objects (instead of functions ) has a helpful effect on how easily you can design programs • OOP is a paradigm in computer science that strongly relies on the concept of classes and objects • OOP is used to structure a software program into simple , reusable pieces of code (called classes), which are used to create individual instances of objects OOP Basic Concepts Object - Oriented Programming Esi - sba 2022/2023 8 T he fundamental building blocks of an OOP program are: • Classes: offer templates to better characterize objects. classes serve as blueprints for generating objects. • Objects: are essential data fields with distinct structures that the programmer can determine. Once we call an object, the program creates an instance generated from a class. • Methods: encapsulate functions that handle the data and behavior of an object instance. • Attributes: Attributes (or variables in PP) refer to characteristics/proprieties of the object OOP Building blocks Object - Oriented Programming Esi - sba 2022 / 2023 9 What is a Class in OOP Object - Oriented Programming Esi - sba 2022 / 2023 10 New Car Type new Shape other Features ... Edit code again and again ! Example Cars using procedural programming: (chapter 01) Program Cars 1 Var carName:Str ; carType : Str ; ......... function AddCar ( carName , carType ... ) function DeleteCar ():... function SearchCar ():... ......... B E G IN ... END Program Cars 1 _ 2 Var carName:Str ; carType : Str ; ......... carName2:Str ; carType2 : Str ; ......... function AddCar 2 ( carName , carType ... ) function DeleteCar 2 ():... function SearchCar 2 ():... function AddCar ( carName , carType ... ) function DeleteCar ():... function SearchCar ():... .......... Program Cars 12 .. Var carName:Str ; carType : Str ; ......... carList : []; function AddCar 2 ( carName , carType ... ) function DeleteCar 2 ():... function SearchCar 2 ():... f unction AddT oList () ........ .......... Object - Oriented Programming Esi - sba 2022/2023 11 Oriented - Object Programming (Thinking): Single Car Class Car Object 01 Car Object 02 Car Object 03 Car Object 04 .... etc Attributes Methods Car_name Car_color Car_class Car_Options Get_clients () Set_Options () Get_clients () ............ Class name Object - Oriented Programming Esi - sba 2022 / 2023 12 The core element of OOP is the class Classes are essentially user - defined data types C lass is a template definition of the methods and attributes a Class is a group of objects that share common properties and behavior. We can create multiple objects from a class. What is a Class in OOP ? Object - Oriented Programming Esi - sba 2022/2023 13 Graphical representation of Class (UML) A class can be visualized as a three - compartment box, as illustrated: 1. Classname (or identifier): identifies the class. 2. Data Members or Variables (or attributes , states , fields ): contains the static attributes of the class. 3. Member Functions (or methods , behaviors , operations ): contains the dynamic operations of the class. Class Name Data Members (Static Attributes) Members Functions (Dynamic Operations) Object - Oriented Programming Esi - sba 2022 / 2023 14 The class is called “ Customer ” It has four attributes: name (which is text), address (also text), balance (a number), and credit rating (type double). It has one operation: “ printStatement () ” , which has no parameters or return value. Customer Name: String Adress : String Balance: i nt creditRating printStatement () Graphical representation of Class ( Exercises ) Classname attributes Methods Object - Oriented Programming Esi - sba 2022 / 2023 15 Student Name: String SchoolarYear : int grade: char getName () printGrade () getGrade () Circle Radious : double Color: String getRadious () getArea () FootballPlayer Name: String Number: int xLocation : float yLocation : float run() jump() kickball() Car MatNumber : String Type: String EnginePower : Str Model: String move() park() accelerate() Graphical representation of Class (Examples) Classname attributes Methods Object - Oriented Programming Esi - sba 2022/2023 16 What is class in OOP: Association between classes Object - Oriented Programming Esi - sba 2022/2023 17 Graphical representation of Class ( Association and multiplicity ) Student Name: String SchoolarYear : int grade: char getName () printGrade () getGrade () Course amount: String Date: String hours: String getTeacher () update() Team name: String Titles: String Stadium: String Tournament() ......... Play FootballPlayer Name: String Number: int xLocation : float yLocation : float run() jump() kickball() Car MatNumber : String Type: String EnginePower : Str Model: String move() accelerate() Person Name: String Adress : Strin ...... getName () getAdress () ...... Study * * * 1 1 1 Object - Oriented Programming Esi - sba 2022 / 2023 18 Class in Java Object - Oriented Programming Esi - sba 2022 / 2023 19 Class in java Syntax example Class Name Data Members (Static Attributes) Members Functions (Dynamic Operations) Object - Oriented Programming Esi - sba 2022/2023 20