Unit 20 - Assignment 1 Frontsheet
Unit 20 - Assignment 1 Frontsheet
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:
P1&P2 Examine the characteristics of the object-orientated paradigm as well as the various class relationships.
I.OOP
OOP (brief for Object Oriented Programming) – item-orientated programming is a programming approach primarily based
totally at the idea of instructions and items. OOP makes a speciality of manipulating items instead of the common sense for
manipulating them.
The purpose of OOP is to optimize supply code management, growth reusability, and maximum importantly, assist
encapsulate approaches with acknowledged residences via the usage of items.
1.Object
Objects in OOP include 2 principal components:
To make it less complicated to imagine, we've got a real-lifestyles instance of an item that could be a phone. This item will
have:
Attributes: color, memory, working system…
2.Class
Class is an abstraction of the item. Objects with comparable residences are grouped right into a magnificence. The
magnificence may even consist of portions of records: residences and methods.
Continuing the instance withinside the item segment above, we've got a phone magnificence including 2 components:
3. Example
Create a Class:
- Class contributors consist of fields and homes and keyword ”public” is an get entry to modifier.
- The fields which are taken into consideration traits of gadgets inclusive of name, age, height, weight ... You
also can set the preliminary rate for the sector proper after initialization
- Properties are the actions of that object such as eating, running, emitting a cry...
Constructors
o A constructor is a special method that is used to initialize objects. The advantage of a constructor
is that it is called when an object of a class is created. It can be used to set initial values for fields
o The constructor ought to suit the elegance call and it can not have a go back type (void, int so
on ...).
o The constructor is referred to as whilst the item is created.
o The constructor assist shop time.
Diagram
To create a "Animal", specify the call after the challenge and use the new "new":
o You can create multiple objects of one class:
1.Definition Encapsulation
Encapsulation permits hiding records and the inner processing homes of the item. Other items can't immediately have an
effect on the facts internal and extrade the kingdom of the item, however should undergo public strategies supplied via way
of means of that item.
2. Access Modifiers
Definition : All sorts and kind contributors have an accessibility degree. The accessibility degree controls whether or
not they may be used from different code on your meeting or different assemblies. Use the subsequent get right of
entry to modifiers to specify the accessibility of a kind or member whilst you claim it:
public: The kind or member may be accessed with the aid of using every other code withinside the identical meeting or
some other meeting that references it.
non-public: The kind or member may be accessed best with the aid of using code withinside the identical magnificence or
struct.
included: The kind or member may be accessed best with the aid of using code withinside the identical magnificence, or in a
category this is derived from that magnificence.
internal: The kind or member may be accessed with the aid of using any code withinside the identical meeting, however
now no longer from some other meeting.
included internal: The kind or member may be accessed with the aid of using any code withinside the meeting wherein it is
declared, or from inside a derived magnificence in some other meeting.
non-public included: The kind or member may be accessed best inside its asserting meeting, with the aid of using code
withinside the identical magnificence or in a kind this is derived from that magnificence.
Property is contributors named the layers, structure, and Interface. The versions of contributors or protocol in a study room
or systems are known as the Field. Properties are an inheritance of the Field and are accessed through the use of the equal
tool. They use accessor via Private Field values may be read, written and manipulated.
3. Property
Definition : Property Property does now no longer call the archives. Instead, they have got accessors to read, write or
calculate their values.
This assets enables to boom the safety of the item and keep away from accidental facts corruption.
4. For example
Height and width here are the properties (properties) of the class object hinhchunhat
Tinhdientich() is a method that is public for the purpose of interacting with other objects. Create a class Program
with a static method to run, see how to interact and change the properties of the object through public methods:
Run Code :
So whilst we need to alternate the properties (properties), we can not have interaction without delay with the properties,
however must undergo public strategies described in the class,
We can not understand the inner common sense of the object.
Diagram:
IV.Inheritance
1. Definition Inheritance
This is a feature that is used quite a lot. Inheritance allows building a new class (Child class), inheriting and reusing
properties and methods based on the old class (Parent class) that existed before.
Child classes inherit all the members of the Parent class and do not need to be redefined. Subclasses can extend inherited
components or add new ones.
2. For example
Single level Inheritance : With a parent class and a child class.
o We have class Animal (father) with fields: height, weight, name, age.
The Dog class also has a color field and a Bark() method
Since here dog has inherited Animal, there are methods and fields of animal we can call them in main function.
Run Code :
o So we only create an object of the Dog class, but that object can still use the fields and methods of the Animal class
because the Dog class inherits the Animal class.
O The identical applies with the instance above. We create some other Puppy magnificence that inherited the Dog
magnificence with 1 field: Legs and a method: Cry()
- In the Class Program, we create a puppy object of the puppy class. We then give that object access to the fields and
methods of the Puppy class, Dog class and the Animal class as shown below
Run Code:
- In the Puppy magnificence most effective the Legs area and the Cry () technique are available, however right here we
will nevertheless get entry to the Color area of the Dog magnificence and the Eat () technique of the Animal
magnificence. Because it inherits the Dog magnificence and the Dog magnificence inherits from the Animal
magnificence, it may use the fields of the 2 instructions above. This is known as multi-stage inheritance
1.Method Overloading
Is a manner to overload strategies with the identical call however special parameters
For Example :
We want to add 3 add methods with the same name but different parameters as the code below.
Diagram:
Run code :
When we run it we get 3 different results. We have loaded 3 different corresponding values and called them to main
function.
2.Method Overriding:
This is a technique that overwrites the digital strategies of a sure superclass (declared with the digital keyword).
override: key-word used to mark the approach to override the approach of the discern class.
For Example:
Create a superclass Animal with fields and methods But with method declaration with virtual keyword. And 2 subclasses,
Dog and Cat, inherit Animal
When using virtual and overestimate the outcome of the performance, this time from the lock has been priority and
overwrite the method from the father's class, When the subjects call the same method "Speak" it will be pointed to the
corresponding method of each object created.
Override has taken precedence and overrides the virtual method from the superclass, when objects call the EXplainSelf()
method it will point to the corresponding method of each instantiated object.
Diagram:
VI.Abstraction
1. Definition :
Abstraction removes the pointless complexity of the item and focuses best on what's critical and important.
2. Abstract Class
everyday technique (remains has common sense to go back records or carry out a few action, it's miles used for widespread
purposes)
Heading to your calculations and your executable features are used as general functions for the class extend.
For Example:
Abstract class animal has 2 method is abstract method and method(Eat class Dog class Dog class and Cat inherited the
abstract method.
Create the abstract class animal contains 2 method, a abstract method and a method that is usually implemented. As you can
see, the class Dog, Cat can only extend a abstract class, and only implement the method of the ractical abstract method and
implement method can still use, which can also write a Eat method, with virtual lock in the Cat may not practice method.
Run code:
Diagram:
3. Interface
Quite just like summary magnificence however interface isn't always magnificence, in interface handiest announces
empty methods/homes with out a implementation, implementation might be proven in inherited classes, interface is
sort of a template framework to implementation and comply with classes.
It's not class.
Only containing method/empty properties are no execution.
It's like a pattern, a frame for implement and follow.
The classes can implements a lot of interface.
As a contract, the implement class must deploy method as interface.
For Example:
• Interface may be described the usage of the interface keyword. An interface can include declarations of techniques,
properties, indexers, and events (with empty bodies). However, it can't include fields, auto-carried out properties.
• An interface is a completely "summary class", which could simplest include summary techniques and properties
(with empty bodies):
• Example of interface:
o Create interface IAnimal:
Typically the interface call begins offevolved with the letter "I" so that you can without problems distinguish that it's
miles an interface instead of a class.
o In order to get entry to interface methods, the interface wishes to be implemented (inherited) through some other
class. The following instance will make this clear:
Run code :
Diagram:
4 Interface vs Abstract Class
Priority:
Interface
Abstract class
Disarm point:
Interface:
Every time I define the calculations, the class impeccable they have to add that feature, there's a possibility of nothing to do with it.
Abstract class
https://viblo.asia/p/khac-nhau-giua-abstract-class-va-interface-khi-nao-dung-chung-ORNZq9YrZ0n. (n.d.).