1st Project Work Given by Kiran Sir
1st Project Work Given by Kiran Sir
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.
//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.
//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.
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
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."
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.
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 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!!