OOP Lecture Notes (Arabic) PDF
Document Details
Uploaded by WholesomeHafnium
عبد المطلب العبور
Tags
Related
Summary
These lecture notes provide an introduction to object-oriented programming (OOP). They explain the fundamental concepts of OOP, the differences between OOP and procedural programming, and why OOP was developed. The notes also cover key OOP concepts and introduce programming classes and their methods.
Full Transcript
What is object-oriented programming? Object-oriented programming (OOP): is a programming model that organizes software design around data, or objects. OOP: is a programming paradigm based on the concept of objects. programming paradigm: is a style of programming ,a way of thinking about s...
What is object-oriented programming? Object-oriented programming (OOP): is a programming model that organizes software design around data, or objects. OOP: is a programming paradigm based on the concept of objects. programming paradigm: is a style of programming ,a way of thinking about software construction. Programing paradigm does not refer to a specific language, but rather it refers to the way to build a program or a methodology to apply. عبد المطلب العبور 1 Why Was Object-Oriented Programming Developed? Object-Oriented Programming (OOP) was developed in response to some limitations found in earlier programming paradigms, particularly the procedural approach. Procedural Programming: Early programming techniques, especially procedural programming, focused on writing sequences of instructions to perform tasks. Programs were structured around procedures or functions, and data was passed between these functions. عبد المطلب العبور 2 Why Was Object-Oriented Programming Developed? Limitations of Procedural Programming: 1.Code Reusability Issues: Procedural code often led to repetitive code structures, making it harder to reuse code. It was more difficult to maintain and extend large programs. 2.Data Security Problems: Data in procedural programming is often exposed to the entire program, making it hard to control access or protect data. 3.Tight Coupling of Data and Logic: Procedural programs tightly coupled data and functions, making it difficult to update parts of the code without affecting other areas. 4.Complexity in Large Programs: As programs grew larger, procedural programming struggled to organize the code effectively. Managing functions and data across larger applications became harder, leading to unmanageable "spaghetti code." عبد المطلب العبور 3 The Shift to Object-Oriented Programming Object-Oriented Approach: To address these limitations, Object-Oriented Programming (OOP) was introduced. The focus shifted from procedures (functions) to objects htob etaluspacne hcihw ,data and behavior.(sdohtem) Key Advantages of OOP: 1.Modular Design: Large programs are broken into smaller, manageable objects, making code organization easier. 2.Code Reusability: Through inheritance and class hierarchies, OOP promotes code reuse and reduces redundancy. 3.Data Security: Encapsulation restricts direct access to an object's data, protecting it from unintended interference. 4.Easier Maintenance and Scalability: OOP's modular design simplifies updates and enhancements by allowing developers to change parts of the system without affecting others. عبد المطلب العبور 4 OOP vs. Procedural Programming عبد المطلب العبور 5 OOP vs. Procedural Programming Languages like Pascal, C, FORTRAN, and COBOL are called procedure oriented programming languages Since in these languages, a programmer uses procedures or functions to perform a task When the programmer wants to write a program, he will first divide the task into separate sub tasks, each of which is expressed as functions/ procedures This approach is called procedure oriented approach. The languages like C++ and Java use classes and object in their programs and are called Object Oriented Programming languages The main task is divided into several modules and these are represented as classes Each class can perform some tasks for which several methods are written in a class This approach is called Object Oriented approach عبد المطلب العبور 6 OOP Concepts: There are 6 OOP Concepts: Object. Class. Polymorphism. Inheritance. Encapsulation. Abstraction. عبد المطلب العبور 7 Classes in Object-Oriented Programming What is a Class? A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) that the objects created from the class will have. A class defines what an object is and what it can do. Property A property is a characteristics or attribute of an object which will be defined in a class. For example on the next slide Engine Power, Model, Speed Limit, Color are the property/Attribute/Characteristics of a Car. Method Methods are the behavior or function of an object that can be performed by the object. Methods also called the tasks or action of the objects. Example: Taking methods from the next example. Start(), Stop(), Backward(), Forward() etc. are the basic action/function or method of a car object. عبد المطلب العبور 8 Classes in Object-Oriented Programming عبد المطلب العبور 9 Classes in Object-Oriented Programming Class definition Syntax: class class_name { // member variables (Attribute or properties) // class methods } عبد المطلب العبور 10 Classes in Object-Oriented Programming Variables inside a class are called as instance variables. Variables inside a method are called as method variables. عبد المطلب العبور 11 Access Modifiers Access modifiers help to restrict the scope of a class, constructor, variable, method, or data member. It provides security, accessibility, etc to the user depending upon the access modifier used with the element. Keyword Scope Visible to all classes everywhere. If a class, method, or variable is declared as Public public, it can be accessed from any other class, regardless of the package. Visible only within the same class. If a method or variable is declared private, it Private can only be accessed and modified within that class. Protected Visible within the same package and by subclasses (in any package). Visible only within the same package. The class, method, or variable is not Default accessible outside the package but is accessible to any other class in the same package. عبد المطلب العبور 12 Access Modifiers Notes: Classes can only be declared public or default (package-private). They cannot be private or protected. Members (variables, methods) can use any of the four access modifiers. عبد المطلب العبور 13 Attributes: color (String): Represents the color of the car. price (double): Represents the price of the car. speed (int): Represents the current speed of the car. Methods: start(): Increases the speed to 10 and prints a message indicating the car has started. stop(): Sets the speed to 0 and prints a message indicating the car has stopped. عبد المطلب العبور 14 Objects in Object-Oriented Programming Object An object is an entity or anything that have some property/behavior/attribute and actions/methods/functions. An object can be a thing, concept place, person, real world object, etc. Instance/Instantiation When we create the object of a class, it is also the instance. The process of creating the object from a class is called instantiation. An instance of class is called object. Any entity that has state and behavior is known as an object. An object contains an address and takes up some space in memory. عبد المطلب العبور 15 Objects in Object-Oriented Programming عبد المطلب العبور 16 Objects in Object-Oriented Programming declaring an object called x from type of class Car عبد المطلب العبور 17 Constructors A constructor is similar to a method that is invoked when an object of the class is created. Unlike methods, a constructor has the same name as that of the class and does not have any return type. عبد المطلب العبور 18 Constructors Here, Car() is a constructor. It has the same name as that of the class and doesn't have a return type. Output: Green عبد المطلب العبور 19 Constructor Overloading - Multiple Constructors A class can have multiple constructors, as long as their signature (the parameters they take) are not the same. You can define as many constructors as you need. When a Java class contains multiple constructors, we say that the constructor is overloaded (comes in multiple versions). This is what constructor overloading means. عبد المطلب العبور 20 Constructor Overloading - Multiple Constructors The Car class contains Three constructors. The first constructor takes two parameters, The second constructor takes a string parameter. And the third constructor takes a double parameter. Now we can declare an objects from the class Car as following: عبد المطلب العبور 21 Constructors Class can have special methods called Constructors. A constructor is a special member function that is executed automatically whenever an object is created. Automatic Initialization: constructor typically initialize object attributes and perform other object initialization tasks. constructor are used to perform operations at the time an object is created. عبد المطلب العبور 22 Constructors properties Have same name as the Class. Have No return type. Have No return value. Are typically public. عبد المطلب العبور 23 Constructors Example Here, Car() is a constructor. It has the same name as that of the class and doesn't have a return type. Output: Green عبد المطلب العبور 24 Destructor As opposite of constructor, the destructor is called automatically when an object life cycle is ended (object is destroyed). Like constructors, destructors do not have a return value. Destructors take no arguments. The most common use of destructors is to deallocate memory that was allocated for the object by the constructor. Like constructor, has the same name of the class but is preceded by a tilde ~ in C++. Example: ~Car() عبد المطلب العبور 25 Destructor Java does not have destructors like C++. In Java, the garbage collector automatically manages memory and resources, so destructors are not necessary. However, Java has a method called finalize(), which is somewhat analogous to a destructor, but it's not recommended for resource management. عبد المطلب العبور 26 Destructor عبد المطلب العبور 27 Destructor Remember that there is no concept of destructor in Java. In place of the destructor, Java provides the garbage collector that works the same as the destructor. The garbage collector is a program (thread) that runs on the JVM. It automatically deletes the unused objects (objects that are no longer used) and free-up the memory. عبد المطلب العبور 28 Method Overloading Two or more methods can have same name as long as their parameters declaration(signatures) are different. Method signatures (number of Arguments ,types of Arguments, order of Arguments). When this occurs, it is called method overloading. This also applies to Constructor. Method overloading is important because sometimes you need several different ways to perform the same operation. Method overloading increases the readability of the program. عبد المطلب العبور 29 Different ways to overload the method There are two ways to overload the method in java: By changing number of arguments By changing the data type عبد المطلب العبور 30 Method Overloading: changing no. of arguments Output: 11 9 عبد المطلب العبور 31 Method Overloading: changing data type of arguments Output: 11 12.0 عبد المطلب العبور 32 Why Method Overloading is not possible by changing the return type of method only? In java, method overloading is not possible by changing the return type of the method only because of ambiguity. Ambiguity during method calls: When you call an overloaded method, the Java compiler uses the method signature (method name and parameter list) to decide which method to invoke. If two methods have the same name and parameter list but only differ in return type, the compiler would not be able to differentiate between them at the call site. The return type is not part of the method signature: According to Java's language specification, a method's signature is made up of: 1. Method name 2. Number and type of parameters The return type is not included in the signature because the method call itself does not specify what return type is expected, only the parameters. عبد المطلب العبور 33 Why Method Overloading is not possible by changing the return type of method only? عبد المطلب العبور 34 Method Overloading and Type Promotion One type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the figure given below: As displayed in the diagram, byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int,long,float or double and so on. عبد المطلب العبور 35 Example of Method Overloading with TypePromotion Output: 11 11.0 عبد المطلب العبور 36 Example of Method Overloading with Type Promotion if matching found Output: int arg method invoked 11 عبد المطلب العبور 37 Example of Method Overloading with Type Promotion in case of ambiguity Output: The method add(int, long) is ambiguous for the type Calculator عبد المطلب العبور 38 Encapsulation Encapsulation is one of the fundamental concepts in Object-Oriented Programming (OOP). It refers to the practice of bundling the data (attributes or fields) and methods (functions or behaviors) that operate on the data into a single unit or class, while restricting direct access to some of the object's components. This is done to protect the internal state of the object and to prevent unauthorized access or modification. عبد المطلب العبور 39 Key Concepts of Encapsulation ❑ Data Hiding: Encapsulation allows the internal details of an object (such as the state of its fields) to be hidden from the outside world. External code can only access or modify the data through controlled interfaces, such as getter and setter methods. Fields in a class are typically declared as private, meaning they are only accessible within that class. This prevents external classes or functions from directly modifying the fields. عبد المطلب العبور 40 Key Concepts of Encapsulation ❑ Controlled Access: To interact with the private data, classes provide public methods, typically called Getters (to retrieve the data) and Setters (to modify the data). These methods can include validation or other logic to ensure the integrity of the data. By providing controlled access, encapsulation ensures that the object's state is maintained consistently and can prevent unintended side effects. عبد المطلب العبور 41 Key Concepts of Encapsulation ❑ Implementation Details are Hidden: With encapsulation, the internal implementation of an object is hidden, allowing the object to be changed or improved without affecting the external code that depends on it. As long as the public interface (i.e., the methods) remains the same, the internal workings of the class can be modified without breaking the rest of the system. عبد المطلب العبور 42 Benefits of Encapsulation: Data Protection: It ensures that sensitive data is hidden from the outside world and can only be accessed in a controlled manner, reducing the chance of accidental or malicious changes. Modularity: Each object in the program is self-contained. This makes it easier to understand and modify individual parts of the system without affecting others. Maintainability: Changes to the internal workings of a class can be made without affecting external code, provided the public interface remains consistent. Reusability: Encapsulated code can be reused across different parts of the application or even in other applications, since its internal logic is hidden, and only the interface is exposed. عبد المطلب العبور 43 Encapsulation: Getter and Setter Example Private Fields: The fields name and mark are marked as private to ensure they cannot be directly accessed or modified from outside the class. The getter methods (getName(), and getMark()) provide read-only access to the fields. The setter methods (setName(), and setMark()) allow controlled modification of the fields. عبد المطلب العبور 44 Encapsulation: Getter and Setter Example Output: Student Name: Ahmed Ali Student Mark: 90.0 عبد المطلب العبور 45 Encapsulation: Setter Validation Output: Mark should be between 0 and 100 Student Name: Ahmed Ali Student Mark: 85.5 عبد المطلب العبور 46 this keyword There can be a lot of usage of Java this keyword. In Java, this is a reference variable that refers to the current object. عبد المطلب العبور 47 Usage of Java this keyword 1. this can be used to refer current class instance variable. 2. this can be used to invoke current class method (implicitly) 3. this() can be used to invoke current class constructor. 4. this can be passed as an argument in the method call. 5. this can be passed as argument in the constructor call. 6. this can be used to return the current class instance from the method. عبد المطلب العبور 48 Understanding the problem without this keyword Output: Student Name: null Student Mark: 0.0 In this example, parameters (formal arguments) and instance variables are same. So, we are using this keyword to distinguish local variable and instance variable عبد المطلب العبور 49 Understanding the problem without this keyword Output: Student Name: Ahmed Student Mark: 80.0 عبد المطلب العبور 50 this() : to invoke current class constructor Output: hi عبد المطلب العبور 51 Calling parameterized constructor from default constructor: Output: hi عبد المطلب العبور 52 this: to pass as an argument in the method Output: student name: Fatima student mark: 77.0 عبد المطلب العبور 53 Inheritance Inheritance in Object-Oriented Programming (OOP) is a mechanism where a new class (called a derived or child class) acquires the properties and behaviors (methods and fields) of an existing class (called a base, parent, or super class). It allows for code reuse and the creation of hierarchical relationships between classes. عبد المطلب العبور 54 Key Concepts in Inheritance: 1. Base (Parent) (Super) Class: The class whose properties and methods are inherited by another class. 2. Derived (Child) (Sub) Class: The class that inherits from the base class. 3. Is-a Relationship: The child class is a specialized version of the parent class (e.g., a Cat is a specific type of Animal). 4. Access Modifiers: Control how the base class members are accessed in the derived class: public: Accessible everywhere. protected: Accessible in the base class and derived classes. private: Accessible only within the base class. عبد المطلب العبور 55 Benefits of Inheritance: Code Reusability: Classes can reuse code from base classes. Extensibility: Classes can be extended with new functionality without modifying existing code. Hierarchy: Helps model real-world relationships in a hierarchical structure. عبد المطلب العبور 56 The syntax of Java Inheritance The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass. عبد المطلب العبور 57 Types of inheritance in java On the basis of class, there can be three types of inheritance in java: single, multilevel and hierarchical. In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later. عبد المطلب العبور 58 Types of inheritance in java When one class inherits multiple classes, it is known as multiple inheritance. For Example: Multiple inheritance is not supported in Java through class. عبد المطلب العبور 59 Single Inheritance Example When a class inherits another class, it is known as a single inheritance. In the example given below, student class inherits the Person class, so there is the single inheritance. Ahmed Ali 88.5 عبد المطلب العبور Misrata 60 Multilevel Inheritance Example When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, GradeStudent class inherits the Student class which again inherits the Person class, so there is a multilevel inheritance. Fatima Mohammed 66.5 Misrata 2024 عبد المطلب العبور 61 Hierarchical Inheritance Example When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Student and Teacher classes inherits the Person class, so there is hierarchical inheritance. Output: Kamal Mohamed Master Aisha Abdullah 79.51 عبد المطلب العبور 62 Why multiple inheritance is not supported in java? To reduce the complexity and simplify the language, multiple inheritance is not supported in java. Diamond Problem The diamond problem is the classic issue that can arise with multiple inheritance. It occurs when the class inherits from the two classes that have a common ancestor. This situation forms the diamond-shaped inheritance hierarchy. It can lead to ambiguity in the method resolution. Consider the scenario where class D inherits from classes B and C which both inherits from Class A. This creates the diamond-shaped inheritance hierarchy. عبد المطلب العبور 63 Why multiple inheritance is not supported in java? Diamond Problem عبد المطلب العبور 64 Diamond problem with example methods code: عبد المطلب العبور 65 Method Overriding If subclass (child class) has the same method as declared in the parent class, it is known as method overriding. The overridden method in the subclass should have the same name, return type, and parameters as the method in the superclass. عبد المطلب العبور 66 Method Overriding Let's understand the problem that we may face in the program if we don't use method overriding. Output: Person Name:Ahmed Ali Problem is that I have to provide a specific implementation of DisplayName() method in subclass that is why we use method overriding. عبد المطلب العبور 67 Method Overriding Output: Student Name is:Ahmed Ali, Id is: 0924545 عبد المطلب العبور 68 Calling Parent Class constructor super(): This keyword is used to explicitly call the constructor of the parent class. If you don't include super(), the Java compiler automatically inserts a call to the no-argument constructor of the parent class If the parent class doesn't have a no-argument constructor, you must explicitly call the appropriate constructor using super(). عبد المطلب العبور 69 Calling Parent Class constructor Example عبد المطلب العبور 70 Abstract class Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstract methods: These are methods declared without an implementation (abstract keyword), which means the subclasses must provide the implementation. Concrete methods: These are regular methods with implementation that can be used by subclasses. You cannot create objects of an abstract class. An abstract class can have constructors, but those constructors are called when instantiated through a subclass. A class that extends an abstract class must either implement all the abstract methods or be declared abstract itself. عبد المطلب العبور 71 Abstract class عبد المطلب العبور 72 Abstract class Output: Area of Rectangle is 60.0 عبد المطلب العبور 73 Abstract class Output: Area of Circle is 78.53981633974999 عبد المطلب العبور74 Interfaces An interface is a reference type, similar to a class, that is used to specify a set of abstract methods that a class must implement. Interfaces provide a way to define a contract for what a class can do, without dictating how it does it. They are often used for achieving polymorphism and multiple inheritance (since Java doesn’t support multiple inheritance of classes). 75 عبد المطلب العبور Key Characteristics of Interfaces Abstract Methods: By default, all methods in an interface are abstract (they do not have a body), unless they are default or static methods. No State: Interfaces cannot have instance variables (non-static fields), although they can define static and final constants Multiple Inheritance: A class can implement multiple interfaces, providing a workaround for multiple inheritance. Public Accessibility: All methods in an interface are implicitly public, and all variables are public, static, and final. 76 عبد المطلب العبور Relationship Between Classes and Interfaces As shown in the figure given below, a class extends another class, an interface extends another interface, but a class implements an interface. 77 عبد المطلب العبور Multiple Inheritance in Java by Interface If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. 78 عبد المطلب العبور Interface Example Output: student attends OOP and english language. student studing OOP and Database Systems. Taking a break... 79 عبد المطلب العبور Multiple Inheritance The concept of inheritance, which enables classes to adopt features and attributes from other classes, is fundamental to object-oriented programming. Due to Java's support for single inheritance, a class can only descend from one superclass. Java offers a method for achieving multiple inheritances through interfaces, enabling a class to implement many interfaces. We will examine the idea of multiple inheritance in Java, how it is implemented using interfaces, and use examples to help us understand. 80 عبد المطلب العبور Multiple Inheritance student attends OOP and english language student studing OOP and Database Systems Taking a break... total marks is: 196.37 Student average is: 15.70 81 عبد المطلب العبور Virtual Function in Java A virtual function or virtual method in an OOP language is a function or method used to override the behavior of the function in an inherited class with the same signature to achieve the polymorphism. By default, all the instance methods in Java are considered as the Virtual function except final, static, and private methods as these methods can be used to achieve polymorphism. We can override the virtual function with the inheriting class function using the same function name. Generally, the virtual function is defined in the parent class and override it in the inherited class. in the derived class. We can call it by referring to the derived class's object using the reference or pointer of the base class. 82 عبد المطلب العبور Virtual Function in Java A virtual function should have the same name and parameters in the base and derived class. For the virtual function, an IS-A relationship is necessary, which is used to define the class hierarchy in inheritance. The Virtual function cannot be private, as the private functions cannot be overridden. A virtual function or method also cannot be final, as the final methods also cannot be overridden. Static functions are also cannot be overridden; so, a virtual function should not be static. By default, Every non-static method in Java is a virtual function. 83 عبد المطلب العبور Virtual Function in Java Output: This is student 84 عبد المطلب العبور Summary Below are some consideration points about the Virtual function in Java: The Virtual Function is an ordinary function in Java. In Java, no virtual keyword is used to define the virtual function. The Parent class pointer is used to refer to the object of the child class The virtual function should be defined in the child class with the same name in the parent class. All the instance methods in Java are considered the Virtual function except final, static, and private methods. 85 عبد المطلب العبور Final Keyword In Java The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: 1. variable 2. method 3. class 86 عبد المطلب العبور Java final variable If you make any variable as final, you cannot change the value of final variable(It will be constant). Output: Compile Time Error 87 عبد المطلب العبور Java final method If you make any method as final, you cannot override it. Output: Compile Time Error 88 عبد المطلب العبور Java final class If you make any class as final, you cannot extend it. 89 عبد المطلب العبور final parameter If you declare any parameter as final, you cannot change the value of it. Output: Compile Time Error 90 عبد المطلب العبور Summary Final method is inherited but you cannot override it. We can initialize blank final variable but only in constructor. We can’t declare constructor final, because constructor is never inherited. 91 عبد المطلب العبور Difference Between Static and Final in Java BASIS FOR STATIC FINAL COMPARISON Static keyword is applicable to nested static Final keyword is applicable to class, Applicable class, variables, methods and block. methods and variables. It is not compulsory to initialize the static It is compulsory to initialize the final Initialization variable at the time of its declaration. variable at the time of its declaration. Modification The static variable can be reinitialized. The final variable can not be reinitialized. Static methods can only access the static Final methods can not be inherited. Methods members of the class and can only be called by other static methods. Static class's object can not be created, and A final class can not be inherited by any Class it only contains static members only. class. 92 عبد المطلب العبور Introduction In object-oriented programming, relationships between classes play a crucial role in defining how objects interact with each other. Java, being an object-oriented language, provides mechanisms to model these relationships through association, aggregation, and composition. Aggregation, association, and composition in Java describe how instances of classes relate to each other. 93 عبد المطلب العبور عبد المطلب العبور 94 Association Association is a relationship between two classes that establishes a connection where one class uses or interacts with another. Each is a separate object, but together they represent the whole. The association refers to relationships whose participant objects have independent lifecycles and there is no ownership between the objects. Association can be viewed as describing a “uses-a” relationship where an object uses, or in any way interacts with, another object. Association implemented by creating references to other objects (fields) in the classes, often using collections like List or Set for multiple associations. 95 عبد المطلب العبور Type of Association One-to-One: One object of class A is associated with only one object of class B (e.g., Person and Passport). One-to-Many: One object of class A is associated with multiple objects of class B (e.g., Teacher and Students). Many-to-One: Multiple objects of class A are associated with one object of class B (e.g., Students and School). Many-to-Many: Multiple objects of class A are associated with multiple objects of class B (e.g., Students and Courses). 96 عبد المطلب العبور Direction of Association Unidirectional Association: This association is the one in which one class is aware and associated with another class; the reverse is not true. For example, the Student class can be associated with the LibraryCard class, for the association where the student has a library card; a LibraryCard does not need to ‘know about’ a Student. Bidirectional Association: In this type of association, the classes are aware of each other and interact with one another. For example, a Teacher class and a Classroom class may be associated bidirectionally; there would be a teacher assigned to a classroom, and a classroom would know to which teacher it is assigned. 97 عبد المطلب العبور Association Example – one-to-one 98 عبد المطلب العبور Association Example – one-to-many 99 عبد المطلب العبور Association Example – many-to-many 100 عبد المطلب العبور Aggregation Aggregation is a type of association with a "has-a" relationship, where one class contains a reference to another class, but the contained class can exist independently. For instance, a Department has multiple Teacher objects, but a Teacher can exist independently outside of a Department. Aggregation represents a weak relationship between classes. The whole class plays a more important role than the part class, but unlike the case of composition, the part class can meaningfully exist on its own without the whole class. Aggregation is a little restrictive type of association. It refers to relationship between two classes such that objects of both classes can have independent lifecycle, but object of one class will belong to a specific object of another class in its lifetime 101 عبد المطلب العبور Aggregation example Each Department has multiple Teacher objects, but a Teacher can exist independently outside of a Department. 102 عبد المطلب العبور Composition Composition is the more strict relationship between two classes. It to refer to relationships where two objects don’t have an independent lifecycle, and if the parent object is deleted, all child objects will also be deleted. Composition is a core concept in object-oriented programming that refers to the relationship “part-of” between classes. Composition is a type of association meaning one class “contains” another. This association can be said to be a “part-of” relationship and would denote that the contained object is strongly connected with the containing object, the whole. The parts cannot be without the whole. 103 عبد المطلب العبور Composition Let’s take an example of the relationship between questions and answers. A multiple-choice question can have multiple answers. But an answer cannot belong to multiple questions. If we delete a question, its answers will automatically be deleted. 104 عبد المطلب العبور Class Diagram Class diagrams are a type of UML (Unified Modeling Language) diagram used in software engineering to visually represent the structure and relationships of classes within a system i.e. used to construct and visualize object-oriented systems. 105 عبد المطلب العبور Class Diagram 106 عبد المطلب العبور UML Class Notation 1.Class Name: 1. The name of the class is typically written in the top compartment of the class box and is centered and bold. 2.Attributes: 2. Attributes, also known as properties or fields, represent the data members of the class. They are listed in the second compartment of the class box and often include the visibility (e.g., public, private) and the data type of each attribute. 3.Methods: 3. Methods, also known as functions or operations, represent the behavior or functionality of the class. They are listed in the third compartment of the class box and include the visibility (e.g., public, private), return type, and parameters of each method. 107 عبد المطلب العبور UML Class Notation 4.Visibility Notation: Visibility notations indicate the access level of attributes and methods. Common visibility notations include: + for public (visible to all classes) - for private (visible only within the class) # for protected (visible to subclasses) ~ for package or default visibility (visible to classes in the same package) 108 عبد المطلب العبور Class Diagram 109 عبد المطلب العبور Class Diagram 110 عبد المطلب العبور Parameter Directionality In class diagrams, parameter directionality refers to the indication of the flow of information between classes through method parameters. It helps to specify whether a parameter is an input, an output, or both. This information is crucial for understanding how data is passed between objects during method calls. 111 عبد المطلب العبور Parameter Directionality There are three main parameter directionality notations used in class diagrams: In (Input): An input parameter is a parameter passed from the calling object (client) to the called object (server) during a method invocation. It is represented by an arrow pointing towards the receiving class (the class that owns the method). Out (Output): An output parameter is a parameter passed from the called object (server) back to the calling object (client) after the method execution. It is represented by an arrow pointing away from the receiving class. InOut (Input and Output): An InOut parameter serves as both input and output. It carries information from the calling object to the called object and vice versa. It is represented by an arrow pointing towards and away from the receiving class. 112 عبد المطلب العبور Relationships between classes 113 عبد المطلب العبور Relationships between classes 114 عبد المطلب العبور Relationships between classes 115 عبد المطلب العبور Relationships between classes 116 عبد المطلب العبور Relationships between classes 117 عبد المطلب العبور Generalization(Inheritance) 118 عبد المطلب العبور Interface عبد المطلب العبور 119 عبد المطلب العبور Indicator Meaning 0..1 Zero or one 1 One only Multiplicity 0.. Zero or more values and Zero or more their 1..* One or more indicators 3 Three only 0..5 5..15 Zero to Five Five to Fifteen 120 Multiplicity values and their indicators 121 عبد المطلب العبور Multiplicity values and their indicators The relationship drawn in the Figure means that an instance of Employee can be the manager of another Employee instance. However, because the relationship role of "manages" has a multiplicity of 0..*; an Employee might not have any other Employees to manage. 122 عبد المطلب العبور Abstract class in UML You have to write the class name in italic : in this example Person class written in italic format which mean it’s Abstract class. 123 عبد المطلب العبور Packages If you are modeling a large system or a large area of a business, there will be many different classifiers in your model. Managing all the classes can be a daunting task; therefore, UML provides an organizing element called a package. Packages enable modelers to organize the model's classifiers into namespaces, which is sort of like folders in a filing system. Dividing a system into multiple packages makes the system easier to understand, especially if each package represents a specific part of the system. 124 عبد المطلب العبور Packages عبد المطلب العبور 125 Packages عبد المطلب العبور 126 Class Diagrams - Examples 127 عبد المطلب العبور Class Diagrams - Examples 128 عبد المطلب العبور Class Diagrams - Examples 129 عبد المطلب العبور The attributes term and grade really belong to the many-to many association between Student and Course. The grade of a student for a course cannot be determined unless both the student and the course are known. Similarly, to find the term(s) in which the student took the course, both student and course must be known. The check Eligibility operation, which determines if a student is eligible to register for a given course, also belongs to the association, rather than to any of the two participating classes. We have also captured the fact that, for some course registrations, a computer account is issued to a student. For these reasons, we model Registration as an association class, with its own set of features and an association with another class (Computer Account). 130 عبد المطلب العبور عبد المطلب العبور 131