SlideShare a Scribd company logo
Better Understanding of
OOP Using C#
Chandan Gupta Bhagat
“Write shy code - modules that don't reveal
anything unnecessary to other modules and that
don't rely on other modules' implementations.”
Andy Hunt & DaveThomas
General Overview
• Classes and Objects
• Functions
• Constructors
• Destructors
• Other Members
Classes and Objects
• Classes are blueprints for Object.
• Objects are instance of classes.
using System;
namespace NameSpace
{
<access_modifier> class ClassName
{
<access_modifier> DataMembers
Constructors
<access_modifier> <return_type> Functions
Destructors
}
}
Classes and Objects
Access Modifiers
•public
•private
•protected
•internal
•protected internal
Classes and Objects
MyClass myObject_One=new MyClass();
MyClass myObject_Two=new MyClass();
Or
var myObject_One=new MyClass();
var myObject_Two=new MyClass();
Functions
<access_modifier> <return_type> Functions (<Parameters>)
{
//Function Logic codes here
}
• return_type is void => write return; or don’t write anything
• return_type is other than void => return object of the return_type
• Parameters can be of any number
• Function Overloading (inside the same class with same name but different
signature):
• Add(int a, int b) => a + b;
• Add(int a, int b, int c) =>a + b + c;
Constructors
• a special method that is used to initialize a newly created object and is called
just after the memory is allocated for the object.
public class ClassName
{
private int _number;
public ClassName() //parameterless constructor
{
_number=10;
}
public ClassName(int number) //parameterized constructor{
_number=number;
}
}
ClassName myObject_One=new MyClass(); //the variable number has the value 10
ClassName myObject_Two=new MyClass(2); //the variable number has the value 2
Destructors
~ClassName()
{ }
• Destructors are invoked automatically, and cannot be invoked explicitly.
• Destructors cannot be overloaded.Thus, a class can have, at most, one destructor.
• Destructors are not inherited.Thus, a class has no destructors other than the one,
which may be declared in it.
• Destructors cannot be used with structs.They are only used with classes.
• An instance becomes eligible for destruction when it is no longer possible for any
code to use the instance.
• Execution of the destructor for the instance may occur at any time after the
instance becomes eligible for destruction.
• When an instance is destructed, the destructors in its inheritance chain are called,
in order, from most derived to least derived.
Data Members
• Constants representing constant values
• Fields representing variables
• Properties that define the class features and include actions to fetch and
modify them
• Events generated to communicate between different classes /objects
• Indexers that help in accessing class instances similar to arrays
• Operators that define semantics when used in expressions with class
instances
• Static constructor to initialize the class itself
• Types that are local to the class (nested type)
Abstraction and Encapsulation
Encapsulation
• Encapsulation is the process of hiding irrelevant data from the user.
• These are the irrelevant information for the mobile user, that’s why it is
encapsulated inside a cabinet.
• To understand encapsulation, consider an example of a mobile phone.
Whenever you buy a mobile, you don’t see how circuit board works.You are
also not interested to know how digital signal converts into the analog
signal and vice versa.
Encapsulation
public class Rectangle
{
private float _length;
private float _breadth;
public float Area
{
get{return _length * _breadth;}
}
public float Perimeter
{
get{return 2*(_length + _breadth);}
}
}
• The Encapsulation fields of a class basically can
be write-only or can be read-only.
• A Encapsulated class can have control over in its
fields.
• A class can change data type of its fields
anytime but users of this class do not need to
change any code.
Abstraction
• Abstraction is just opposite of Encapsulation.
• Abstraction is a mechanism to show only relevant data to the user.
• It is an abstraction because you are seeing only relevant information instead
of their internal engineering.
• Consider the same mobile example again. Whenever you buy a mobile
phone, you see their different types of functionalities as a camera, mp3
player, calling function, recording function, multimedia etc.
Abstraction
public abstract class Shape
{
private float _area;
private float _perimeter;
public float Area
{
get{return _area;}
set{_area = value;}
}
public float Perimeter
{
get{return _perimeter;}
set{_perimeter = value;}
}
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}
Inheritance
• Allows us to define a class in terms of another class, which makes it easier to
create and maintain an application.
• Provides an opportunity to reuse the code functionality and speeds up
implementation time.
• inherit data and functions from multiple base classes or interfaces.
• Classes in Inheritance
• Base Class :The Origin Classes
• Derived Classes : Derived from base class
• Sealed Classes :This classes are the last of their kind. Cannot derived further
Inheritance
• Example :
• class A is the origin class having the public member One, protected memberTwo and
private memberThree
• class B : A this means B is derived from A having public member Four
• class C : B this means C is derived from B
• sealed class D : A Now D is derived from A and D is last of its species.
• In this example, when we create an instance of C “Obj_c”, the constructor of
hierarchy A is called first, then B and then C.
• public members are accessible from the object of C, i.e. Member One can be
accessible. We can use Obj_c.One
• Protected members are public inside the class C but not accessible outside the
Object of C. i.e. we can useTwo Inside the Class C but Obj_c.Two is not accessible
• private members are private only inside the class and cannot be used outside any
classes. We cannot use memberThree not Obj_c.Three is notValid.
Inheritance
//Base class or Parent class.
public class Shape
{
public Shape(double Length , double Breadth)
{
this.Length=Length;
this.Breadth=Breadth
}
protected double Length;
protected double Breadth;
public void ShowDim()
{
Console.WriteLine(“Length and Breadth” +
Length + " and " + Breadth);
}
}
//Derived class or Child class.
public class Rectangle : Shape
{
public Rectangle (double Length , double
Breadth) : base( Length , Breadth)
{
}
public double Area()
{
return Length * Breadth;
}
public double Perimeter()
{
return 2* ( Length + Breadth );
}
}
Polymorphism
• Polymorphism refers to the ability to present the same interface for
different forms.
• Polymorphism is often expressed as 'one interface, multiple functions’.
• Polymorphism can be static or dynamic.
• Static : the response to a function is determined at the compile time.
• Dynamic : it is decided at run-time.
• Static Polymorphism
• Function overloading
• Operator overloading
• Dynamic polymorphism is implemented by abstract classes and virtual
functions.
Polymorphism
• Dynamic polymorphism is implemented by abstract classes and virtual
functions.
• When you have a function defined in a class that you want to be implemented in an
inherited class(es), you use virtual/abstract functions.
• The virtual functions could be implemented differently in different inherited class and
the call to these functions will be decided at runtime.
• The derived class needs to be overridden in case of any changes to be done from base
classes.
Polymorphism
abstract class Shape {
public abstract int area();
}
--OR--
public class Shape {
public virtual int area() {
return 0;
}
}
public class Rectangle: Shape {
private int length;
private int width;
public Rectangle( int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area () {
Console.WriteLine("Rectangle class area
:");
return (width * length);
}
}
Interface And Abstract Classes
Interface Abstract Class
Supports multiple inheritance Does not support multiple inheritance
Does not contain data members Contains data members
No Constructors Contains constructors
Contains only signature of members Contains both complete and incomplete members
Cannot have access modifiers (all assumed public) Can have access modifiers for the functions,
members
Members cannot be static Only complete members can be static
Interface
• An interface is a contract
• An interface is an empty shell
• Implementing an interface consumes very little CPU
// A motor vehicles should look like this:
interface MotorVehicle
{
void run();
int getFuel();
}
Abstract Class
• Abstract classes, unlike interfaces, are classes
• More expensive to use, because there is a look-up to do when you inherit
from them.
• You can define a behavior for them.
abstract class MotorVehicle{
int fuel;
int getFuel() => fuel;
abstract void run();
}
Extending a Class
• Extension methods enable you to "add" methods to existing types without
creating a new derived type.
• Extension methods are a special kind of static method.
• They are called as if they were instance methods on the extended type.
• Define a static class to contain the extension method.
• Implement the extension method as a static method with at least the same
visibility as the containing class.
• The first parameter of the method specifies the type that the method
operates on; it must be preceded with the this modifier.
• Call the methods as if they were instance methods on the type.
Extending a Class
public static class MyExtensions {
public static int WordCount(this String str,<parameters>){
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
string s = "Hello WebinarforallNepal";
int i = s.WordCount(<parameters>);
Thank you!

More Related Content

PDF
Introduction to java and oop
baabtra.com - No. 1 supplier of quality freshers
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPTX
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
PPTX
Introduction to oop and java fundamentals
AnsgarMary
 
PPT
Object Oriented Programming In .Net
Greg Sohl
 
PPT
Java oops PPT
kishu0005
 
PPTX
Packages and Interfaces
AkashDas112
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
03 classes interfaces_principlesofoop
Vladislav Hadzhiyski
 
Introduction to oop and java fundamentals
AnsgarMary
 
Object Oriented Programming In .Net
Greg Sohl
 
Java oops PPT
kishu0005
 
Packages and Interfaces
AkashDas112
 

What's hot (18)

PDF
Object Oriented Programming using C++ Part II
Ajit Nayak
 
PPTX
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
PDF
JAVA PROGRAMMING – Packages - Stream based I/O
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPT
Classes and data abstraction
Hoang Nguyen
 
PDF
Java Programming Paradigms Chapter 1
Sakthi Durai
 
PPTX
Vb ch 3-object-oriented_fundamentals_in_vb.net
bantamlak dejene
 
PDF
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Object Oriented Programming with C#
foreverredpb
 
PPTX
OOP Unit 2 - Classes and Object
dkpawar
 
PPT
Inheritance
abhay singh
 
PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
DOCX
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
PPTX
Object oriented programming in java
Elizabeth alexander
 
PPTX
01 Java Language And OOP PART I
Hari Christian
 
PDF
Object Oriented Programming using JAVA Notes
Uzair Salman
 
PPT
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
PPTX
OOPS – General Understanding in .NET
Sabith Byari
 
Object Oriented Programming using C++ Part II
Ajit Nayak
 
Unit3 part3-packages and interfaces
DevaKumari Vijay
 
JAVA PROGRAMMING – Packages - Stream based I/O
Jyothishmathi Institute of Technology and Science Karimnagar
 
Classes and data abstraction
Hoang Nguyen
 
Java Programming Paradigms Chapter 1
Sakthi Durai
 
Vb ch 3-object-oriented_fundamentals_in_vb.net
bantamlak dejene
 
Core java complete notes - Contact at +91-814-614-5674
Lokesh Kakkar Mobile No. 814-614-5674
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Object Oriented Programming with C#
foreverredpb
 
OOP Unit 2 - Classes and Object
dkpawar
 
Inheritance
abhay singh
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Object oriented programming in java
Elizabeth alexander
 
01 Java Language And OOP PART I
Hari Christian
 
Object Oriented Programming using JAVA Notes
Uzair Salman
 
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 
OOPS – General Understanding in .NET
Sabith Byari
 
Ad

Similar to Better Understanding OOP using C# (20)

PDF
Object-oriented Analysis, Design & Programming
Allan Mangune
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
PDF
LectureNotes-02-DSA
Haitham El-Ghareeb
 
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
vmickey4522
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
Java-Intro.pptx
VijalJain3
 
PPTX
VB.net&OOP.pptx
BharathiLakshmiAAssi
 
PPTX
Introduction to oop
colleges
 
PPT
Md02 - Getting Started part-2
Rakesh Madugula
 
PPTX
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
babasahebgaikwad8
 
PPTX
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
PPTX
Unit3 packages &amp; interfaces
Kalai Selvi
 
PPTX
CSharp presentation and software developement
frwebhelp
 
PDF
Btech chapter notes batcha ros zir skznzjsbaajz z
bhatiaanushka101
 
PPTX
Introduction to Design Patterns in Javascript
Santhosh Kumar Srinivasan
 
PPTX
Object oriented programming
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
04 Java Language And OOP Part IV
Hari Christian
 
PPT
C++Presentation 2.PPT
VENARATEKANHURU
 
Object-oriented Analysis, Design & Programming
Allan Mangune
 
Pi j3.2 polymorphism
mcollison
 
C++ & Data Structure - Unit - first.pptx
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY
 
LectureNotes-02-DSA
Haitham El-Ghareeb
 
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
vmickey4522
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java-Intro.pptx
VijalJain3
 
VB.net&OOP.pptx
BharathiLakshmiAAssi
 
Introduction to oop
colleges
 
Md02 - Getting Started part-2
Rakesh Madugula
 
Unit - I Intro. to OOP Concepts and Control Structure -OOP and CG (2024 Patte...
babasahebgaikwad8
 
OOP_in_CPP_Animesh_Animated_Diagram.pptx
animesh713331
 
Unit3 packages &amp; interfaces
Kalai Selvi
 
CSharp presentation and software developement
frwebhelp
 
Btech chapter notes batcha ros zir skznzjsbaajz z
bhatiaanushka101
 
Introduction to Design Patterns in Javascript
Santhosh Kumar Srinivasan
 
04 Java Language And OOP Part IV
Hari Christian
 
C++Presentation 2.PPT
VENARATEKANHURU
 
Ad

More from Chandan Gupta Bhagat (20)

PPTX
Unit 3 - URLs and URIs
Chandan Gupta Bhagat
 
PPTX
Unit 2 : Internet Address
Chandan Gupta Bhagat
 
PPTX
Unit 7 : Network Security
Chandan Gupta Bhagat
 
PPTX
Unit 6 : Application Layer
Chandan Gupta Bhagat
 
PPTX
Unit 5 : Transport Layer
Chandan Gupta Bhagat
 
PPTX
Unit 4 - Network Layer
Chandan Gupta Bhagat
 
PPTX
Unit 3 - Data Link Layer - Part B
Chandan Gupta Bhagat
 
PPTX
Unit 3 - Data Link Layer - Part A
Chandan Gupta Bhagat
 
PPTX
Computer Network - Unit 2
Chandan Gupta Bhagat
 
PPTX
Computer Network - Unit 1
Chandan Gupta Bhagat
 
PPTX
Efficient Docker Image | MS Build Kathmandu
Chandan Gupta Bhagat
 
PPTX
Parytak sahayatri
Chandan Gupta Bhagat
 
PPTX
Developing windows 8 apps
Chandan Gupta Bhagat
 
PPTX
IOE assessment marks and attendance system
Chandan Gupta Bhagat
 
PPTX
BLOGGING
Chandan Gupta Bhagat
 
PPTX
Oblique parallel projection
Chandan Gupta Bhagat
 
PPTX
Brainstorming session
Chandan Gupta Bhagat
 
PPTX
Presentation of 3rd Semester C++ Project
Chandan Gupta Bhagat
 
Unit 3 - URLs and URIs
Chandan Gupta Bhagat
 
Unit 2 : Internet Address
Chandan Gupta Bhagat
 
Unit 7 : Network Security
Chandan Gupta Bhagat
 
Unit 6 : Application Layer
Chandan Gupta Bhagat
 
Unit 5 : Transport Layer
Chandan Gupta Bhagat
 
Unit 4 - Network Layer
Chandan Gupta Bhagat
 
Unit 3 - Data Link Layer - Part B
Chandan Gupta Bhagat
 
Unit 3 - Data Link Layer - Part A
Chandan Gupta Bhagat
 
Computer Network - Unit 2
Chandan Gupta Bhagat
 
Computer Network - Unit 1
Chandan Gupta Bhagat
 
Efficient Docker Image | MS Build Kathmandu
Chandan Gupta Bhagat
 
Parytak sahayatri
Chandan Gupta Bhagat
 
Developing windows 8 apps
Chandan Gupta Bhagat
 
IOE assessment marks and attendance system
Chandan Gupta Bhagat
 
Oblique parallel projection
Chandan Gupta Bhagat
 
Brainstorming session
Chandan Gupta Bhagat
 
Presentation of 3rd Semester C++ Project
Chandan Gupta Bhagat
 

Recently uploaded (20)

PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PPTX
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
PPTX
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
PDF
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PDF
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
EU POPs Limits & Digital Product Passports Compliance Strategy 2025.pptx
Certivo Inc
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
Build Multi-agent using Agent Development Kit
FadyIbrahim23
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
AZ900_SLA_Pricing_2025_LondonIT (1).pptx
chumairabdullahph
 
Save Business Costs with CRM Software for Insurance Agents
Insurance Tech Services
 
Become an Agentblazer Champion Challenge Kickoff
Dele Amefo
 
Materi_Pemrograman_Komputer-Looping.pptx
RanuFajar1
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pdf
Certivo Inc
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
EU POPs Limits & Digital Product Passports Compliance Strategy 2025.pptx
Certivo Inc
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Better Understanding OOP using C#

  • 1. Better Understanding of OOP Using C# Chandan Gupta Bhagat
  • 2. “Write shy code - modules that don't reveal anything unnecessary to other modules and that don't rely on other modules' implementations.” Andy Hunt & DaveThomas
  • 3. General Overview • Classes and Objects • Functions • Constructors • Destructors • Other Members
  • 4. Classes and Objects • Classes are blueprints for Object. • Objects are instance of classes. using System; namespace NameSpace { <access_modifier> class ClassName { <access_modifier> DataMembers Constructors <access_modifier> <return_type> Functions Destructors } }
  • 5. Classes and Objects Access Modifiers •public •private •protected •internal •protected internal
  • 6. Classes and Objects MyClass myObject_One=new MyClass(); MyClass myObject_Two=new MyClass(); Or var myObject_One=new MyClass(); var myObject_Two=new MyClass();
  • 7. Functions <access_modifier> <return_type> Functions (<Parameters>) { //Function Logic codes here } • return_type is void => write return; or don’t write anything • return_type is other than void => return object of the return_type • Parameters can be of any number • Function Overloading (inside the same class with same name but different signature): • Add(int a, int b) => a + b; • Add(int a, int b, int c) =>a + b + c;
  • 8. Constructors • a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. public class ClassName { private int _number; public ClassName() //parameterless constructor { _number=10; } public ClassName(int number) //parameterized constructor{ _number=number; } } ClassName myObject_One=new MyClass(); //the variable number has the value 10 ClassName myObject_Two=new MyClass(2); //the variable number has the value 2
  • 9. Destructors ~ClassName() { } • Destructors are invoked automatically, and cannot be invoked explicitly. • Destructors cannot be overloaded.Thus, a class can have, at most, one destructor. • Destructors are not inherited.Thus, a class has no destructors other than the one, which may be declared in it. • Destructors cannot be used with structs.They are only used with classes. • An instance becomes eligible for destruction when it is no longer possible for any code to use the instance. • Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction. • When an instance is destructed, the destructors in its inheritance chain are called, in order, from most derived to least derived.
  • 10. Data Members • Constants representing constant values • Fields representing variables • Properties that define the class features and include actions to fetch and modify them • Events generated to communicate between different classes /objects • Indexers that help in accessing class instances similar to arrays • Operators that define semantics when used in expressions with class instances • Static constructor to initialize the class itself • Types that are local to the class (nested type)
  • 12. Encapsulation • Encapsulation is the process of hiding irrelevant data from the user. • These are the irrelevant information for the mobile user, that’s why it is encapsulated inside a cabinet. • To understand encapsulation, consider an example of a mobile phone. Whenever you buy a mobile, you don’t see how circuit board works.You are also not interested to know how digital signal converts into the analog signal and vice versa.
  • 13. Encapsulation public class Rectangle { private float _length; private float _breadth; public float Area { get{return _length * _breadth;} } public float Perimeter { get{return 2*(_length + _breadth);} } } • The Encapsulation fields of a class basically can be write-only or can be read-only. • A Encapsulated class can have control over in its fields. • A class can change data type of its fields anytime but users of this class do not need to change any code.
  • 14. Abstraction • Abstraction is just opposite of Encapsulation. • Abstraction is a mechanism to show only relevant data to the user. • It is an abstraction because you are seeing only relevant information instead of their internal engineering. • Consider the same mobile example again. Whenever you buy a mobile phone, you see their different types of functionalities as a camera, mp3 player, calling function, recording function, multimedia etc.
  • 15. Abstraction public abstract class Shape { private float _area; private float _perimeter; public float Area { get{return _area;} set{_area = value;} } public float Perimeter { get{return _perimeter;} set{_perimeter = value;} } public abstract void CalculateArea(); public abstract void CalculatePerimeter(); }
  • 16. Inheritance • Allows us to define a class in terms of another class, which makes it easier to create and maintain an application. • Provides an opportunity to reuse the code functionality and speeds up implementation time. • inherit data and functions from multiple base classes or interfaces. • Classes in Inheritance • Base Class :The Origin Classes • Derived Classes : Derived from base class • Sealed Classes :This classes are the last of their kind. Cannot derived further
  • 17. Inheritance • Example : • class A is the origin class having the public member One, protected memberTwo and private memberThree • class B : A this means B is derived from A having public member Four • class C : B this means C is derived from B • sealed class D : A Now D is derived from A and D is last of its species. • In this example, when we create an instance of C “Obj_c”, the constructor of hierarchy A is called first, then B and then C. • public members are accessible from the object of C, i.e. Member One can be accessible. We can use Obj_c.One • Protected members are public inside the class C but not accessible outside the Object of C. i.e. we can useTwo Inside the Class C but Obj_c.Two is not accessible • private members are private only inside the class and cannot be used outside any classes. We cannot use memberThree not Obj_c.Three is notValid.
  • 18. Inheritance //Base class or Parent class. public class Shape { public Shape(double Length , double Breadth) { this.Length=Length; this.Breadth=Breadth } protected double Length; protected double Breadth; public void ShowDim() { Console.WriteLine(“Length and Breadth” + Length + " and " + Breadth); } } //Derived class or Child class. public class Rectangle : Shape { public Rectangle (double Length , double Breadth) : base( Length , Breadth) { } public double Area() { return Length * Breadth; } public double Perimeter() { return 2* ( Length + Breadth ); } }
  • 19. Polymorphism • Polymorphism refers to the ability to present the same interface for different forms. • Polymorphism is often expressed as 'one interface, multiple functions’. • Polymorphism can be static or dynamic. • Static : the response to a function is determined at the compile time. • Dynamic : it is decided at run-time. • Static Polymorphism • Function overloading • Operator overloading • Dynamic polymorphism is implemented by abstract classes and virtual functions.
  • 20. Polymorphism • Dynamic polymorphism is implemented by abstract classes and virtual functions. • When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual/abstract functions. • The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime. • The derived class needs to be overridden in case of any changes to be done from base classes.
  • 21. Polymorphism abstract class Shape { public abstract int area(); } --OR-- public class Shape { public virtual int area() { return 0; } } public class Rectangle: Shape { private int length; private int width; public Rectangle( int a = 0, int b = 0) { length = a; width = b; } public override int area () { Console.WriteLine("Rectangle class area :"); return (width * length); } }
  • 22. Interface And Abstract Classes Interface Abstract Class Supports multiple inheritance Does not support multiple inheritance Does not contain data members Contains data members No Constructors Contains constructors Contains only signature of members Contains both complete and incomplete members Cannot have access modifiers (all assumed public) Can have access modifiers for the functions, members Members cannot be static Only complete members can be static
  • 23. Interface • An interface is a contract • An interface is an empty shell • Implementing an interface consumes very little CPU // A motor vehicles should look like this: interface MotorVehicle { void run(); int getFuel(); }
  • 24. Abstract Class • Abstract classes, unlike interfaces, are classes • More expensive to use, because there is a look-up to do when you inherit from them. • You can define a behavior for them. abstract class MotorVehicle{ int fuel; int getFuel() => fuel; abstract void run(); }
  • 25. Extending a Class • Extension methods enable you to "add" methods to existing types without creating a new derived type. • Extension methods are a special kind of static method. • They are called as if they were instance methods on the extended type. • Define a static class to contain the extension method. • Implement the extension method as a static method with at least the same visibility as the containing class. • The first parameter of the method specifies the type that the method operates on; it must be preceded with the this modifier. • Call the methods as if they were instance methods on the type.
  • 26. Extending a Class public static class MyExtensions { public static int WordCount(this String str,<parameters>){ return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length; } } string s = "Hello WebinarforallNepal"; int i = s.WordCount(<parameters>);

Editor's Notes

  • #24: An interface is a contract: The person writing the interface says, "hey, I accept things looking that way", and the person using the interface says "OK, the class I write looks that way". An interface is an empty shell. There are only the signatures of the methods, which implies that the methods do not have a body. The interface can't do anything. It's just a pattern.
  • #25: It's more about a person saying, "these classes should look like that, and they have that in common, so fill in the blanks!".