SlideShare una empresa de Scribd logo
1 de 23
Agenda
Object-Oriented Concepts
Abstraction and Encapsulation
Inheritance
Polymorphism
Interfaces and Abstract Classes
Virtual Methods
Classes
Sealed Classes
C# programming with examples
 Encapsulation and abstraction is the advanced
mechanism in C# that lets your program to hide
unwanted code within a capsule and shows only
essential features of an object.
 Encapsulation is used to hide its members from outside
class or interface, whereas abstraction is used to show
only essential features.
 Access Modifier – Public, Private, Protected, Internal &
Protected Internal
Key Object-Oriented Concepts
Foreach
 using System;
namespace foreach_loop
{
class Program
{
static void Main(string[] args)
{
string[] arr = new string[5]; // declaring array
//Storing value in array element
arr[0] = "Steven";
arr[1] = "Clark";
arr[2] = "Mark";
arr[3] = "Thompson";
arr[4] = "John";
//retrieving value using foreach loop
foreach (string name in arr)
{
Console.WriteLine("Hello " + name);
}
Console.ReadLine();
}
}
}
switch case
switch (opt)
{
case 1:
result = num1 + num2;
Console.WriteLine("n{0} + {1} = {2}", num1, num2, result);
break;
case 2:
result = num1 - num2;
Console.WriteLine("n{0} - {1} = {2}", num1, num2, result);
break;
default:
Console.WriteLine("nInvalid option.Please try again.");
Break;
}
Array
using System;
namespace Declare_Array
{
class Program
{
static void Main(string[] args)
{
int[] num = new int[6]; //Declaring Array
//Initializing array
num[0] = 6;
num[1] = 23;
num[2] = 12;
num[3] = 9;
num[4] = 14;
num[5] = 52;
//Showing value of Array
Console.WriteLine("1st value:t{0}", num[0]);
Console.WriteLine("2nd value:t{0}", num[1]);
Console.WriteLine("3rd value:t{0}", num[2]);
Console.WriteLine("4th value:t{0}", num[3]);
Console.WriteLine("5th value:t{0}", num[4]);
Console.WriteLine("6th value:t{0}", num[5]);
Console.ReadLine();
}
}
}
Data Types
 C# is a strongly typed language. It means, that you cannot use
variable without data types.
 Data types tell the compiler that which type of data is used for
processing.
 Such as if you want to work with string value then you will have to
assign string type variable to work with.
 C# provides two types of data types: Value types and Reference types.
 A Value type data type stores copy of the value whereas the Reference
typedata types stores the address of the value.
Value types
Data
Types
Size Values
sbyte 8 bit -128 to 127
byte 8 bit 0 to 255
short 16 bit -32,768 to 32,767
ushort 16 bit 0 to 65,535
int 32 bit
-2,147,483,648 to
2,147,483,647
uint 32 bit 0 to 4,294,967,295
long 64 bit
-9,223,372,036,854,775,808
to 9,223,372,036,854,775,807
ulong 64 bit
0 to
18,446,744,073,709,551,615
char 16 bit 0 to 65535
float 32 bit -1.5 x 1045 to 3.4 x 1038
double 64 bit -5 x 10324 to 1.7 x 10308
decimal 128 bit -1028 to 7.9 x 1028
bool --- True or false
Reference Types
Data Types Size Values
string
Variable
length
0-2 billion Unicode characters
object --- ---
Boxing and Unboxing
class Test
{
static void Main()
{
int i = 1;
object o = i; // boxing
int j = (int) o; // unboxing
}
}
Overloading and Overriding
class Person
{
private String firstName;
private String lastName;
Person()
{
this.firstName = "";
this.lastName = "";
}
Person(String FirstName)
{
this.firstName = FirstName;
this.lastName = "";
}
Person(String FirstName, String LastName)
{
this.firstName = FirstName;
this.lastName = LastName;
}
}
Calling overloading method
 Person(); // as a constructor and call method
without parameter
 Person(userFirstName); // as a constructor and
call method with one parameter(like User's first
Name)
 Person(userFirstName,userLastName); // as a
constructor and call method with one
parameter(like User's first Name)
Polymorphism
Polymorphism provides following features:
It allows you to invoke methods of derived class through base class reference during runtime.
It has the ability for classes to provide different implementations of methods that are called through the same name.
Polymorphism is of two types:
Compile time polymorphism/Overloading
Runtime polymorphism/Overriding
Polymorphism
Compile Time Polymorphism
 
Compile time polymorphism is method and operators overloading. It is also called early binding.
 
In method overloading method performs the different task at the different input parameters.
 
Runtime Time Polymorphism
 
Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It 
is also called late binding.
 
“When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply
involves having another method with the same prototype.”
Interfaces
 An interface defines a contract
 An interface is a type
 Includes methods, properties, indexers, events
 Any class or struct implementing an interface must support all parts of the contract
 Interfaces provide no implementation
 When a class or struct implements an interface it must provide the implementation
 Interfaces provide polymorphism
 Many classes and structs may implement 
a particular interface
public interface IDelete {
void Delete();
}
public class TextBox : IDelete {
public void Delete() { ... }
}
public class Car : IDelete {
public void Delete() { ... }
}
TextBox tb = new TextBox();
IDelete iDel = tb;
iDel.Delete();
Car c = new Car();
iDel = c;
iDel.Delete();
Interfaces
Example
interface IControl {
void Paint();
}
interface IListBox: IControl {
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {
}
Interfaces
Multiple Inheritance
 Classes and structs can inherit from 
multiple interfaces
 Interfaces can inherit from multiple interfaces
interface IControl {
void Delete();
}
interface IListBox: IControl {
void Delete();
}
interface IComboBox: ITextBox, IListBox {
void IControl.Delete();
void IListBox.Delete();
}
Interfaces
Explicit Interface Members
 If two interfaces have the same method name, 
you can explicitly specify interface + method 
name to disambiguate their implementations
Classes and Structs
Similarities
 Both are user-defined types
 Both can implement multiple interfaces
 Both can contain
 Data 
 Fields, constants, events, arrays
 Functions 
 Methods, properties, indexers, operators, constructors
 Type definitions
 Classes, structs, enums, interfaces, delegates
Class Struct
Reference type Value type
Can inherit from any
non-sealed reference type
No inheritance
(inherits only from System.ValueType)
Can have a destructor No destructor
Can have user-defined
parameterless constructor
No user-defined parameterless
constructor
Classes and Structs
Differences
Classes and Structs
Access Modifiers
 Access modifiers specify who can use a type or
a member
 Access modifiers control encapsulation
 Top-level types (those directly in a namespace)
can be public or internal
 Class members can be public, private,
protected, internal, or
protected internal
 Struct members can be public, private or
internal
If the access
modifier is
Then a member defined in type
T and assembly A is accessible
public to everyone
private within T only (the default)
protected to T or types derived from T
internal to types within A
protected
internal
to T or types derived from T
or to types within A
Classes and Structs
Access Modifiers
Classes and Structs
Abstract Classes
 An abstract class is one that cannot
be instantiated
 Intended to be used as a base class
 May contain abstract and non-abstract
function members
 Similar to an interface
 Cannot be sealed
Classes and Structs
Sealed Classes
 A sealed class is one that cannot be used as a
base class
 Sealed classes can’t be abstract
 All structs are implicitly sealed
 Why seal a class?
 To prevent unintended derivation
 Code optimization
 Virtual function calls can be resolved at compile-time

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Templates
TemplatesTemplates
Templates
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
class and objects
class and objectsclass and objects
class and objects
 
C++ oop
C++ oopC++ oop
C++ oop
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
11. Objects and Classes
11. Objects and Classes11. Objects and Classes
11. Objects and Classes
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
C sharp chap5
C sharp chap5C sharp chap5
C sharp chap5
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
14. Java defining classes
14. Java defining classes14. Java defining classes
14. Java defining classes
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
OOP
OOPOOP
OOP
 
C sharp part 001
C sharp part 001C sharp part 001
C sharp part 001
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Op ps
Op psOp ps
Op ps
 

Destacado

Fluent interface in c#
Fluent interface in c#Fluent interface in c#
Fluent interface in c#Dror Helper
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental PrinciplesIntro C# Book
 
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Akhil Mittal
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritanceHariz Mustafa
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingHariz Mustafa
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programmingHariz Mustafa
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphismHariz Mustafa
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesSami Mut
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 

Destacado (11)

Fluent interface in c#
Fluent interface in c#Fluent interface in c#
Fluent interface in c#
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
 
Lecture03 inheritance
Lecture03 inheritanceLecture03 inheritance
Lecture03 inheritance
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
 
Lecture04 polymorphism
Lecture04 polymorphismLecture04 polymorphism
Lecture04 polymorphism
 
1.Philosophy of .NET
1.Philosophy of .NET1.Philosophy of .NET
1.Philosophy of .NET
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slides
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Build Features, Not Apps
Build Features, Not AppsBuild Features, Not Apps
Build Features, Not Apps
 

Similar a OO Concepts: Abstraction, Encapsulation, Inheritance

Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plusSayed Ahmed
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Sayed Ahmed
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)PROIDEA
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#singhadarsh
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharpSDFG5
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...NALESVPMEngg
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.pptAlmamoon
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.pptmothertheressa
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And EnumsBhushan Mulmule
 
Advanced c#
Advanced c#Advanced c#
Advanced c#saranuru
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Sachin Singh
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpRaga Vahini
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpSatish Verma
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharpsinghadarsh
 

Similar a OO Concepts: Abstraction, Encapsulation, Inheritance (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Bc0037
Bc0037Bc0037
Bc0037
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
C#ppt
C#pptC#ppt
C#ppt
 
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
4Developers 2018: Ile (nie) wiesz o strukturach w .NET (Łukasz Pyrzyk)
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Introduction to-csharp
Introduction to-csharpIntroduction to-csharp
Introduction to-csharp
 
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
Introduction to Csharp (C-Sharp) is a programming language developed by Micro...
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
Introduction-to-Csharp.ppt
Introduction-to-Csharp.pptIntroduction-to-Csharp.ppt
Introduction-to-Csharp.ppt
 
unit 1 (1).pptx
unit 1 (1).pptxunit 1 (1).pptx
unit 1 (1).pptx
 
Arrays, Structures And Enums
Arrays, Structures And EnumsArrays, Structures And Enums
Arrays, Structures And Enums
 
Advanced c#
Advanced c#Advanced c#
Advanced c#
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 
Introduction to csharp
Introduction to csharpIntroduction to csharp
Introduction to csharp
 

Último

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 

Último (20)

INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 

OO Concepts: Abstraction, Encapsulation, Inheritance

  • 1. Agenda Object-Oriented Concepts Abstraction and Encapsulation Inheritance Polymorphism Interfaces and Abstract Classes Virtual Methods Classes Sealed Classes C# programming with examples
  • 2.  Encapsulation and abstraction is the advanced mechanism in C# that lets your program to hide unwanted code within a capsule and shows only essential features of an object.  Encapsulation is used to hide its members from outside class or interface, whereas abstraction is used to show only essential features.  Access Modifier – Public, Private, Protected, Internal & Protected Internal Key Object-Oriented Concepts
  • 3. Foreach  using System; namespace foreach_loop { class Program { static void Main(string[] args) { string[] arr = new string[5]; // declaring array //Storing value in array element arr[0] = "Steven"; arr[1] = "Clark"; arr[2] = "Mark"; arr[3] = "Thompson"; arr[4] = "John"; //retrieving value using foreach loop foreach (string name in arr) { Console.WriteLine("Hello " + name); } Console.ReadLine(); } } }
  • 4. switch case switch (opt) { case 1: result = num1 + num2; Console.WriteLine("n{0} + {1} = {2}", num1, num2, result); break; case 2: result = num1 - num2; Console.WriteLine("n{0} - {1} = {2}", num1, num2, result); break; default: Console.WriteLine("nInvalid option.Please try again."); Break; }
  • 5. Array using System; namespace Declare_Array { class Program { static void Main(string[] args) { int[] num = new int[6]; //Declaring Array //Initializing array num[0] = 6; num[1] = 23; num[2] = 12; num[3] = 9; num[4] = 14; num[5] = 52; //Showing value of Array Console.WriteLine("1st value:t{0}", num[0]); Console.WriteLine("2nd value:t{0}", num[1]); Console.WriteLine("3rd value:t{0}", num[2]); Console.WriteLine("4th value:t{0}", num[3]); Console.WriteLine("5th value:t{0}", num[4]); Console.WriteLine("6th value:t{0}", num[5]); Console.ReadLine(); } } }
  • 6. Data Types  C# is a strongly typed language. It means, that you cannot use variable without data types.  Data types tell the compiler that which type of data is used for processing.  Such as if you want to work with string value then you will have to assign string type variable to work with.  C# provides two types of data types: Value types and Reference types.  A Value type data type stores copy of the value whereas the Reference typedata types stores the address of the value.
  • 7. Value types Data Types Size Values sbyte 8 bit -128 to 127 byte 8 bit 0 to 255 short 16 bit -32,768 to 32,767 ushort 16 bit 0 to 65,535 int 32 bit -2,147,483,648 to 2,147,483,647 uint 32 bit 0 to 4,294,967,295 long 64 bit -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong 64 bit 0 to 18,446,744,073,709,551,615 char 16 bit 0 to 65535 float 32 bit -1.5 x 1045 to 3.4 x 1038 double 64 bit -5 x 10324 to 1.7 x 10308 decimal 128 bit -1028 to 7.9 x 1028 bool --- True or false
  • 8. Reference Types Data Types Size Values string Variable length 0-2 billion Unicode characters object --- ---
  • 9. Boxing and Unboxing class Test { static void Main() { int i = 1; object o = i; // boxing int j = (int) o; // unboxing } }
  • 10. Overloading and Overriding class Person { private String firstName; private String lastName; Person() { this.firstName = ""; this.lastName = ""; } Person(String FirstName) { this.firstName = FirstName; this.lastName = ""; } Person(String FirstName, String LastName) { this.firstName = FirstName; this.lastName = LastName; } }
  • 11. Calling overloading method  Person(); // as a constructor and call method without parameter  Person(userFirstName); // as a constructor and call method with one parameter(like User's first Name)  Person(userFirstName,userLastName); // as a constructor and call method with one parameter(like User's first Name)
  • 12. Polymorphism Polymorphism provides following features: It allows you to invoke methods of derived class through base class reference during runtime. It has the ability for classes to provide different implementations of methods that are called through the same name. Polymorphism is of two types: Compile time polymorphism/Overloading Runtime polymorphism/Overriding
  • 13. Polymorphism Compile Time Polymorphism   Compile time polymorphism is method and operators overloading. It is also called early binding.   In method overloading method performs the different task at the different input parameters.   Runtime Time Polymorphism   Runtime time polymorphism is done using inheritance and virtual functions. Method overriding is called runtime polymorphism. It  is also called late binding.   “When overriding a method, you change the behavior of the method for the derived class. Overloading a method simply involves having another method with the same prototype.”
  • 14. Interfaces  An interface defines a contract  An interface is a type  Includes methods, properties, indexers, events  Any class or struct implementing an interface must support all parts of the contract  Interfaces provide no implementation  When a class or struct implements an interface it must provide the implementation  Interfaces provide polymorphism  Many classes and structs may implement  a particular interface
  • 15. public interface IDelete { void Delete(); } public class TextBox : IDelete { public void Delete() { ... } } public class Car : IDelete { public void Delete() { ... } } TextBox tb = new TextBox(); IDelete iDel = tb; iDel.Delete(); Car c = new Car(); iDel = c; iDel.Delete(); Interfaces Example
  • 16. interface IControl { void Paint(); } interface IListBox: IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox { } Interfaces Multiple Inheritance  Classes and structs can inherit from  multiple interfaces  Interfaces can inherit from multiple interfaces
  • 17. interface IControl { void Delete(); } interface IListBox: IControl { void Delete(); } interface IComboBox: ITextBox, IListBox { void IControl.Delete(); void IListBox.Delete(); } Interfaces Explicit Interface Members  If two interfaces have the same method name,  you can explicitly specify interface + method  name to disambiguate their implementations
  • 18. Classes and Structs Similarities  Both are user-defined types  Both can implement multiple interfaces  Both can contain  Data   Fields, constants, events, arrays  Functions   Methods, properties, indexers, operators, constructors  Type definitions  Classes, structs, enums, interfaces, delegates
  • 19. Class Struct Reference type Value type Can inherit from any non-sealed reference type No inheritance (inherits only from System.ValueType) Can have a destructor No destructor Can have user-defined parameterless constructor No user-defined parameterless constructor Classes and Structs Differences
  • 20. Classes and Structs Access Modifiers  Access modifiers specify who can use a type or a member  Access modifiers control encapsulation  Top-level types (those directly in a namespace) can be public or internal  Class members can be public, private, protected, internal, or protected internal  Struct members can be public, private or internal
  • 21. If the access modifier is Then a member defined in type T and assembly A is accessible public to everyone private within T only (the default) protected to T or types derived from T internal to types within A protected internal to T or types derived from T or to types within A Classes and Structs Access Modifiers
  • 22. Classes and Structs Abstract Classes  An abstract class is one that cannot be instantiated  Intended to be used as a base class  May contain abstract and non-abstract function members  Similar to an interface  Cannot be sealed
  • 23. Classes and Structs Sealed Classes  A sealed class is one that cannot be used as a base class  Sealed classes can’t be abstract  All structs are implicitly sealed  Why seal a class?  To prevent unintended derivation  Code optimization  Virtual function calls can be resolved at compile-time

Notas del editor

  1. The term object sometimes refers to an instance and sometimes to a class. It’s meaning can usually be determined by context.
  2. An interface is a well-defined set of methods (functions). Interfaces do not contain data members. Type is different from class. The type specifies the interfaces, the class specifies the implementation.
  3. Polymorphism is achieved through: Inheritance: a base type can have multiple derived types Interfaces: multiple types can implement a given interface Late binding: you can use any object, as long as it implements the methods you want to call. In C# you can use reflection to dynamically determine if an object implements a method, and then call it. Interfaces can also be used in a late-bound manner. In order to handle multiple types without polymorphism you have to write conditional code (using if or switch statements) that tests the type of an instance and then runs the appropriate code. Such code is brittle and not easily extended. Many Object-Oriented design concepts are motivated by minimizing dependencies. You want to be able to develop independent modules, so that making a change to one doesn’t force you to have to go back and change others.
  4. In scenarios where completely different objects need to support some kind of shared functionality like, let’s say, persist to XML, classes can implement interfaces that make them compatible with even if they don’t share the same base class. This provides most of the benefits of multiple class inheritance without the nasty side-effects that this usually brings. Interface members are implicitly public and abstract.
  5. If there’s no ambiguity then you do not have to specify the name of the interface.
  6. Classes and structs provide a way to create user-defined types.
  7. protected internal = protected OR internal. No way to define protected AND internal.