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

Object Oriented Programming

The document discusses key concepts of object-oriented programming including encapsulation, inheritance, polymorphism, and properties. Encapsulation involves wrapping data and functions into a single entity to hide its internal representation. Inheritance allows new classes to reuse and extend the behavior of existing classes in a hierarchy. Polymorphism allows the same operation to behave differently depending on the type of object it operates on. Properties provide a way to access class members through get and set accessors.

Uploaded by

sanjeev kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Object Oriented Programming

The document discusses key concepts of object-oriented programming including encapsulation, inheritance, polymorphism, and properties. Encapsulation involves wrapping data and functions into a single entity to hide its internal representation. Inheritance allows new classes to reuse and extend the behavior of existing classes in a hierarchy. Polymorphism allows the same operation to behave differently depending on the type of object it operates on. Properties provide a way to access class members through get and set accessors.

Uploaded by

sanjeev kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented Programming

CONFIDENTIAL

Tanuj Sajwan – February 11, 2018


Encapsulation

Encapsulation is nothing but wrapping the data and functions into single entity/ Encapsulation
is the ability of an object to hide its data and methods from the rest of the world.

class temp
{
int x;
float y;
Public:
void multiply(int x, float y)
{
//definition of multiply
}
void addition(int x, float y)
{
//definition of addition
}
}

Retail_Basics_E0_latest
-1-
CONFIDENTIAL
Inheritance

Concept of object-oriented programming which enables us to create new classes that


reuse, extend, and modify the behavior that is defined in other classes. The inherited class
is called super class or base class and the class that inherits the base class is called sub
class or derived class. Sometimes base class known as generalized class and derived
class known as specialized class.

Inheritance can be classified to 5 types.

1. Single Inheritance
2. Hierarchical Inheritance
3. Multi Level Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance

Retail_Basics_E0_latest
-2-
CONFIDENTIAL
Single Inheritance

When a single derived class is created from a single base class.

Base Class

Derived Class

Retail_Basics_E0_latest
-3-
CONFIDENTIAL
Hierarchical Inheritance

When more than one derived class are created from a single base class.

Base Class

Derived Class1 Derived Class2

Retail_Basics_E0_latest
-4-
CONFIDENTIAL
Multi Level Inheritance

When a derived class is created from another derived class

Base Class

Derived Class1

Derived Class2

Retail_Basics_E0_latest
-5-
CONFIDENTIAL
Hybrid Inheritance

A combination of single, hierarchical and multi level inheritances.

Base Class

Derived Class1

Derived Class2 Derived Class3

Retail_Basics_E0_latest
-6-
CONFIDENTIAL
Multiple Inheritance

When a derived class is created from more than one base class then that inheritance is
called as multiple inheritance. But multiple inheritance is not supported by .net using
classes and can be done using interfaces.

Base Class1 Base Class2

Derived Class

Retail_Basics_E0_latest
-7-
CONFIDENTIAL
Interfaces

An interface is not a class. It is an entity that has no implementation; it only has the
signature or in other words, just the definition of the methods without the body.

public interface IEmployee


{
String ID { get; set; }
String FirstName { get; set; }
String LastName { get; set; }

String Update();
String Add();
String Delete();
String Search();
String CalculateWage();
}

Retail_Basics_E0_latest
-8-
CONFIDENTIAL
Polymorphism

Same operation may behave differently on different classes. Polymorphism


can be classified to 2 types.

Compile Time Polymorphism: Method Overloading


Run Time Polymorphism: Method Overriding

Method Overloading
- Method with same name but with different arguments is called method overloading.
- Method Overloading forms compile-time polymorphism.

class A1
{
void hello()
{ Console.WriteLine(“Hello”); }

void hello(string s)
{ Console.WriteLine(“Hello {0}”,s); }
}

Retail_Basics_E0_latest
-9-
CONFIDENTIAL
Polymorphism
Method Overriding
- Method overriding occurs when child class declares a method that has the same type
arguments as a method declared by one of its superclass.
- Method overriding forms Run-time polymorphism.
Note: By default functions are not virtual in C# and so you need to write “virtual” explicitly.

class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}
Class child: parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}
static void main()
{
parent objParent = new child();
objParent.hello();
}

//Output
Hello from Child.
Retail_Basics_E0_latest
- 10 -
CONFIDENTIAL
Operator overloading
Operator overloading permits user-defined operator implementations to be specified for
operations where one or both of the operands are of a user-defined class or struct type.

public class Complex


{
public int real;
public int imaginary;

public Complex(int real, int imaginary)


{
this.real = real;
this.imaginary = imaginary;
}
// Declare which operator to overload (+), the types
//that can be added (two Complex objects), and the return type (Complex):
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
// Override the ToString method to display an complex number in the suitable format.
public override string ToString()
{
return(String.Format("{0} + {1}i", real, imaginary));
} Retail_Basics_E0_latest
CONFIDENTIAL
Operator overloading

public static void Main()


{
Complex num1 = new Complex(2,3);
Complex num2 = new Complex(3,4);

// Add two Complex objects (num1 and num2) through the


// overloaded plus operator:
Complex sum = num1 + num2;

// Print the numbers and the sum using the overriden ToString method:
Console.WriteLine("First complex number: {0}",num1);
Console.WriteLine("Second complex number: {0}",num2);
Console.WriteLine("The sum of the two numbers: {0}",sum);

}
}

Retail_Basics_E0_latest
- 12 -
CONFIDENTIAL
Properties
A property is like a "virtual field" that contains get and set assessors and provides
an interface to the members of a class. They can be used to set and get values to
and from the class members.

class Test
{
private int number;
public int MyProperty
{
get { return number; }
set { number = value; }
}

static void Main(string[]args)


{
Test test = new Test();
test.MyProperty = 100;
Console.WriteLine(test.MyProperty);
Console.ReadLine();
}
}

Retail_Basics_E0_latest
- 13 -
CONFIDENTIAL
Automatically implemented properties

In C# 3.0 and later, auto-implemented properties make property declaration more


concise when no additional logic is required in the property assessors.

public class MyClass


{
public int X { get; private set;}
public string MyValue1 { get; set;}
}

Retail_Basics_E0_latest
- 14 -
CONFIDENTIAL
Indexers
Concept which is used for treating an object as an array.
class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get { return data[index]; }
set { data[index] = value; }
}
}
class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = “Sears";
mc[1] = “Retail";
mc[2] = “TCS";
mc[3] = “Noida";
mc[4] = “UP";
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}
Retail_Basics_E0_latest
- 15 -
CONFIDENTIAL
Structures
- Structs are stack objects.
- Structs cannot inherit from other structs.
- We cannot declare a default constructor for a struct, constructors must have parameters.
- No heap allocation, less GC pressure.
- Ideal for light weight objects
struct Student
{
public int maths;
public int english;
public int csharp;

public int GetTot()


{ return maths+english+csharp; }

public Student(int y) { maths = english = csharp = y; }


public string GetGrade()
{
if(GetTot() > 240 ) return "Brilliant";
if(GetTot() > 140 ) return "Passed";
return "Failed";
}
}

Retail_Basics_E0_latest
- 16 -
CONFIDENTIAL
Enumeration

Enums are basically a set of named constants. They are declared in C# using the enum
keyword. Enums are value types and are created on the stack and not on the heap.

class Program
{
enum Importance
{ None, Trivial, Regular, Important, Critical };

static void Main()


{
Importance value = Importance.Critical;

if (value == Importance.Trivial)
{
Console.WriteLine("Not true");
}
else if (value == Importance.Critical)
{
Console.WriteLine("True");
}
}
}
Retail_Basics_E0_latest
- 17 -
CONFIDENTIAL
Assemblies

An assembly is the .NET name for an executable file. A .NET assembly is just the .EXE or
.DLL file that is the end result of our program. An assembly provides a fundamental unit of
physical code grouping.

There are three types of assemblies:

Private Assembly - This type of assembly is used by a single application. It is stored in the
application's directory or the applications sub-directory. There is no version constraint in a
private assembly.

Shared Assembly or Public Assembly - A shared assembly has version constraint. It is


stored in the Global Assembly Cache (GAC). GAC is a repository of shared assemblies
maintained by the .NET runtime.

A Satellite Assembly - is an assembly that contains only resources, and no code. The
resources are location specific. A satellite assembly is associated with a main assembly, the
one that actually contains the code.

Retail_Basics_E0_latest
- 18 -
CONFIDENTIAL
Namespaces

A namespace provides a fundamental unit of logical code grouping. It is an another way to


organize our .NET code. Namespaces are a way of grouping type names and reducing the
chance of name collisions.

Namespace PowerPlant
Namespace ControlPanel
Public Class Button
' Statements to implement Button
End Class
End Namespace
End Namespace

Retail_Basics_E0_latest
- 19 -
CONFIDENTIAL
Access Specifiers

The availability (scope) of the member objects of a class may be controlled using access
specifiers.

1. PUBLIC: If a member of a class is defined as public then it can be accessed anywhere in


the class as well as outside the class.

2. PRIVATE: It can't be accessed outside the class. Its the private property of the class and
can be accessed only by the members of the class.

3. FRIEND/ INTERNAL: Friend & Internal mean the same. Friend is used in VB.NET. Internal
is used in C#. Friends can be accessed by all classes within an assembly but not from
outside the assembly.

4. PROTECTED: Protected variables can be used within the class as well as the classes that
inherits this class.

5. PROTECTED FRIEND/ PROTECTED INTERNAL: The Protected Friend can be accessed


by Members of the Assembly or the inheriting class, and of course, within the class itself.

Retail_Basics_E0_latest
- 20 -
CONFIDENTIAL
Partial classes
Partial classes allow for a single class's members to be divided among multiple source code
files. At compile-time these multiple files get combined into a single class as if the class's
members had all been specified in a single file.
public partial class Employee
{
public int EmployeeID;
public string FirstName;
public string LastName;
public decimal Salary;
}

public partial class Employee


{
public void ApplyAnnualCostOfLivingRaise()
{
Salary *= 1.05;
}
}

Uses of Partial Classes:


- Improve the readability of extremely large classes.
- Reduce the likelihood of one developer being blocked from working on a class until it is
checked in by another developer.
Retail_Basics_E0_latest
CONFIDENTIAL
Partial methods

A partial method is like a usual method in a class except that the user may or may not
implement it. A partial method gets executed only when it has an implementation. What is the
use of a method that does not have an implementation? Such methods can act as hooks for
plugging in code.

partial class Program


{
static void Main()
{
myMethod();
}
static partial void myMethod();
}

partial class Program


{
static partial void myMethod() {//body of partial method}
}

Retail_Basics_E0_latest
- 22 -
CONFIDENTIAL
Conversion operators

Conversion operators convert an object from one type to another type. Conversion operators
can be implicit or explicit. Implicit conversion operators do not require a type cast to be
specified in source code to perform the conversion. Explicit conversion operators require a
type cast be present in the source code to perform the conversion.

Uint X = 100; //Unsigned integer


Long Y;

Implicit Conversion: Y = X;

Explicit Conversion: X= (Uint)Y;

Explicit conversions are those conversions that require a typecast, and are normally
used for conversions that might result in loss of data. Since Int128 is larger than any
of the existing integer types, converting from it to any of those types may result in loss
of data.

Retail_Basics_E0_latest
CONFIDENTIAL
Delegates
Delegate is a type which holds the method(s) reference in an object. It is also referred to as
a type safe function pointer.

public delegate double Delegate_Prod(int a, int b);


class Class1
{
static double fn_Prodvalues(int val1,int val2)
{
return val1*val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
Console.Write("Please Enter Values");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());
//use a delegate for processing
double res = delObj(v1,v2);
Console.WriteLine ("Result :"+res);
}
}

Retail_Basics_E0_latest
- 24 -
CONFIDENTIAL
Lambda expressions

A lambda expression is an anonymous function that can contain expressions and statements,
and can be used to create delegates or expression tree types.

All lambda expressions use the lambda operator =>, which is read as "goes to". The left side
of the lambda operator specifies the input parameters (if any) and the right side holds the
expression or statement block. The lambda expression x => x * x is read "x goes to x times
x." This expression can be assigned to a delegate type as follows:

delegate int del(int i);

static void Main(string[] args)


{
del myDelegate = x => x * x;
int j = myDelegate(5); //j = 25
}

Retail_Basics_E0_latest
- 25 -
CONFIDENTIAL
Thank you

You might also like