0% found this document useful (0 votes)
17 views

LAB02

This document discusses the four principles of object-oriented programming (OOP) in C#: abstraction, encapsulation, inheritance, and polymorphism. It provides examples of how to create a Rectangle class to demonstrate basic C# syntax and OOP concepts.

Uploaded by

Saboor Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

LAB02

This document discusses the four principles of object-oriented programming (OOP) in C#: abstraction, encapsulation, inheritance, and polymorphism. It provides examples of how to create a Rectangle class to demonstrate basic C# syntax and OOP concepts.

Uploaded by

Saboor Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB #02

To study basic Principles of OOP in C# using simple codes.

Introduction

C# provides full support for object-oriented programming including abstraction, encapsulation,


inheritance, and polymorphism.

 Abstraction means hiding the unnecessary details from type consumers.


 Encapsulation means that a group of related properties, methods, and other members are
treated as a single unit or object.
 Inheritance describes the ability to create new classes based on an existing class.
 Polymorphism means that you can have multiple classes that can be used interchangeably,
even though each class implements the same properties or methods in different ways.

Creating Rectangle Program

For example, let us consider a Rectangle object. It has attributes such as length and width.
Depending upon the design, it may need ways for accepting the values of these attributes,
calculating the area, and displaying details.
Let us look at implementation of a Rectangle class and discuss C# basic syntax –
Task:
01) Create a variable called name of type string and assign it the value
"John".

Input:

Output:

---------------------------------------------------------------------------
02) Create a variable called myNum of type int and assign it the
value 15.

Input:

Output:

---------------------------------------------------------------------------
03) Declare a variable without assigning the value, and assign the
value later:

Input:

Output:

-----------------------------------------------------------------------------
04) Perform Decision Making: Equality and Relational Operators
program given in theory lecture using Classes, Variable and Objects.

Input:

Output:
--------------------------------------------------------------------
05) Make a class EvenODD, which checks whether the number you
input is an Even or Odd number.

Input:

Ouput:
-----------------------------------------------------------------------------
06) Give real time examples (3) of four principles of OOP in your own
words.

1. Encapsulation: Consider a banking application. Each bank


account object encapsulates its data (such as account balance,
account holder's name, and account number) and methods (such as
deposit, withdraw, and check balance). These details are
encapsulated within the account object, and external entities can
only interact with the account through well-defined interfaces
(methods), ensuring data integrity and security.
2. Inheritance: Think about a vehicle manufacturing company. The
company may have a base class called "Vehicle," which defines
common attributes and methods shared among different types of
vehicles, such as cars, trucks, and motorcycles. Each specific type
of vehicle (e.g., "Car" or "Truck") inherits from the base class
"Vehicle" and adds its unique attributes and methods. For instance,
a "Car" subclass inherits features like "number of wheels" and
"engine type" from the "Vehicle" superclass but may also have
additional methods specific to a car, like "openTrunk" or
"startEngine."
3. Polymorphism: Imagine a drawing application with various
shapes like circles, squares, and triangles. Each shape class may
have a method called "draw" to render itself on the screen.
Through polymorphism, you can treat each shape object uniformly
by calling the "draw" method, regardless of its specific type. For
example, you could have a list containing circles, squares, and
triangles, and iterate through it, invoking the "draw" method on
each shape. Despite the different shapes, the "draw" method
behaves differently based on the specific type of shape being
drawn, showcasing polymorphic behavior.
4. Abstraction: Let's consider a media player application. The
application might have an interface called "Playable" that defines
methods like "play," "pause," and "stop." Different media types,
such as audio files, video files, and streaming content, can
implement this interface. Each media type provides its
implementation of these methods, abstracting away the specific
details of how they are played. Users interact with the media player
through these abstracted methods, without needing to know the
underlying complexities of playing each type of media.

You might also like