


















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Advanced programming (1651) - Grade D
Typology: Study Guides, Projects, Research
1 / 90
This page cannot be seen from the preview
Don't miss anything!



















































































Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 20: Advanced Programming Submission date 12 / 10 / 2022 Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Phan Nhat Linh^ Student ID GCD Class GCD0905 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 Linh Grading grid P1 P2 M1 M2 D1 D
Grade: Assessor Signature: Date: Lecturer Signature:
2. What are the characteristics of Object-Oriented programming language? 2.1 Object / Class Figure 2 : Class – Object In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). The user-defined objects are created using the class keyword. The class is a blueprint that defines a nature of a future object. An instance is a specific object created from a particular class. Classes are used to create and manage new objects and support inheritance—a key ingredient in object-oriented programming and a mechanism of reusing code.
The image above shows how a Car object can be the template for many other Car instances. In the image, there are three instances: polo, mini, and beetle. Here, we will make a new class called Car, that will structure a Car object to contain information about the car’s model, the color, how many passengers it can hold, its speed, etc. A class can define types of operations, or methods, that can be performed on a Car object. For example, the Car class might specify an accelerate method, which would update the speed attribute of the car object. 2.2 Abstraction Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. That’s a very generic concept that’s not limited to object-oriented programming. You can find it everywhere in the real world. Objects in an OOP language provide an abstraction that hides the internal implementation details. Similar to the coffee machine in your kitchen, you just need to know which methods of the object are available to call and which input parameters are needed to trigger a specific operation. But you don’t need to understand how this method is implemented and which kinds of actions it has to perform to create the expected result. Let’s implement the coffee machine example in Java. You do the same in any other object-oriented programming language. The syntax might be a little bit different, but the general concept is the same. Advantages of Abstraction The main benefit of using an Abstraction in Programming is that it allows you to group several related classes as siblings. Abstraction in Object Oriented Programming helps to reduce the complexity of the design and implementation process of software.
Figure 5 : Example of abstraction in C# (2) As the image above we can see that Interface IGradeStrategy has a method RatingGrade() which returns a string but no body. The FailStrategy class has inherited from the IGradeStrategy class and has to write an extra body for the RatingGrade() method. 2.3 Encapsulation Encapsulation Meaning: In object-oriented computer programming languages, the notion of encapsulation (or OOP Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into a single unit. Many programming languages use encapsulation frequently in the form of classes. A class is a program code-template that allows developers to create an object that has both variables (data) and behaviors (functions or methods). A class is an example of encapsulation in computer science in that it consists of data and methods that have been bundled into a single unit. Encapsulation may also refer to a mechanism of restricting the direct access to some components of an object, such that users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
Figure 6 : UML of Encapsulation in C#
2.4 Inheritance Figure 8 : Inheritance Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can to derive a class from another class for a hierarchy of classes that share a set of attributes and methods. You can use it to declare different kinds of exceptions, add custom logic to existing frameworks, and even map your domain model to a database. Creating a unique class for every single animal would quickly get very repetitious because there are some properties and behaviors that apply to every single animal, from a mouse to an elephant. Shared functions might include feed( ), hydrate( ), cleanEnclosure( ). Instead of creating these shared attributes over and over for every animal, we could instead create a
parent Animal class! This parent class would contain the properties and behaviors universal to all animals and save us from having to create those shared functions ad infinitum. UML: Figure 9 : UML of Inheritance in C#
Figure 11 : Example of Inheritance in C# (2)
The Student class will inherit the Person class as shown in the figure we are seeing. All fields of the Person class such as id, name, age will be inherited by the Student class, in addition to the methods with the virtual keyword so that the subclass can override that method. In the submethod, except for inherited fields, the Student class can have a new field of grade. Methods that the Student class overrides will have the override keyword added. 2.5 Polymorphism The word polymorphism is used in various contexts and describes situations in which something occurs in several different forms. In computer science, it describes the concept that objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface. It is one of the core concepts of object-oriented programming (OOP). If you’re wondering if an object is polymorphic, you can perform a simple test. If the object successfully passes multiple is-a or instanceof tests, it’s polymorphic. As I’ve described in my post about inheritance, all Java classes extend the class Object. Due to this, all objects in Java are polymorphic because they pass at least two instanceof checks. Different types of polymorphism Static polymorphism o C#, like many other object-oriented programming languages, allows you to implement multiple methods within the same class that use the same name. That is called method overloading and represents a static form of polymorphism. o The parameter sets have to differ in at least one of the following three criteria: They need to have a different number of parameters, e.g. one method accepts 2 and another one 3 parameters. The types of the parameters need to be different, e.g. one method accepts a String and another one a Long. They need to expect the parameters in a different order, e.g. one method accepts a String and a Long and another one accepts a Long and a String. This kind of overloading is not recommended because it makes the API difficult to understand. o In most cases, each of these overloaded methods provides a different but very similar functionality.