UML Class Diagrams and Object-Oriented Programming

Summary

This document provides an overview of object-oriented programming (OOP) concepts such as recursion, encapsulation, and polymorphism, and explores the use of UML class diagrams. UML class diagrams and relationships that are used to visualize software design are explained. It covers the advantages and disadvantages of OOP and also includes key terms and definitions related to OOP.

Full Transcript

D4 D4.1 -problem solving Recursion – specific approach to problem solving in computer science - The solu;on to a problem depends on solu;ons to smaller instances of the same problem In programming, recursion happens when a procedur...

D4 D4.1 -problem solving Recursion – specific approach to problem solving in computer science - The solu;on to a problem depends on solu;ons to smaller instances of the same problem In programming, recursion happens when a procedure calls itself un;l some termina;ng condi;on is met This is accomplished without any specific repe;;on construct, such as a while or a for loop Recursion breaks the problem at hand into smaller subtasks - Most algorithms that may be presented recursively may also be presented in an itera;ve manner and vice versa ! UNIT 5 – SL D1 1.1 Object – an abstract en;ty that describes the data that this en;ty has and the ac;ons that this en;ty can perform It can correspond to real-world en;;es, has helped in the evolu;on of good design prac;ces, and is a core aspect of OOP 1.2 Class – abstract object en;;es Someone needs to build a specific object of a class before the object can be used Calling methods – sending messages to an object in order to perform ac;ons in OOP Every specific object cannot perform ac;ons but also has some specific data Every specific object has data that are specific to the object and accompany it throughout the execu;on of a program Classes, from which specific objects can be instan;ated, do not occupy any memory in a program Blueprints Classes do not occupy memory space in a program; a specific object that is instan;ated from the class occupies enough memory to accommodate the object’s data and ac;ons 1.3 Unifies modeling language (UML) – provides a way to visualize the design of any soVware system and has evolved into the standard for modeling object-oriented programs - Defines the number of different types of diagrams, but only the class diagram is within the scope of this book - Not the same as flow charts (UML diagrams) UML ac>vity diagram – considered as a modern extension of flow charts, and as such, UML can be thought of as a superset of flowcharts UML class diagram – the goal of this diagram is to depict the classes within an object-orientated program, as well as their collabora;ons - It illustrates classes as three-;er compartments, rectangles divided into 3 compartments These ac;ons are presented in the returnType: ac>onName(inputType) form. inputType – type of data that is required by the specific ac;on returnType – a type of data that is returned by the specific ac;on aVer it has completed its execu;on - if it’s missing, that depicts an ac;on that does not return any data UML class diagrams depict classes but also display how those classes collaborate Classes within an object-oriented program are connected in various ways, and UML class diagrams represent those connec;ons à they describe the sta;c structure of the system Associa>on – a line connec;ng the two classes Since UML class diagrams can become quite complex as programs increase in size and incorporate more classes, informa;on can be a\ached to these associa;ons to make things easier Each associa;on has 2 roles, and each role has a direc;on The role does not have to be explicitly named à it’s given a name to make things clearer Associa;ons between classes also have mul;plici;es – these are placed at each end of an associa;on line and indicate the number of objects of one class linked to one object of the other class Inheritance - the mechanism that takes advantage of similari;es - Inheritance can be thought of as an is-a or is-is-like rela;onship In UML class diagrams, inheritance is displayed with hollow arrows UML class diagrams can also be created using just the names of the classes that par;cipate in the diagram UML class diagrams are created in this manner to focus on the associa;ons, roles, mul;plici;es, and inheritances that take part between the classes and not on the actual data or ac;ons that each of these classes may contain 1.6 Descrip;on of UML class diagram was used to depict how various objects associated with each other, as well as how some classes could inherit data and ac;ons from other classes that were termed generaliza;ons 4 rela;onships in UML diagrams: associa>on, dependency, aggrega>on, inheritance ASSOCIATION Simplest rela;onship between 2 or more objects One or two role labels may be presented to signify the type of associa;on that takes place between the two objects DEPENDENCY – “USES“ RELATIONSHIP This s. “““ignifies that one object is dependent on one or more objects in order to func;on The implementa;on of an object depends on one or more other objects This occurs when one class links to one or more classes Represented with the dashed arrow à the arrow leaves the object that depends on the object that the arrow points to This can be described as “a client uses a supplier“ AGGREGATION The aggrega;on rela;onship signifies that one object belongs to another object and no other There is a parent and a child object – the child object cannot belong to another parent object The lifecycles of the parent and the child object are independent Example – can be seen in the UML class diagram Aggrega;on is said to be “has“ a rela;onship A hollow diamond followed by a line represents the aggrega;on rela;onship INHERITANCE The inheritance rela;onship (a.k.a generaliza;on) signifies that one object (a.k.a child or subclass) is a specialized form of another object (a.k.a parent or superclass) “is a” rela;onship A hollow triangle followed by a line represents the inheritance rela;onships 1.7 Dependencies between objects are what the name suggests: when one object uses another object, the first object depends on the second since it cannot func;on without it Dependencies are direc;onal, in that an object can depend on another object, but the second object does not necessarily need to depend on the first object as well Ex. If object A depends on object B, it does not necessarily mean that B also depends on A Finding the solu;on using object-oriented programming involves dealing with a number of interdependent objects Dependencies decrease the ability of code reuse, as well as increase maintenance overheads 1.8 1.9 DATA TYPES Data types – allow programs to classify these data into different types – crucial as to the meaning of the data, as well as to how they can be stored and manipulated Common data types: integer numbers, real numbers, Booleans, and strings INTEGER NUMBERS Represents a finite subset of the mathema;cal integers Can include nega;ve values Depending on the hardware and programming language used, every integer is represented by a specific number of bits REAL NUMBERS Represents numbers that contain frac;onal values Computers represent the approxima;on of real numbers with a trade-off between range and precision Two primi;ve data types that can represent floa;ng point numbers: float and double Double has more precision and can represent larger numbers than float BOOLEANS Represent only two possible values, true/false STRINGS Represents a series of characters and is mainly used to display informa;on Ex. String s=2this is a string” – would build a new String object that could be used to display the informa;on between the quotes to the user 1.10 Parameter – the name of the informa;on that is used in a method, func;on or procedure Argument – the value that is passed into a method, func;on, or procedure FEATURES OF OOP 2.1 Encapsula;on – inclusion of both data and ac;ons into a single component Classes contain data and ac;ons that are built.in such a way so that their structure is hidden and can only be accessed outside the class via specific methods 2.2 Inheritance (1.3,1.4,1.6) – refers to that one object (child/subclass) is a specialized form of another object (parent/superclass) 2.3 Polymorphism - means “many“ – the ability to have many forms Int: add(int a, int b) – takes in two integer parameters, a and b, and adds them together and returns the results as an integer Int: add(int a, int b, int c) – takes three integers, adds them together, and returns the result as an integer Float: add(float a float b) – takes in two float numbers and adds them together, and returns the result as a float STATIC POLYMORPHOSIS – the compiler is able to understand the method signatures and decide the method that needs to be invoked for a par;cular method call depending on the number and types of parameters passed DYNAMIC POLYMORPHOSIS – when a subclass overrides ac;ons of the superclass - Allows for subclasses to alter any parent class ac;ons and make them specific to their needs 2.4 Plays the role of a protec;ve wall that prevents data and ac;ons from being accessed from outside a class, resul;ng in unwanted behavior Data and ac;ons are only accessible in specific predefined ways ADVANTAGES OF ENCAPSULATIONS: - Data in a class can be made read or write only - A class restricts the ways that its data and ac;ons can be altered or called - A class can hide the way that data is stored - Easier to maintain, as changes to data and ac;ons in a class can take place without being apparent on the outside, as long as data and ac;ons can s;ll be accessed in the same way - Increase in usability Encapsula;on creates a “black box“ for an object, separa;ng its behavior and implementa;on by restric;ng access to data and ac;ons as necessary Outside the object, only the behavior of the object is apparent, hiding and protec;ng its internal workings 2.5 ADVANTAGES OF INHERITANCE: 2.6 ADVANTAGES OF POLYMORPHOSIS: 2.7 Libraries of objects – a collec;on of classes that have already been wri\en in code and can be used by the programmer while developing a program The availability of libraries simplifies the work that has to be put into a project, since standard, tested, working code is available for various func;ons – used as a “black box“ Allows reuse of code 2.8 DISADVANTAGES OF OOP: - For small projects, with few developers and modules, OOP may increase complexity, development ;me, and maintenance cost - Programming in OOP requires care in order not to make the project too abstract, which would lead to unnecessarily large and complex programs 2.9 Working as a solo developer allows for one person to be in charge of the whole development cycle from design to implementa;on, tes;ng, and maintenance - This approach is reasonable for small-scale projects and in fact does have some advantages, since the developer is aware of every aspect of the project - Allows the developers to have an overall view of the whole project and be able to make changes quickly and without the need to communicate with anyone else These decisions are quicker for a single individual than for a team that has to coordinate Solo developers do not need to develop a common “language“ with his/her peers, since he/she is working alone, and will not face any of the coordina;on or collabora;on problems that may arise in a team 2.10 Modularity – the process of dividing a computer program into separate smaller sub-programs that can be implemented and tested on their own before combining them all together to build the final program When a program increases in size and complexity, designing, implemen;ng, and tes;ng it becomes cumbersome - Modularity in the program development is meant to solve this drawback by separa;ng the program into smaller pieces The division of the program into smaller sub-programs is more effec;ve when there are logic boundaries between the sub-programs, and there are very few dependencies with each other 3.1 Class – in OOP, defines a template through which objects may be created - Provide specific data and ac;ons Iden>fier – a name that iden;fies an en;ty - Generated by alphanumeric sequences, underscore, and shouldn’t begin with a digit Primi>ve – a predefined iden;fier (keyword) that us provided by a programming language as a basic building block - Primi>ve data includes: 1. Characters (char) – primi;ve type that represents a character 2. Integers (int, short, long, byte) – 4 primi;ve data types that iden;fy integers depending on the range of integers that need to be represented 3. Floa;ng point numbers (float, double) – two or more primi;ve types that iden;fy floa;ng-point numbers depending on the range and precision that is required 4. Boolean (bool) – primi;ve type that may take only one of two values, true/false 3.2 Method – ac;ons that are associated with an object - Provide interface an object depicts to the outside world in that through those methods other classes can access and modify its data proper;es Accessor – a special kind of method that is called in order to read a specific data value of an object Mutator – a special kind of method that is called in order to modify a specific data value of an object Constructor - a special kind of method that is called when an object is instan;ated so that it ini;alizes its data with par;cular values - Run only once when an object is created - They use the name of the class and have no return type Signature – iden;fies the method – every method has it - Includes the name of the method, as well as its parameters, their number, and types - Return types are not considered part of the signature Return value – value that is passed back, returned, to the code that called the specific method which returns the value - The value returned aVer the execu;on of the method has taken place 3.3 Private, protected, public – these three terms are strongly related to each other in that they are access modifiers that allow for the implementa;on of encapsula;on - They provide the compiler with informa;on as to which other classes can have access to class data and ac;ons Private – if some data is private, it can be accessed only by the class that defines it Protected – if some data or ac;on is protected, then only the class that defines it and its subclasses can access it Public – if some data or ac;on is public, then any class can access it 3.4 There are mainly 8 primi;ve data types BYTE – minimum value: -128 - Maximum value: 127 - It occupies 8 bits of memory - Typically used for saving memory in large arrays of small numbers SHORT - minimum value: -32.768 - Maximum value: 32,767 - It occupies 16 bits of memory - Typically used for the same reason as a byte but for a wider range of numbers INT - minimum value: -231 - Maximum value: 231-1 - It occupies 32 bits of memory - Most commonly used primi;ve data type to represent integer numbers LONG - minimum value: -263 - Maximum value: 263-1 - It occupies 64 bits of memory - Primi;ve data type used to represent integer numbers when the range of the int data type is not sufficient FLOAT – minimum and maximum values are beyond the scope of this book - Ranges from 1.4-45 to 3.438 - Occupies 32 bits of memory - Typically used for saving memory in large arrays of floa;ng point numbers - Should not be used as a data type for numbers that need precision DOUBLE Ranges from 4.9-324 to 1.7308 It occupies 64 bits of memory Typically used for the representa;on of decimal values Should not be used as a data type for precise values such as currency CHAR Minimum value: ‘\u000’ – 0 Maximum value: ‘\ufff’ – 65,353 Occupies 16 bits of memory Represents a Unicode character BOOLEAN Can take only one of two values: true or false Typically used for condi;ons that may have one of two outcomes STRING It is not a primi;ve data type but rather a reference class Typically used to represent a series of characters (ex. A word or sentence) Once it is created, its value cannot change, and it is thus immutable 3.5 – 3.8 – code examples 3.9 Modern programming languages enable interna>onaliza>on in a variety of ways: - Support for the display of interna;onal fonts and text - Support for language and local specific needs, such as date and ;me formalng - Support for different keyboard layouts, as well as complex characters and symbols - Support for variety of wri\en languages with the use of Unicode, allowing the representa;on and handling of text in most languages - Support for user language auto-detec;on and tailoring of the user experience according to it - Support for global development of soVware that includes localized content 3.10

Use Quizgecko on...
Browser
Browser