[go: up one dir, main page]

0% found this document useful (0 votes)
11 views11 pages

1st Project Work Given by Kiran Sir

The document provides an overview of Object-Oriented Programming (OOP) in C#, detailing key concepts such as abstraction, encapsulation, inheritance, and polymorphism. It explains the structure of classes and objects, including constructors and destructors, and illustrates these concepts with code examples. Additionally, it covers advanced topics like abstract classes, interfaces, and static classes, emphasizing their roles and functionalities in OOP.

Uploaded by

seasonaryan80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

1st Project Work Given by Kiran Sir

The document provides an overview of Object-Oriented Programming (OOP) in C#, detailing key concepts such as abstraction, encapsulation, inheritance, and polymorphism. It explains the structure of classes and objects, including constructors and destructors, and illustrates these concepts with code examples. Additionally, it covers advanced topics like abstract classes, interfaces, and static classes, emphasizing their roles and functionalities in OOP.

Uploaded by

seasonaryan80
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1st Project Work Given by sir on the topic of OOP with C#

Date: 2081/10/21
1. Object-Oriented Programming
 Object-oriented programming (OOP) refers to the creation of reusable software object-
types / classes that can be efficiently developed and easily incorporated into multiple
programs.
 In OOP an object represents an entity in the real world (a student, a desk, a button, a
file, a text input area, a loan, a web page, a shopping cart).
 An OOP program = a collection of objects that interact to solve a task / problem.
 OOPs is a concept of modern programming language that allows programmers to
organize entities and objects. Four key concepts of OOPs are abstraction,
encapsulation, inheritance, and polymorphism. Here learn how to implement OOP
concepts in C# and .NET.

2. OOP Features/Principles:
Here are the key features of OOP:
o Object Oriented Programming (OOP) is a programming model where programs are
organized around objects and data rather than action and logic.
o OOP allows decomposing a problem into many entities called objects and then
building data and functions around these objects.
o A class is the core of any modern object-oriented programming language such as C#.
o In OOP languages, creating a class for representing data is mandatory.
o A class is a blueprint of an object that contains variables for storing data and
functions to perform operations on the data.
o A class will not occupy any memory space; hence, it is only a logical representation
of data.
o To create a class, you use the keyword "class" followed by the class name:
class Employee
{
}
C#
Copy

Object
 The software is divided into several small units called objects. The data and
functions are built around these objects.
 An object's data can be accessed only by the functions associated with that object.
 The functions of one object can access the functions of another object.
 Objects are the basic run-time entities of an object-oriented system. They may
represent a person, a place, or any item the program must handle.
 "An object is a software bundle of related variables and methods."
 "An object is an instance of a 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, which is called an
object.
 When an object is created using the new operator, memory is allocated for the class
in a 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 a heap; in other words, an instance will not be created, and the object in the stack
will contain the value null.
 When an object contains null, it is impossible to access the class members using
that object.
 class Employee
{

}
C#
Copy
Syntax to create an object of class Employee:
Employee objEmp = new Employee();
C#
Copy

3. Constructors are a particular type of method associated with a class and gets
automatically invoked when the classes instance (i.e., objects) are created. Like other
member functions, i.e., methods, these constructors also contain specific instructions that
get executed when the objects are created. It is specifically used to assign values to all or
some data members of a class.

Some Special Characteristics of the Constructor:


 The name of the constructors must have to be the same as that of the class's name in
which will resides.
 A constructor can never be final, abstract, synchronized, and static.
 You can produce only a single static constructor.
 A static constructor cannot be used as a parameterized constructor.
 Constructors usually don't have a return type, not even void.
 The number of constructors can be any within a class.
 Constructors can contain access modifiers along with it.

C# program to demonstrate constructor


using System;
namespace KarlosData
{
class EmpGkr
{
private string month;
private int year;
public string name, location;

//Default Constructor
public EmpGkr()
{
name = "Santosh Aryal";
location = "Bheerkot-1";
}

//Parameterized Constructor
public EmpGkr(string a, string b)
{
name = a;
location = b;
}

public EmpGkr(EmpGkr s)
{
month = s.month;
year = s.year;
}
}

class Program
{
static void Main(string[] args)
{
EmpGkr empGkr = new EmpGkr();
EmpGkr u2 = new EmpGkr(empGkr);
Console.WriteLine(empGkr.name);
Console.WriteLine(empGkr.location);
Console.WriteLine("\n Press to Exit .... ");
Console.ReadLine();
}
}
}
4. Destructor is another method that uses the class-name but is preceded with a ~ (tilde)
operator/symbol. Destructors are used to de-initialize objects, and the memories occupied
when constructors are created. You can consider them as the opposite of constructors.

C# program to demonstrate destructor:


public class EmpGkr
{
int start;
//constructor
public EmpGkr()
{
val = 10;
}

//destructor
~ EmpGkr()
{
val = 0;
}
}

5. OOPs Concepts
The key concepts of OOPs are:
 Abstraction
 Encapsulation
 Inheritance
 Polymorphism

 Abstraction
o Abstraction is "To represent the essential feature without representing the
background details."
o Abstraction lets you focus on what the object does instead of how it does it.
o Abstraction provides a generalized view of your classes or objects by providing
relevant information.
o Abstraction is the process of hiding the working style of an object and showing the
information about an object understandably.

Real-world Example of Abstraction


Suppose you have an object Mobile Phone.
Suppose you have three mobile phones as in the following:
Nokia 1400 (Features: Calling, SMS)
Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)
BlackBerry (Features: Calling, SMS, FM Radio, MP3, Camera, Video Recording,
Reading E-mails)
Abstract information (necessary and common information) for the "Mobile Phone"
object is that it calls any number and can send an SMS.
So, for a mobile phone object, you will have the abstract class as in the following,
abstract class MobilePhone {
public void Calling();
public void SendSMS();
}
public class Nokia1400: MobilePhone {}
public class Nokia2700: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
}
public class BlackBerry: MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}
C#
Copy

Abstraction means putting all the necessary variables and methods in a class.
Example
If somebody in your college tells you to fill in an application form, you will provide
your details, like name, address, date of birth, which semester, the percentage you
have, etcetera.
If some doctor gives you an application, you will provide the details, like name,
address, date of birth, blood group, height, and weight.
See in the preceding example what is in common?
Age, name, and address, so you can create a class that consists of the common data.
That is called an abstract class.
That class is not complete, and other classes can inherit it.

 Encapsulation
Wrapping up a data member and a method together into a single unit (in other words,
class) is called Encapsulation.
Encapsulation is like enclosing in a capsule. That is, enclosing the related operations
and data related to an object into that object.
Encapsulation is like your bag in which you can keep your pen, book, etcetera. It
means this is the property of encapsulating members and functions.
class Bag {
book;
pen;
ReadBook();
}
C#
Copy

o Encapsulation means hiding the internal details of an object, in other words, how
an object does something.
o Encapsulation prevents clients from seeing its inside view, where the behavior of
the abstraction is implemented.
o Encapsulation is a technique used to protect the information in an object from
another object.
o Hide the data for security, such as making the variables private, and expose the
property to access the private data that will be public.
o So, when you access the property, you can validate the data and set it.
Example 1
class Demo {
private int _mark;
public int Mark {
get {
return _mark;
}
set {
if (_mark > 0) _mark = value;
else _mark = 0;
}
}
}
C#
Copy

Real-world Example of Encapsulation


Let's use as an example Mobile Phones and Mobile Phone Manufacturers.
Suppose you are a Mobile Phone Manufacturer and have designed and developed a
Mobile Phone design (a class). Now by using machinery, you are manufacturing
Mobile Phones (objects) for selling; when you sell your Mobile Phone, the user only
learns how to use the Mobile Phone but not how the Mobile Phone works.

This means that you are creating the class with functions and by with objects
(capsules), of which you are making available the functionality of your class by that
object and without interference in the original class.

Example 2
TV operation
It is encapsulated with a cover, and we can operate it with a remote, and there is no
need to open the TV to change the channel. Everything is private except the remote, so
anyone can access the remote to operate and change the things on the TV.

 Inheritance
When a class includes a property of another class, it is known as
inheritance. Inheritance is a process of object reusability.
For example, a child includes the properties of its parents.
public class ParentClass {
public ParentClass() {
Console.WriteLine("Parent Constructor.");
}
public void print() {
Console.WriteLine("I'm a Parent Class.");
}
}
public class ChildClass: ParentClass {
public ChildClass() {
Console.WriteLine("Child Constructor.");
}
public static void Main() {
ChildClass child = new ChildClass();
child.print();
}
}
C#
Copy

Output
Parent Constructor.
Child Constructor.
I'm a Parent Class.

 Polymorphism
Polymorphism means one name, many forms. One function behaves in different
forms. In other words, "Many forms of a single object is called Polymorphism."

Real-world Example of Polymorphism


Example 1
A teacher behaves with his students.
A teacher behaves with their seniors.
Here the teacher is an object, but the attitude is different in different situations.
Example 2
A person behaves like a son in a house at the same time that the person behaves like
an employee in an office.
Example 3
Your mobile phone, one name but many forms:
As phone
As camera
As mp3 player
As radio

6. Polymorphism means “Many Forms”. In polymorphism, poly means “Many,” and


morph means “Forms.” polymorphism is one of the main pillars in Object Oriented
Programming. It allows you to create multiple methods with the same name but different
signatures in the same class. The same name methods can also be in derived classes.
There are two types of polymorphism,
 Method Overloading
 Method Overriding

Method Overloading
Method Overloading is a type of polymorphism. It has several names like “Compile Time
Polymorphism” or “Static Polymorphism,” and sometimes it is called “Early Binding”.
Method Overloading means creating multiple methods in a class with the same names but
different signatures (Parameters). It permits a class, struct, or interface to declare multiple
methods with the same name with unique signatures.

// Example of Overloading in C#
// Overloading by changing the Number
// of parameters
using System;
class Geeks
{
// adding two integer values.
public int Add(int a, int b)
{
int sum = a + b;
return sum;
}
// adding three integer values.
public int Add(int a, int b, int c)
{
int sum = a + b + c;
return sum;
}
public static void Main(String[] args)
{
Geeks ob = new Geeks();
int sum1 = ob.Add(1, 2);
Console.WriteLine("add() with two integers");
Console.WriteLine("sum: " + sum1);
Console.WriteLine("add() with three integers");
int sum2 = ob.Add(1, 2, 3);
Console.WriteLine("sum: " + sum2);
}
}

Output
add() with two integers
sum: 3
add() with three integers
sum: 6

Method Overriding
Method Overriding is a type of polymorphism. It has several names like “Run Time
Polymorphism” or “Dynamic Polymorphism,” and sometimes it is called “Late Binding”.
Method Overriding means having two methods with the same name and same signatures
[parameters]; one should be in the base class, and another method should be in a derived
class [child class]. You can override the functionality of a base class method to create the
same name method with the same signature in a derived class. You can achieve method
overriding using inheritance. Virtual and Override keywords are used to achieve method
overriding.
// Example of Overriding in C#
using System;
class Animal {
// Base class
public virtual void Move()
{
Console.WriteLine("Animal is moving.");
}
public void Eat()
{
Console.WriteLine("Animal is eating.");
}
}
class Dog : Animal {
// Overriding the Move method from the base class
public override void Move()
{
Console.WriteLine("Dog is running.");
}
public void Bark()
{
Console.WriteLine("Dog is barking.");
}
}
class Geeks {
static void Main()
{
Dog d = new Dog();
d.Move();
d.Eat();
d.Bark();
}
}

Output
Dog is running.
Animal is eating.
Dog is barking.

7. Abstract Classes and Methods


Data abstraction is the process of hiding certain details and showing only essential
information to the user.
Abstraction can be achieved with either abstract classes
The abstract keyword is used for classes and methods:
 Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).
 Abstract method: can only be used in an abstract class, and it does not have a body.
The body is provided by the derived class (inherited from).
An abstract class can have both abstract and regular methods:

Example:
abstract class Animal
{
public abstract void animalSound();
public void sleep()
{
Console.WriteLine("Zzz");
}
}

Interfaces
An interface is a completely "abstract class", which can only contain abstract methods
and properties (with empty bodies):

Example
// interface
interface Animal
{
void animalSound(); // interface method (does not have a body)
void run(); // interface method (does not have a body)
}

8. Static Class
In C#, a static class is a class that cannot be instantiated. The main purpose of using static
classes in C# is to provide blueprints of its inherited classes. A static class is created using
the static keyword in C# and .NET. A static class can contain static members only. You
can‘t create an object for the static class.
Advantages of Static Classes
1. You will get an error if you declare any member as a non-static member.
2. When you try to create an instance to the static class, it again generates a compile time
error because the static members can be accessed directly with their class name.
3. The static keyword is used before the class keyword in a class definition to declare a
static class.
4. Static class members are accessed by the class name followed by the member name.
Syntax of static class
static class classname
{
//static data members
//static methods
}

Static Class example


The following code declares a class MyCollege with the CollegeName and the Address as
two static members. The constructor of the class initializes these member values, and in
the Main method of the program, we call these static members to display their values.
namespace StaticConstructorsDemo
{
class MyCollege
{
//static fields
public static string CollegeName;
public static string Address;

//static constructor
static MyCollege()
{
CollegeName = "ABC College of Technology";
Address = "Hyderabad";
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(MyCollege.CollegeName);
Console.WriteLine(MyCollege.Address);
Console.Read();
}
}
}

Thank you!!

You might also like