Object Oriented Programming
Object Oriented Programming
CONFIDENTIAL
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
1. Single Inheritance
2. Hierarchical Inheritance
3. Multi Level Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance
Retail_Basics_E0_latest
-2-
CONFIDENTIAL
Single Inheritance
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
Retail_Basics_E0_latest
-4-
CONFIDENTIAL
Multi Level Inheritance
Base Class
Derived Class1
Derived Class2
Retail_Basics_E0_latest
-5-
CONFIDENTIAL
Hybrid Inheritance
Base Class
Derived Class1
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.
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.
String Update();
String Add();
String Delete();
String Search();
String CalculateWage();
}
Retail_Basics_E0_latest
-8-
CONFIDENTIAL
Polymorphism
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.
// 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; }
}
Retail_Basics_E0_latest
- 13 -
CONFIDENTIAL
Automatically implemented properties
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;
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 };
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.
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.
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
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.
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.
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;
}
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.
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.
Implicit Conversion: Y = X;
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.
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:
Retail_Basics_E0_latest
- 25 -
CONFIDENTIAL
Thank you