Object-Oriented Programming - Lecture Notes PDF

Document Details

Uploaded by Deleted User

University of Science and Technology Houari Boumediene

Dr. F. Mekahlia

Tags

object-oriented programming programming paradigms computer science java

Summary

These lecture notes cover the fundamentals of object-oriented programming (OOP). The document explains various programming paradigms and illustrates examples of code, and also discusses the benefits and limitations of OOP. The document likely serves as course material for computer science students.

Full Transcript

UNIVERSITY OF SCIENCE AND TECHNOLOGY HOUARI BOUMEDIENE FACULTY OF COMPUTER SCIENCE Course Object-Oriented Programming ,, Chapter 1: Introduction to OOP 1 Dr MEKAH...

UNIVERSITY OF SCIENCE AND TECHNOLOGY HOUARI BOUMEDIENE FACULTY OF COMPUTER SCIENCE Course Object-Oriented Programming ,, Chapter 1: Introduction to OOP 1 Dr MEKAHLIA Fatma Zohra LAKRID Associate Professor MOVEP Laboratory COURSE OUTLINE FOR OOP1 (ING) Chapter 01: Introduction to OOP. ,, Chapter 02: OOP Basic Concepts. Chapter 03: Inheritance and Polymorphism Chapter 04: Abstraction, Interface and Implementation. 2 Dr. F. MEKAHLIA MOTIVATION ,,. What is the principle of programming ? 3 Dr. F. MEKAHLIA MOTIVATION Program Processing/ Treatment + under Data 4 Dr. F. MEKAHLIA MOTIVATION ,, Treatment Function 1 Function 2 … Function n 5 Dr. F. MEKAHLIA MOTIVATION ,, Data Variable 1 Variable 2 … Variable n 6 Dr. F. MEKAHLIA MOTIVATION Treatment ,, Function 1 Function 2 … Function n Variable 1 Variable 2 … Variable n 7 Data Dr. F. MEKAHLIA MOTIVATION  How do functions communicate with data? ,,  How do functions use data to find a result to the problem?  How is data used by functions?  So, the answer to this question will be different depending on the programming paradigm used to create your program! 8 PROGRAMMING PARADIGMS  A programming paradigm is: ,,. ✓ A way of approaching a problem, ✓ A way of thinking, ✓ A way to solve a problem, ✓ A realization of a programming philosophy. 9 Dr. F. MEKAHLIA PROGRAMMING PARADIGMS  Imperative programming (procedural) :  operations are presented in sequence for execution by the computer to modify the program state. ,,.  Different variables are declared and assigned values.  Variables evolve during execution and determine the evolution of the program. In procedural programming, a program = algorithm + data structure. Its dedicated languages are Pascal, C, etc. 10 Dr. F. MEKAHLIA PROGRAMMING PARADIGMS  Functional programming:  It takes an approach to mathematical programming.  is a sequence of evaluations of stateless functions. ,,.  Boolean references for functions that determine program progress.  Its dedicated languages are: Lisp, Matlab, OCaml, etc. 11 Dr. F. MEKAHLIA PROGRAMMING PARADIGMS  Logic programming: In logic programming, the description of a program is in the form of predicates. Example language: Prolog. ,,.  Object-Oriented Programming: Its dedicated languages are: Java, C++, C#, etc. 12 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS Imperative programming (procedural) : ,, Global data Local Local Local data data data 13 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS Imperative programming (procedural) : We give priority number 1 to treatment, i.e. in ,, the design we think about treatment first before data. The treatment is carried out by the functions that will use the data, So in this paradigm, we think of treatment before data !! 14 IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS  Program C Funct1{ void main (){ Data Global Data Funct 4 ,, Funct1 …….. Funct4{ } Data Funct2 …. } Funct2{ Funct3 Data } Funct 5 …..} Funct5{ Data …. } Funct3{ Data …..} 15 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS ,, 16 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS  Procedural programming is a paradigm that is based on the concept of procedural appeal. ,,  A procedure, also known as a routine, subroutine, or function, simply contains a series of steps that must be performed. Any procedure can be called at any stage of the program's execution, including within other procedures, or even within the procedure itself (recursion). 17 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS  The C language programmer is primarily interested in the processing that his program will have to perform. ,, Priority 1: Treatments. Priority 2: Data. 18 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS Disadvantages: ,,  Changes to one function cause other changes to other functions.  Code redundancy (the same part is coded several times).  Error propagation and difficult debugging. 19 Dr. F. MEKAHLIA IMPERATIVE / PROCEDURAL PROGRAMMING REMINDERS  In this paradigm, data and processing are treated independently of each other. ,,  The questions that can be asked in this case : 1. This separation (data, processing) useful? 2. Why prioritize processing over data? 3. Why not consider programs as sets of computer objects characterized by well-defined operations? 20 Dr. F. MEKAHLIA PROGRAMMING PARADIGMS Should we forget the C ? ,, NO You will have the choice between several programming paradigms depending on the problem to be addressed! 21 Dr. F. MEKAHLIA OBJECT-ORIENTED PROGRAMMING  Object-oriented languages were born to answer these questions. ,,  They are based on knowledge of a single category of entity, called an object.  In effect, an O. O program is made up of a set of objects, each with its own processing and data parts.  Objects interact with each other by sending and receiving messages. 22 OBJECT-ORIENTED PROGRAMMING ,, Data Data Data 23 Dr. F. MEKAHLIA OBJECT-ORIENTED PROGRAMMING  In object-oriented programming, the program is divided into parts called objects.  Object-oriented programming is a programming ,, concept that focuses on the object rather than actions and data.  The difference between procedural programming and object-oriented programming is that in procedural programming, programs are function- based and data can be easily accessed and edited, whereas in object-oriented programming, each program consists of entities called objects, which are not easily accessible and editable. 24 Dr. F. MEKAHLIA OBJECT-ORIENTED PROGRAMMING  OOP Benefits: Expresses the problem and its solution in terms of ,, real-world objects → Contributes to readability, maintenance, reuse, and code correction. The different access types that exist -> software that is secured by prohibiting or allowing access to certain objects. 25 Dr. F. MEKAHLIA OBJECT-ORIENTED PROGRAMMING  OOP Limit : It takes a lot of practice to assimilate all its concepts. ,, 26 Dr. F. MEKAHLIA COMPARISON BETWEEN PROCEDURAL AND OOP Procedural Paradigm:  Separate data from processing. ,,  Subdivision based on treatments. ✓ What should my program do? ✓ What functions do I need? Object-oriented paradigm:  Manipulate a set of objects that interact with each other.  Data-driven subdivision. ✓ What should my program consist of ? ✓ What are the entities of this program? 27 Dr. F. MEKAHLIA OBJECT-ORIENTED PROGRAMMING ,, messages Data Data Processing Processing Object 1 Object 2 Data Processing 28 Object 3 Dr. F. MEKAHLIA OBJECTS AND CLASSES  An object is a variable of complex type (as opposed to the primitive types integer, character, etc.). ,,  The type of an object is called: Class.  A class groups together data (variables) + processing (methods) acting on this data. 29 Dr. F. MEKAHLIA OBJECTS AND CLASSES  In other words: ,,  A class is the definition of a type, while an object is a declaration of variables. Once a class has been created, we can create as many objects based on that class.  An object is an instance of the class, i.e. a particular value of the class. 30 Dr. F. MEKAHLIA OBJECTS AND CLASSES : EXEMPLE 1 Bicycle: ,,  Its data (attributes) depends on: type, number of gears, size, current speed, color, etc.  While its behavior (methods) is to: ride, turn, accelerate, change gear, brake, etc.  Objects can be myBicycle , yourBicycle with different state values. 31 Dr. F. MEKAHLIA CLASSES  A class represents the structure of a group of objects sharing common properties and methods. It is used to declare attributes and define ,, methods.  A class is seen as a mold for creating objects, so an object would be an instance of a class. 32 Dr. F. MEKAHLIA CLASSES  Defining a class involves creating a new type. Once created, the class becomes a type, and objects can be associated with it. ,,  A class generalizes the notion of a type, and an object that of a variable.  A class is a set of objects that share : - the same methods. - the same attributes. 33 Dr. F. MEKAHLIA OBJECTS AND CLASSES  Classe = attributes + methods ,,  Object = state (attribute values) + behavior (execution of methods) 34 Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Shortbread cookies : To make a shortbread cookie, you need to define some properties (attributes): thickness (épaisseur), ,, nature, taste (goût), color. The cake undergoes processing (methods): create (), bake () (cuire), decorate (), 35 glaze (). Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Class = dough + mould : ,,  Create cookies with the same characteristics from a single mould  Objects or class instances = cookie1, cookie2, cookie3. 36 Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Create a first cookie (object 1) from a mould. ,, ✓Identité: 01 ✓Épaisseur: 4 ✓Nature: Non cuit ✓Goût: vanille ✓Couleur: jaune create () 37 Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Baking the cookie = another object 2. ,, ✓Identité: 02 ✓Épaisseur: 5 ✓Nature: consommable ✓Goût: vanille ✓Couleur: jaune create () 38 bake () Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Baking the cookie (a third object). ,, ✓Identité: 03 ✓Épaisseur: 5 ✓Nature: consommable ✓Goût: chocolat ✓Couleur: brown create () bake () 39 Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Decorate cookie (a fourth object). ,, ✓Identité: 04 ✓Épaisseur: 10 ✓Nature: consommable ✓Goût: vanille ✓Couleur: jaune create () bake () decorate () 40 Dr. F. MEKAHLIA OBJECTS AND CLASSES: EXAMPLE 2  Glaze cookie with sugar. ,, ✓Identité: 05 ✓Épaisseur: 11 ✓Nature: consommable ✓Goût: abricot ✓Couleur: blanc create () bake () glaze () 41 Dr. F. MEKAHLIA SUMMARY A cookie has ✓Identité: 05 properties ✓Épaisseur: 11 Each property has a (attributes ) ✓Nature: consommable value ,, ✓Goût: abricot ✓Couleur: blanc ✓créer, A cookie undergoes ✓cuire, processing / ✓décorer, methods (behaviors) ✓glacer. 42 Dr. F. MEKAHLIA SUMMARY ✓ All cookies have the same properties: Identité: entier Épaisseur: entier Nature: chaîne de caractères ,, Goût: chaîne de caractères Couleur: chaîne de caractères ✓ However, the values of these properties may differ from one cookie to another. ✓ The same methods can be applied to any cookie: ✓créer, ✓cuire, ✓décorer, ✓glacer. 43 Dr. F. MEKAHLIA INSTANTIATION: OBJECT  An object is a software entity:  Having an identity: a unique, invariant value that characterizes the object. ,,  Having properties/attributes: capable of saving a state, i.e. the set of values of the object's attributes.  Having methods: responding to specific messages that change the object's state. These operations are called methods. These are the processes that the object performs, which may or may not modify attribute values. 44 Dr. F. MEKAHLIA INSTALL THE DEVELOPER TOOLS Source code.java High-level language ,, (for humans) Compiler (by jdk) Byte code.class Cross-platform (portable) Interpreter (J V M by JRE) 45 Low-level language Object code (for machine) WHAT IS A JDK ? SDK SDK- Software Development Kit Available for various (developers tools) languages ,, JDK- Java Development Kit JDK (developers tools for java) For Java JRE – Java Runtime Envr. JRE ( Libraries & toolkits) JVM -Java Virtual Machine JVM (runs java programs in byte code To object code ) 46 WHAT IS A JDK ? ,, JDK Compilation JRE creates > virtual byteCode machine -> Creates the Java machine-specific code -> Development Kit Executes the program.exe.class 47 LINK TO DOWNLOAD JDK  https://www.oracle.com/java/technologies/javase- downloads.html ,, 48 WHAT IS AN IDE ?  IDE (Integrated Development Environment).  Is a software that helps us write our software. ,, 49 WHAT IS AN IDE ? ,, 50 WHERE CAN I GET AN IDE ? Eclipse IDE for Java Develpers ,, For windows 51 ,, Merci !! 52

Use Quizgecko on...
Browser
Browser