SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante 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 c#
Introduction to c#Introduction to c#
Introduction to c#singhadarsh
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 
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
 

Semelhante 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 c#
Introduction to c#Introduction to c#
Introduction to c#
 
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 (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

Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Último (20)

Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.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 do 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.