OOP Introduction and UML Class Diagram
2026-01-28 19:48
Tags: #java
Laboratory Activity - 1
Object-Oriented Programming
Object- Oriented programming is a programming paradigm that organizes a program in to objects. which represent real-world entities. Each object is create from a class and contains data (attributes) and behavior(methods). OOP focuses on modeling real-life systems by combining data and operations into a single unit, making programs easier to design , understand , and maintain.
物件導向程式設計(Object-Oriented Programming, OOP)是一種程式設計範式,主要使用「物件」的概念來組織和簡化程式碼。這種方法強調資料與操作資料的方法(函數)的結合,使得程式更易於理解、維護和擴展。
Characteristics of Object Oriented Programming
Encapsulation:
1 2 | |
Abstraction:
1 2 3 | |
Inheritance:
1 2 3 | |
Polymorphism:
1 2 3 | |
Modularity:
1 2 3 | |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Advantages of OOP over Procedural Programming
| Aspec | Procedural Programming | Object-Oriented Programming |
|---|---|---|
| 1. Program Structure | Based on functions as and procedures | Based on objects and classes |
| 2. Data Handling | Data is often global and exposed | Data is encapsulated and protected |
| 3. Reusability | Limited reuse | High reuse through inheritance |
| 4. Scalability | Difficult for large systems | Suitable for large and complex systems |
| 5. Maintenance | Harder to update and debug | Easier to maintain and extend |
| 6. Real-world Modeling | Less intuitive | Closely models real-world systems. |
Benefits of OOP in Program Design
- Improved program organization
- through modular class structures
- Easier maintenance and debugging
- due to well-defined responsibilities
- Code reusability
- reducing development time and effort
- Scalability
- making it easier to add new features
- Better collaboration
- among developers working on large systems
- Clear visual representation
- using UML class diagrams during system planning
What is an Class
A class is a blueprint or template used in Object-Oriented Programming(OOP) to define the properties(attributes) and behaviors (methods) that objects will have .
It does not represent a real object by itself, but is describes what an object is and what it can do .
In simple terms, a class defines: - What data an object holds.(attributes) - What actions an object can perform(methods)
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
What is an Object ?
An object is a real , usable entity created from a class. It represents something that exists in a program and has its own data(attributes) ab behavior (methods)
Class and Instance Data Values(Attributes)
Class and Instance Data Values(attributes)
- Attributes are variables that store data about an object , they represent the state of an object .
- Example: class Student has attribute studentID
Code Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
UML Class Diagram
UML = Unified Modeling Language A UML class diagram is a visual representation of classes in an object-oriented system. It shows: - Classes - Attributes - Methods - Relationships between classes
Structure of a UML Class Diagram A class is drawn as a rectangle with three sections:
| Class Name | | ---------- | | Atrributes | | Methods | Visibility of Attributes and Methods in the Class
| symbol | means | example | explain | for |
|---|---|---|---|---|
| + | public | + name:String | public attributes or methods | any class can be accessed |
| - | private | - passportCode:String | private attributes or methods | can't be accessed by any other class or subclass |
| # | protected | # phone_num:String | protected attributes or methods | can only be accessed by the same class or its subclasses |
| ~ | package/default | ~eat() | can be used by any other class as long it's in the same package | |
Parameter Directionality
| direction | means | example | explain |
|---|---|---|---|
| in | need get parameters form user or other class | + method(in p1: String):void | |
| inout | parameters and return values from self | + method(inout p1:String):String | |
| out | no need parameters but return values from self | # method(out p1: String):String |
References:
Module 1 - Introduction to Object Oriented Programming PPT
UML class diagrams - https://www.youtube.com/watch?v=6XrL5jXmTwM
UML Class Diagram Tutorial - https://www.lucidchart.com/pages/uml-class-diagram
Online shopping cart UML class diagram example - https://lucid.co/templates/online-shopping-cart-uml-class-diagram-example