















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
P1: The characteristics of the object-orientated paradigm
Typology: Essays (university)
Uploaded on 10/06/2021
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Qualification TEC 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 Student ID BHAF Class Assessor name 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 D
Grade: Assessor Signature: Date: Internal Verifierās Comments: Signature & Date:
āObject-Oriented Programmingāā and āāData Abstractionāā have become very common terms. Unfortunately, few people agree on what they mean. In this assignment I will explain the characteristics of object-oriented programming such as encapsulation, inheritance, polymorphism, ... as well as different class relationships. After that, I will design a class diagram based on a given code scenario. This report consists of two sections:
OOP stands for Object-Oriented Programming. Object-oriented programming is a methodology or paradigm to design a program using classes and objects. OOP focuses on the objects that developers want to manipulate rather than the logic required to manipulate them. This approach to programming is well-suited for programs that are large, complex and actively updated or maintained. An OO program: āa bunch of objects telling each other what to do by send messagesā. Some benefits of OOP: ļ· Better abstractions (modeling information and behavior together). ļ· Better maintainability (more comprehensible, les fragile). ļ· Better reusability (classes as encapsulated components). OOP has many features that make it useable including the followings: ļ· Object ļ· Class ļ· Inheritance ļ· Polymorphism ļ· Abstraction ļ· Encapsulation. Letās discuss above each OOPs concepts with real-world example.
The two most important concepts in object-oriented programming are the class and the object. The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables like the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword in Java Programming language. A class is a template or blueprint from which objects are created. So, an object is the instance(result) of a class. I can find various Object definitions as:
From the above program, the Student object are: Each of these statements has three parts (discussed in detail below): Declaration: The code Student student ; declarations that associate a variable name with an object type. Instantiation : The new keyword is a Java operator that creates the object. Initialization : The new operator is followed by a call to a constructor, which initializes the new object. 1) Declaring a Variable to Refer to an Object General syntax; type name; This notifies the compiler that you will use a name to refer to data whose type is a type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. From the above program, we can declare variables to refer to an object as:
2) Instantiating a Class The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor. For example: Note that we have used a new keyword to create Student objects. 3) Initializing an Object The new keyword is followed by a call to a constructor, which initializes the new object. For example: From above code will call below constructor in Student class.
A class is a group of objects which have common properties. A class is a kind of mold or template that dictates what objects can and cannot do. An object is called an instance of a class. An object is an instance of exactly one class. An instance of a class belongs to the class. In short, a class is the specification or template of an object. Once a class is defined, we can create as many instances of the class as a program requires. Figure below shows a diagram that we will use throughout the book to represent a class.
Output:
Abstraction means hiding the lower-level details and exposing only the necessary and relevant details to the user. Real-world example
Step 2 : The Contractor class inherits all properties from its parent abstract Employee class but have to provide its own implementation to calculateSalary () method. In this case, we multiply the value of payment per hour with given working hours. Step 3 : The FullTimeEmployee also has its own implementation of calculateSalary () method. In this case, we just multiply by a constant value of 8 hours. Step 4 : Let's create a AbstractionDemo class to test implementation of Abstraction with below code:
Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation. In OOP, data and methods operating on that data are combined together to form a single unit, which is referred to as a Class. Real-world example
Output:
The Inheritance is a process of obtaining the data members and methods from one class to another class, plus can have its own is known as inheritance. It is one of the fundamental features of object-oriented programming. Inheritance - relationship between a superclass and its subclasses. ļ· Sub class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class. ļ· Super class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class. This can be visualized as follows.
In class diagrams such as this, subclasses point up to their superclass. The attributes and behaviors implemented in the superclass are āinheritedā by all the subclasses. The attributes and behaviors implemented in one of the subclasses are unique that subclass. In a sense, the features shared by subclass1 and subclass 2, that might otherwise have been implemented separately in each of the subclasses, can be collected and āraised upā into the single shared superclass. Real-world example
Notice that the modifier for the instance variables is protected, making them visible and accessible from the instance methods of the class and the descendant classes. If I declare a data member of a class private, then this data member is accessible from the methods of the
class only. If I declare a data member public, this data member is accessible from everywhere. We declare them protected so they are accessible from both the methods of the class and the descendant classes. I will define the classes UndergraduateStudent and GraduateStudent as subclasses of the Student class. In Java, we say a subclass extends its superclass. The difference between the classes GraduateStudent and UndergraduateStudent lies in the way their final course grades are computed. The two subclasses are defined as follows: Figure below shows the class diagram relating the three classes. By seeing an inheritance arrow connecting a subclass to its superclass, we know that data members and methods indicated on the superclass are applicable to the subclasses also.