ASSIGNMENT 1 FRONT SHEET
Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 20: Advanced Programming
Submission date Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Nguyen Hoang Duy Student ID GCD201858
Class GCD1001 Assessor name Pham Thanh Son
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that
making a false declaration is a form of malpractice.
Student’s signature
Grading grid
P1 P2 M1 M2 D1 D2
Summative Feedback: Resubmission Feedback:
Grade: Assessor Signature: Date:
Lecturer Signature:
Table of Contents
LO1 Examine the key components related to the object-orientated programming paradigm, analysing design pattern types ..................... 6
INTRODUCTION ................................................................................................................................................................................................. 6
P1 Examine the characteristics of the object-orientated paradigm as well as the various class relationships. ............................................... 6
I. Introduce about OOP(Object-oriented programming ): ....................................................................................................................... 6
II. Class and Object: ................................................................................................................................................................................... 8
P2 Characteristics OOP ...................................................................................................................................................................................... 9
I Abstraction .................................................................................................................................................................................................. 9
II Inheritance ............................................................................................................................................................................................... 16
III Polymorphism.......................................................................................................................................................................................... 22
IV Encapsulation .......................................................................................................................................................................................... 31
P3 RELATIONSHIPS BETWEEN CLASSES ........................................................................................................................................................... 37
I. Binary Association ............................................................................................................................................................................... 37
II Composition ............................................................................................................................................................................................. 40
III Aggregation ............................................................................................................................................................................................. 43
IV Unary association .................................................................................................................................................................................... 46
LO2 Design a series of UML class diagrams..................................................................................................................................................... 48
P4 Design and build class diagrams using a UML tool..................................................................................................................................... 48
I. UML Class Diagram: ............................................................................................................................................................................. 48
II. Explain UML Class Diagram: ................................................................................................................................................................ 49
References ....................................................................................................................................................................................................... 50
Table Of Figures
Figure 1: Object-Oriented Programming........................................................................................................................................................... 7
Figure 2: Interface ........................................................................................................................................................................................... 10
Figure 3: Diagram Interface ............................................................................................................................................................................. 11
Figure 4: Snippet Code of Interface................................................................................................................................................................. 11
Figure 5: Snippet Code of Interface................................................................................................................................................................. 12
Figure 6: Snippet Code of Interface................................................................................................................................................................. 12
Figure 7:Snippet Code of Interface ................................................................................................................................................................. 12
Figure 8: Snippet Code of Interface................................................................................................................................................................. 13
Figure 9: Result ................................................................................................................................................................................................ 13
Figure 10: Diagram of Abstract Class. ............................................................................................................................................................. 14
Figure 11: Snippet Code of Abstract Class....................................................................................................................................................... 15
Figure 12: Snippet Code of Abstract Class....................................................................................................................................................... 15
Figure 13: Snippet Code of Abstract Class....................................................................................................................................................... 16
Figure 14: Result .............................................................................................................................................................................................. 16
Figure 15: Diagram of Inheritance................................................................................................................................................................... 18
Figure 16:Snippet Code of Inheritance........................................................................................................................................................... 19
Figure 17: Snippet Code of Inheritance. .......................................................................................................................................................... 20
Figure 18: Snippet Code of Inheritance. .......................................................................................................................................................... 20
Figure 19:Snippet Code of Inheritance............................................................................................................................................................ 21
Figure 20: Snippet Code of Inheritance. .......................................................................................................................................................... 21
Figure 21: Polymorphism in OOP. ................................................................................................................................................................... 23
Figure 22: Diagram of Override. ...................................................................................................................................................................... 24
Figure 23: Snippet Code of Override. .............................................................................................................................................................. 25
Figure 24: Snippet Code of Override. ............................................................................................................................................................. 26
Figure 25: Snippet Code of Override. .............................................................................................................................................................. 27
Figure 26: Result .............................................................................................................................................................................................. 27
Figure 27:Diagram of Overloading. ................................................................................................................................................................ 28
Figure 28:Snippet Code of Overloading. ......................................................................................................................................................... 30
Figure 29: Snippet Code of Overloading. ........................................................................................................................................................ 30
Figure 30: Result .............................................................................................................................................................................................. 31
Figure 31: Diagram Encapsulation................................................................................................................................................................... 33
Figure 32:Snippet Code and Explain. ............................................................................................................................................................... 34
Figure 33: Snippet Code and Explain. .............................................................................................................................................................. 35
Figure 34:Snippet Code and Explain. ............................................................................................................................................................... 35
Figure 35: Snippet Code and Explain. .............................................................................................................................................................. 36
Figure 36: Result .............................................................................................................................................................................................. 36
Figure 37 .......................................................................................................................................................................................................... 37
Figure 38 .......................................................................................................................................................................................................... 38
Figure 39 .......................................................................................................................................................................................................... 38
Figure 40 .......................................................................................................................................................................................................... 39
Figure 41 .......................................................................................................................................................................................................... 39
Figure 42 .......................................................................................................................................................................................................... 40
Figure 43 .......................................................................................................................................................................................................... 41
Figure 44 .......................................................................................................................................................................................................... 41
Figure 45 .......................................................................................................................................................................................................... 42
Figure 46 .......................................................................................................................................................................................................... 42
Figure 47 .......................................................................................................................................................................................................... 43
Figure 48 .......................................................................................................................................................................................................... 44
Figure 49 .......................................................................................................................................................................................................... 44
Figure 50 .......................................................................................................................................................................................................... 45
Figure 51 .......................................................................................................................................................................................................... 45
Figure 52 .......................................................................................................................................................................................................... 45
Figure 53 .......................................................................................................................................................................................................... 46
Figure 54 .......................................................................................................................................................................................................... 46
Figure 55 .......................................................................................................................................................................................................... 47
Figure 56 .......................................................................................................................................................................................................... 47
Figure 57 .......................................................................................................................................................................................................... 47
LO1 Examine the key components related to the object-orientated programming paradigm,
analysing design pattern types
INTRODUCTION
In Danang, HoangDuy store is a famous convenience store with branches everywhere in the city and their customers abound.
To achieve the success today, the CEO of Wakanda has made the right bold and bold decision to invest in his store
management system. CEO HoangDuy store, who was knowledgeable about technology, recognized the problem that
technology is increasingly popular in life from economy, education, health, ... He found that his previous store management
style was outdated, and management by books and paperwork had many negative issues such as failure and loss of store
sales. In addition, this management is limited because if the quantity of goods is large, it will be difficult to manage and if you
want to manage it effectively, you need to hire many people, but this will lead to a lot of costs. Ultimately, if you keep the old
method, it is very difficult to expand the scale of the everywhere in Da Nang.
• To solve this problem, CEO HoangDuy came to me and the solution that I give is to build a store management system. This
system only needs 1 person to manage many stores, in addition, CEO in the office can still see the situation of his store
without going to the store. The advantages of the system include: managing many goods, saving labor costs, managing
effectively, not causing loss. And thanks to this system, the HoangDuy store has grown very quickly. Here I will introduce
some functions of the system such as being able to add multiple store products to the system such as id, name, production
date and expiry date of the product, in addition to the product price and Total quantity of products in stock. Goods may also
be made in different categories. Once added, we can also view all information of the store through the ShowProductDetail
function and they will be displayed in a list form. In this program we use C # to solve requests System
P1 Examine the characteristics of the object-orientated paradigm as well as the various class
relationships.
I. Introduce about OOP(Object-oriented programming ):
1. Definition:
• Object-oriented programming (OOP) is the core ingredient of the .NET framework. OOP is so important that, before
embarking on the road to .NET, you must understand its basic principles and terminology to write even a simple
program. The fundamental idea behind OOP is to combine into a single unit both data and the methods that operate on
that data; such units are called an object. All OOP languages provide mechanisms that help you implement the object-
oriented model. They are encapsulation, inheritance, polymorphism and reusability. Let's now take a brief look at these
concepts.
• Everything in OOP is placed together as self-sustainable “objects.” An object is a combination of variables, functions,
and data that performs a set of related activities. When the object performs those activities, it defines the object’s
behavior. In addition, an object is an instance of a class.
• Furthermore, C# offers full support for OOP including inheritance, encapsulation, abstraction, and polymorphism:
- Encapsulation is when a group of related methods, properties, and other members are treated as a single object.
- Inheritance is the ability to receive (“inherit”) methods and properties from an existing class.
- Polymorphism is when each class implements the same methods in varying ways, but you can still have several classes
that can be utilized interchangeably.
- Abstraction is the process by which a developer hides everything other than the relevant data about an object in order
to simplify and increase efficiency.
Figure 1: Object-Oriented Programming
2. Why use OOP:
• The Object-Oriented Programming (OOPs) in C# is a design approach where we think in terms of real-world objects
rather than functions or methods. Unlike procedural programming language, here in oops, programs are organized around
objects and data rather than action and logic. Please have a look at the following diagram to understand this better.
• Reusability: - To address reusability, object-oriented programming provides something called Classes and Objects. So,
rather than copy-pasting the same code again and again in different places what you can do here is, create a class and
make an instance of the class which is called object and reuses them whenever you want.
• Extensibility: - Suppose you have a function and you want to extend it with some new features that were not possible
with functional programming. You have to create a completely new function and then change the whole function
whatever you want. In OOPs, this problem is addressed by using some concepts called Inheritance, Aggregation, and
Composition. In our upcoming article, we will discuss all these concepts in detail.
• Simplicity: - Because we don’t have extensibility and reusability in functional programming, we end up with lots of
functions and lots of scattered code. In OOPs, this problem is addressed by using some concepts called Abstraction,
Encapsulation, and Polymorphism.
• Maintainability: - As OOPs address Reusability, Extensibility, and Simplicity, we have good maintainable code and clean
code which increases the maintainability of the application.
II. Class and Object:
1. Definition of Class and Object:
a. Definition class:
• A class is like a blueprint of a specific object. In the real world, every object has some color, shape, and
functionalities - for example, the luxury car Ferrari. Ferrari is an object of the luxury car type. The luxury car is a
class that indicates some characteristics like speed, color, shape, interior, etc. So any company that makes a car
that meets those requirements is an object of the luxury car type. For example, every single car of BMW,
Lamborghini, Cadillac are an object of the class called 'Luxury Car'. Here, 'Luxury Car' is a class, and every single
physical car is an object of the luxury car class.
• Likewise, in object-oriented programming, a class defines some properties, fields, events, methods, etc. A class
defines the kinds of data and the functionality their objects will have.
• A class enables you to create your custom types by grouping variables of other types, methods, and events.
b. Definition Object:
• Objects are basic building blocks of a C# OOP program. An object is a combination of data and methods. The data and
the methods are called members of an object. In an OOP program, we create objects. These objects communicate
together through methods. Each object can receive messages, send messages and process data.
• There are two steps in creating an object. First, we define a class. A class is a template for an object. It is a blueprint
which describes the state and behavior that the objects of the class all share. A class can be used to create many objects.
Objects created at runtime from a class are called instances of that particular class.
• A class will not occupy any memory space. Hence to work with the data represented by the class you must create a
variable for the class, that is called an object.
• When an object is created using the new operator, memory is allocated for the class in the heap, the object is called
an instance and its starting address will be stored in the object in stack memory.
• When an object is created without the new operator, memory will not be allocated in the heap, in other words an
instance will not be created and the object in the stack contains the value null.
P2 Characteristics OOP
I Abstraction
1. Abstraction in OOP:
a. Definition Abstraction:
• Data Abstraction is the property by virtue of which only the essential details are exhibited to the user. The trivial or the
non-essentials units aren’t exhibited to the user.
• Data Abstraction may also be defined as the process of identifying only the required characteristics of an object
ignoring the irrelevant details. The properties and behaviors of an object differentiate it from other objects of similar
type and also help in classifying/grouping the objects.
• Abstraction can be achieved using abstract classes in C#. C# allows you to create abstract classes that are used to
provide a partial class implementation of an interface. Implementation is completed when a derived class inherits from
it. Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have
more specialized functionality.
• Example:
- I will take a practical example of abstraction so that I can visualize it. For a car, when looking from the outside you will
see the wheel, the color of the car, the steering wheel, the windshield, ... When sitting in a car you can know it is moving,
Air-cooled operation. However, you will not see or know how the car works, how it can be moved, how the air
conditioner can be cool, etc. People simply know that it can run but it cannot. know how it works and this is abstraction.
2. Definition and Characteristics of Interface:
a. Definition Interface:
• An interface contains definitions for a group of related functionalities that a nonabstract class or a struct must
implement. An interface may define static methods, which must have an implementation. Beginning with C# 8.0, an
interface may define a default implementation for members. An interface may not declare instance data such as
fields, auto-implemented properties, or property-like events.
• We already know that an object contains methods that can be set as private or public. The object's interface
consists of its public methods, which is the way we communicate with certain types of objects. We have already dealt
with public methods in previous lessons, e.g. the ones we set up for our arena warrior.
• Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only
the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in
providing a standard structure that the deriving classes would follow.
• Abstract classes to some extent serve the same purpose, however, they are mostly used when only few methods
are to be declared by the base class and the deriving class implements the functionalities.
Figure 2: Interface
b. Features of an interface:
• Interface has members that are methods, properties, indexers, events and do not contain fields.
• Interface can implement many base interfaces (base interface), a class or struct can implement multiple
interfaces.
• Any class or struct that implements an interface implements all the members and fully defines the members of
that interface.
• The members of the interface are not allowed to be defined, but only declared (declaration).
• Interface has no constructors.
• If a class implements from multiple interfaces with the same member name, the class must specify which
interface member belongs to (explicit interface).
c. Notes on Interfaces:
• Like abstract classes, interfaces cannot be used to create objects (in the example above, it is not possible to
create an "IAnimal" object in the Program class)
• Interface methods do not have a body - the body is provided by the "implement" class
• On implementation of an interface, you must override all of its methods
• Interfaces can contain properties and methods, but not fields/variables
• Interface members are by default abstract and public
• An interface cannot contain a constructor (as it cannot be used to create objects)
d. Diagram Interface:
Figure 3: Diagram Interface
• In the picture above we can see that the IAnimal interface has 2 methods: Run and Eat,
in which both methods have access modifier public. In addition, Dog is a class that
inherits the IAnimal interface
e. Snippet Code of Interface and Explain:
Below is the code about the interface. First, I create an interface called IAnimal. Then declare the methods of
Interface IAnimal. include: Run and Eat. All the methods here have no function body
Figure 4: Snippet Code of Interface
• Next, the Dog class implements the IAnimal interface. After creating the Dog class, we will define the methods on the interface.
Figure 5: Snippet Code of Interface
• Main method content.
Figure 6: Snippet Code of Interface
• First, declare the dog_1 object of class Dog.
Figure 7:Snippet Code of Interface
• Call the Run() and Eat() methods in the Dog class. And in this method we can print the
results to the screen.
Figure 8: Snippet Code of Interface
• Here are the results after executing the program. After calling the Run method, the
result will return "dog run very fast" and the Eat method returns "Dog can eat rice".
Figure 9: Result
3. Definition and Characteristics of Abstract Class:
a. Definition of Abstract Class:
• When designing applications, it is important to know when to use an abstract class and when to use an interface.
Although abstract classes and interfaces seem similar in some ways, there are key differences that will determine
which is the best choice for what you’re trying to accomplish.
• The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An
interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract
class, it can take advantage of multiple interfaces.
• An abstract class is an incomplete class or special class we can't be instantiated. The purpose of an abstract class is to
provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit
an abstract class.
• We can use an abstract class as a base class and all derived classes must implement abstract definitions. An abstract
method must be implemented in all non-abstract classes using the override keyword. After overriding the abstract
method is in the non- Abstract class. We can derive this class in another class and again we can override the same
abstract method with it.
b. Abstract Class Features:
• An abstract class can inherit from a class and one or more interfaces.
• An abstract class can implement code with non-Abstract methods.
• An Abstract class can have modifier for methods, properties etc.
• An Abstract class can have constants and fields.
• An abstract class can implement a property.
• An abstract class can have constructors or destructors.
• An abstract class cannot be inherited from by structures.
• An abstract class cannot support multiple inheritance.
c. Diagram of Abstract Class:
Figure 10: Diagram of Abstract Class.
In the diagram above we can see that the abstract class Person has 3 methods: ShowName, ShowJob and ShowAddress.
All of these methods have the access modifier public. Besides, the Father class is the inheritance class of Person.
d. Snippet Code of Abstract Class and Explain:
First create an abstract class named Person. In class Person there are 3 methods ShowName, ShowJob and
ShowAddress. All three methods are default and return type is abstract void.
Figure 11: Snippet Code of Abstract Class.
Next, create a class called Father and inherit from class Person and have access modifiers. In Father class we define
methods in Person class. To define methods use the Console. WriteLine command.
Figure 12: Snippet Code of Abstract Class.
Finally, in the main method I create an object named father and then execute the command to call methods on the
Father class.
Figure 13: Snippet Code of Abstract Class.
Here are the results after the program runs.
Figure 14: Result
II Inheritance
1. Definition of Inheritance:
• One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of
another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code
functionality and speeds up implementation time.
• When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the
new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as
the derived class.
• The child classes inherit methods and properties of the parent class, but at the same time, they can also modify the behavior of the
methods if required. The child class can also define methods of its own if required.
2. Important terminology:
• Super Class: The class whose features are inherited is known as super class (or a base class or a parent class).
• Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child
class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
• Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that we want, we can derive our new class from the
existing class. By doing this, we are reusing the fields and methods of the existing class.
3. Important facts about inheritance in C#
• Default Superclass: Except Object class, which has no superclass, every class has one and only one direct
superclass(single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass
of Object class.
• Superclass can only be one: A superclass can have any number of subclasses. But a subclass can have only one
superclass. This is because C# does not support multiple inheritance with classes. Although with interfaces,
multiple inheritance is supported by C#.
• Inheriting Constructors: A subclass inherits all the members (fields, methods) from its superclass. Constructors
are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked
from the subclass.
• Private member inheritance: A subclass does not inherit the private members of its parent class. However, if
the superclass has properties(get and set methods) for accessing its private fields, then a subclass can inherit.
4. Diagram of Inheritance:
Figure 15: Diagram of Inheritance.
-In the picture above we see a class named Person, in addition, in this class there are fields Name (datatype string),
Address (datatype string), Sport (datatype string). Then there are Class, Class and ShowName constructors. where the
first-Class constructor is empty and the second-Class constructor has the parameters Name (datatype string), Sport
(datatype string).
-Next, the Sister class has the relationship that is inheritance of class Person. In the Sister class, there is a field of Score
(datatype double) and the Sister constructor has the parameters name (datatype string), sport (datatype string), and
score (datatype double).
5. Snippet Code of Inheritance and Explain:
Below is the code for the Inheritance of OOP property.
Figure 16:Snippet Code of Inheritance.
First I create a class named Person and create three fields including Name (datatype string), Address (datatype string), Sport
(datatype string).
Figure 17: Snippet Code of Inheritance.
-Next, create a class named Sister and inherit the class Person. In the Sister class, it creates a public field, Score, with
the data type double. In addition, create two constructors named Sister, but a default constructor and a constructor
contain the parameters name, sport, score and this constructor will inherit from class Person
Figure 18: Snippet Code of Inheritance.
-In the main function I will create an object named sister_1. Through the Sister constructor. I'll then assign values to
the Sister fields. After assigning, it will print the result to the screen.
Figure 19:Snippet Code of Inheritance.
-Below are the results after the program is running. After assigning the value Soccer to the Sport field and printing out Soccer is his
favorite sport, similar Thien is assigned to the Name field and printed on the screen “His name is Thien.” . 6.5 is assigned to the Score
field and prints His total first. semester grade is 6.5
Figure 20: Snippet Code of Inheritance.
III Polymorphism
1. Definition
• Polymorphism forms one of the fundamental concepts of object-oriented programming, along with
encapsulation and inheritance.
• In C#, polymorphism is implemented through inheritance and the use of the keyword "virtual". Derived classes inherit the
base class members, except constructors, based on their accessibility levels. Hence, the compiler generates the code to
check and identify the correct object type (that is pointed to by the reference type) at runtime and the appropriate method
to be called.
• An example of polymorphism is an employee base class, which includes all the basic details about employees.
Classes such as clerk and manager could inherit from the employee base class with specific implementations
(by overriding virtual methods) wherever necessary in the derived.
Figure 21: Polymorphism in OOP.
Polymorphism can be achieved using many techniques including:
• Method overloading (defining several methods at the same time)
• Method overriding (this allows a subclass to override a specific implementation of a method already issued
by one of its super-classes)
• Operator overloading (some or all of the operators are handled has polymorphic functions with different
behaviors depending on the types of its arguments)
2. Definition and Characteristics of Override:
• In c#, Method Overriding means override a base class method in the derived class by creating a method with
the same name and signatures to perform a different task. The Method Overriding in c# can be achieved by
using override & virtual keywords along with the inheritance principle.
• Suppose, if we want to change the behavior of the base class method in a derived class, then we need to use
method overriding. The base class method which we want to override in the derived class that needs to be
defined with virtual keyword and we need to use override keyword in derived class while defining the method
with the same name and parameters then only we can override the base class method in a derived class.
Diagram of Override:
Figure 22: Diagram of Override.
• In the diagram above we can see that the BuyProduct class is the parent class, in this class there are 2 fields Amount
(datatype int) and _Cost (datatype int), in addition the Amount field is public and _Cost is private. In the BuyProductc class the
constructors are BuyProduct, BuyProduct, and Total. The first BuyProduct is the empty constructor and the second
BuyProduct is the constructor with the parameters a (datatype int) and c (datatype int). Total has a double data type.
• The Charge class inherits the BuyProduct class. In addition, there is a field called Tax (datatype int). In this
class, the constructor is empty Charge and Charge has the parameters a (datatype int), c (datatype int) and t
(datatype int). Finally, the Total (datatype double) method is the override method.
Snippet Code of Override:
• First create a class named BuyProduct and create fields named Amount (datatype int) and _Cost (datatype int),
but the Amount field has access modify is public and _Cost is private. In addition, create two constructors, Buy
Product. The first constructor has no parameters, and the second constructor has parameters a and c, both of
which are of data type int. In the second constructor the fields assigned to the parameters are Amount = a and
_Cost = c. Finally, the BuyProduct class creates a Total method, which stores the sum of the products.
Figure 23: Snippet Code of Override.
Next, will create a class named Charge and this class will be a subclass of class BuyProduct. In the Charge
class I will add a field named Tax of data type int. Create 2 constructors named Charge, the first constructor
will be the default constructor. The second Charge generator will pass the parameters a, c, and t. In which
the parameters a and c inherit from the class BuyProduct. And next we go to assign the value of the field to
the parameter t. Finally use the Total () override method. The Total method of Charge class will take the Base
Total () of the BuyProduct class and multiply by t and divide by 100, the purpose of this is to calculate the
tax of the product
Figure 24: Snippet Code of Override.
In the main method. First I will create a product_1 object of BuyProduct and a charge_1 object of Charge. Both statues
are assigned values. In product_1 gasn2 the values are Amount and _Cost, the purpose is to charge 3 items for each
product costing $ 2000. In assigned charge_1 (3,200,10) for tax purposes is 10% of the above products. Finally print out
the job.
Figure 25: Snippet Code of Override.
Below are the results after printing to the screen.
Figure 26: Result
3. Definition and Characteristics of Overloading:
Definition of Overloading:
• It is a process of creating multiple methods in a class with the same name but with a different signature. In C#,
It is also possible to overload the methods in the derived classes, it means, it allows us to create a method in
the derived class with the same name as the method name defined in the base class.
• In simple words, we can say that the Function Overloading in C# allows a class to have multiple methods with
the same name but with a different signature. So in C# functions or methods can be overloaded based on the
number, type (int, float, etc), order and kind (Value, Ref or Out) of parameters.
Method overloading can be achieved by the following:
• By changing number of parameters in a method
• By changing the order of parameters in a method
• By using different data types for parameters
Diagram of Overloading:
Figure 27:Diagram of Overloading.
In this diagram the class is named Person. In this class there are 2 constructors named ShowInfor, to express
overloading, the first ShowInfor constructor has 2 parameters: name (datatype string), age (datatype age). The second
constructor has 3 parameters: name (datatype string), age (datatype age) and address (datatype string). Snippet Code
of Overloading:
Snippet Code of Overloading and Explain:
• Below is the code about Overloading. First, create a class named Person and have access modifier public. Next,
create the ShowInfor method, where the ShowInfor method will be defined twice. First method will be passed
2 parameters are name (datatype string) and age (datatype int). The method next will be taken in 3 parameters,
name (datatype string), age (datatype int) and address (datatype string). Method ShowInfor is called
overloaded because there are two implementations in class scope and same name. If the two ShowInfor
methods are declared in different ranges (the example is declared in different classes) then the ShowInfor
method is not called overload.
Figure 28:Snippet Code of Overloading.
• At the main function, create an object named man_1 of class Person. Use the person_1. ShowInfor method to call the
ShowInfor method from the Person class. Here I will call 2 times, the first time pass that object 2 parameters " Nguyen Hoang
Duy" and "20". The second time to pass 3 parameters to the object man_1 is "Nguyen Van A", 20, "Quang Nam". Based on
the number of parameters: for example, one method has 2 parameters, another has 3, ... This way, the compiler will call the
correct method with the exact number of parameters passed.
Figure 29: Snippet Code of Overloading.
• Results after the program runs. With method overloading, the program passes the correct parameters to the
methods. When passing 2 parameters "Vo Thanh thien", 20, the program will automatically understand as
calling the first ShowInfor method. And vice versa, when passing 3 parameters, the program will automatically
understand and call the second ShowInfor method.
Figure 30: Result
IV Encapsulation
1. Definition of encapsulation:
• The process of binding the data and functions together into a single unit (i.e. class) is called encapsulation in
C#. Or you can say that the process of defining a class by hiding its internal data member direct access from
outside the class and providing its access only through publicly exposed methods (setter and getter methods)
or properties with proper validations and authentications is called encapsulation.
• Encapsulation protects data from unwanted access or altered. it is the mechanism where the abstraction is
implemented. It solves the problem at the implementation level.
• The encapsulated class won’t allow us to access the fields directly instead, we need to use the getter and setter
to access functions to read and write data based on requirements.
2. Definition and Characteristics of Access modifier:
Access modifiers or Access Specifiers are used to define the visibility of a class property or method. There are
times when you may not want other programs to see the properties or the methods of a class. In such cases, C#
gives the ability to put modifiers on class properties and methods. The class modifiers can restrict access so that
other programs cannot see the properties or methods of a class.
The available access modifiers:
• The public keyword gives a type or type member public access, the most permissive access level. There are no
restrictions on accessing public members.
• The protected keyword gives a type or type member protected access, which means it's accessible from within
the class in which it is declared, and from within any class derived from the class that declared this member. As
discussed in the next chapter, a protected member of a base class is accessible in a derived class only if the
access takes place through the derived class type.
• The internal keyword gives a type or type member internal access, which is accessible only within files in the
same assembly. It is an error to reference a member with internal access outside the assembly within which it
was defined. The C++ analog is friend.
• The private keyword gives a type or type member private access, which is the least permissive access level.
Private members are accessible only within the body of the class or the struct in which they are declared. It is a
compile-time error to reference a private member outside the class or the struct in which it is declared.
• Protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or
from within a derived class in another assembly.
3. Diagram Encapsulation:
Figure 31: Diagram Encapsulation.
• In class Person there will be fields as id (datatype string), _name (datatype string), email (datatype string). In the
above 3 fields there are different types of access, modifier: id will be public, _name will be private and email is
protected. There are also ID, Name, Email constructors and they all have string data types. Get, Set is the
expression for those constructors.
• After creating class Person, I create a class Student that inherits class Person.
4. Snippet Code and Explain:
• Create a class named Person. Next, I will create the fields _id, _name, email and for the properties of the
Encapsulation the access modifier of name will be private , email will be protected and id is public , for the
encapsulation of variables or data of a class hidden from any other class. and can only be accessed through any
member function of the private class to which they are declared.
Figure 32:Snippet Code and Explain.
• So to get and set the explicit values, create properties that have access modifier is public or in other words these
properties are accessors to get and set values for the file. In the code below I have created 2 field Name and
Email and constructor Person.
Figure 33: Snippet Code and Explain.
• After creating Person class, I will create Student class and Student will inherit Person. In the Student class there will be a
constructor named Student and it will inherit the properties of Person class, but the Student constructor also implements
Person's email field. And through here proves protected in OOP
Figure 34:Snippet Code and Explain.
• In function main, create student object of class Student. Then use the command to access properties and
indirectly for the field. For example: student.ID = "GCD201858"; Used to access ID and property "34345" for
filed id and then print the output to the screen as ID =GCD201858.
Figure 35: Snippet Code and Explain.
• Below are the results from the program. ID assigned isGCD201858, assigned name is Nguyen Hoang Duy and
email assigned is duynhgcd201858@fpt,edu,vn
Figure 36: Result
P3 RELATIONSHIPS BETWEEN CLASSES
I. Binary Association
Association is a relationship among the objects. Association is "*a*" relationship among objects. In Association, the relationship
among the objects determines what an object instance can cause another to perform an action on its behalf. We can also say that an
association defines the multiplicity among the objects. We can define a one-to-one, one-to-many, many-to-one and many-to-many
relationship among objects. Association is a more general term to define a relationship among objects. Association means that an
object "uses" another object.
For example, suppose we have two classes such as Employees and Mother then these two classes are said to be “has-a” relationship
if both entities share each other’s object for some work and at the same time they can exists without each other’s dependency,
orboth have their own lifetime.
Figure 37
Figure 38
Figure 39
Figure 40
Figure 41
II Composition
Composition is special type of Aggregation. It is a strong type of Aggregation. In this type of Aggregation the child object does not
have their own life cycle. The child object's life depends on the parent's life cycle. Only the parent object has an independent life
cycle. If we delete the parent object then the child object(s) will also be deleted. We can define the Composition as a "Part of"
relationship.
❖ For example, Between Aparment building and rooms. Building can contain multiple rooms. Any room cannot belong to two
different building if we delete the building room will automatically delete. Each class referenced is part-of the aggregate class.
Figure 42
Figure 43
Figure 44
Figure 45
Figure 46
III Aggregation
Aggregation is a special type of Association. Aggregation is "*the*" relationship among objects. We can say it is a direct association
among the objects. In Aggregation, the direction specifies which object contains the other object. There are mutual dependencies
among objects.
Figure 47
Figure 48
Figure 49
Figure 50
Figure 51
Figure 52
IV Unary association
Unary Association: A knows about B, but B knows nothing about A
Figure 53
Example:
Figure 54
Figure 55
Figure 56
Figure 57
LO2 Design a series of UML class diagrams
P4 Design and build class diagrams using a UML tool.
I. UML Class Diagram:
II. Explain UML Class Diagram:
• Above is my entire Uml Class diagram, with a total of 5 classes, 2 interfaces are created.
• First create an abstract class named Product, in Product create fields like id (int), name (string), dateofmanufacture (date
time), expirydate (date time), price (double) and quantity () all These fields are private. In addition, in Product also has a
constructor Product () and this is the default constructor. Then create a method called DisplayInfor () and it has the data type
string and it is the function of displaying program input.
• Then I create a class named ProductDetail and this class will inherit the Product class, but in this class there will be a
variable called _type with a data type of string, it means the product type. In addition we also create a default constructor
named ProductDetail and the DisplayInfor () method inherits from the Product class, however it overrides the Product class.
• Create an interfaces named IIterator, in this interface there will be methods such as CurrentIterm () with the return type of
Product class, the First () method with the return type void, Isdone () with the return type of bool and the end. with Next ()
the return type is Product class. IIterator is an interface that defines the operations for accessing collection elements in a
sequence.
• An interface named Ilist has a method createIterator () whose return type is class IIterator. This interface has the function
of defining an operation to create an iterator, and the operation here is createIterator ().
• Class ListProduct will inherit the Ilist interface and it will implement the createIterator () function of Ilist. In addition, in this
class there is a field called lisproduct with the return type of Product class. Create a constructor named ListProduct () and this
is a default constructor. We create a method called addProduct () with the return type void. The ListProduct class also has an
aggregation relationship with the Product class. It is the responsibility of each ListProduct to instantiate a ProductIterator
that iterates over its set of objects. The iterator interface provides a set of methods for traversing or modifying sets
• Class ProductIterator will be responsible for implementing all the functions of class IIterator. In addition, it also has a field
named List and position with data type private, besides creating a constructor ProductIterator.
• Finally, create a class called FunctionApp, it has an association with the ListProduct class. In this class, we will create 2 filed
instance and listproductdetail both with private access modifier. In addition, there are 3 methods in this class:
AddProductDetal () whose return type is void, ShowProductDetal () has the return type void and finally Open () has the return
type void. These two AddProductDetal (), ShowProductDetal () methods will implement the system functions of adding
products and showing all of the store's products. In addition, the above two methods will be implemented in the open ()
method.
References
(n.d.). https://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/.
(n.d.). https://stackify.com/oop-concepts-c-sharp/.
(n.d.). https://stackify.com/oop-concepts-c-sharp/.
(n.d.). https://www.tutorialspoint.com/csharp/csharp_interfaces.htm.
(n.d.). https://www.infoworld.com/article/2928719/when-to-use-an-abstract-class-vs-interface-in-csharp.html.
(n.d.). https://www.c-sharpcorner.com/UploadFile/annathurai/abstract-class-in-C-Sharp/.
(n.d.). https://dotnettutorials.net/lesson/function-overloading-csharp/.
(n.d.). https://www.guru99.com/c-sharp-access-modifiers-constructor.html.
(n.d.). https://www.informit.com/articles/article.aspx?p=101373&seqNum=3.
(n.d.). https://dotnettutorials.net/lesson/encapsulation-csharp/.
(n.d.). https://dotnettutorials.net/lesson/function-overloading-csharp/.
(n.d.). https://www.geeksforgeeks.org/c-sharp-method-overriding/.