SlideShare a Scribd company logo
5. OOP
2 
Microsoft 
Objectives 
“Classes, objects and object-oriented programming (OOP) play a fundamental role in .NET. C# features full support for the object- oriented programming paradigm…” 
•Designing your own classes 
•Destroying objects and garbage collection 
•Inheritance 
•Interfaces
3 
Microsoft 
Part 1 
•Designing your own classes…
4 
Microsoft 
Motivation 
•.NET contains thousands of prebuilt classes in the FCL 
•So why design your own? 
–to model entities unique to your application domain… 
•Examples: 
–employees 
–customers 
–products 
–orders 
–documents 
–business units 
–etc.
5 
Microsoft 
Simple class members 
•C# supports standard fields, methods and constructors 
–with standard access control: public, private, protected 
public class Person { public string Name; // fields public int Age; public Person() // default constructor { this.Name = "?"; this.Age = -1; } public Person(string name, int age) // parameterized ctor { this.Name = name; this.Age = age; } public override string ToString() // method { return this.Name; } }//class
6 
Microsoft 
Basic design rules 
•Provide constructor(s) 
•Omit default constructor for parameterized initialization 
•Override ToString, Equals and GetHashCode 
•Data hiding: "hide as many details as you can" 
–enable access when necessary via accessors and mutators 
–.NET provides a cleaner mechanism via properties…
7 
Microsoft 
Properties 
•Goal: 
–to allow our class users to safely write code like this: 
–provides field-like access with method-like semantics… 
–… enabling access control, validation, data persistence, screen updating, etc. 
Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1;
8 
Microsoft 
Observation 
•Read of value ("Get") vs. Write of value ("Set") 
Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1; 
Get age 
Set age
9 
Microsoft 
Property implementation 
•Implementation options: 
–read-only 
–write-only 
–read-write 
public class Person { private string m_Name; private int m_Age; . . . public string Name { get { ... } } public int Age { get { ... } set { ... } } } 
read-only 
read-write
10 
Microsoft 
Example 
•Simplest implementation just reads / writes private field: 
public class Person { private string m_Name; private int m_Age; . . . public string Name // Name property { get { return this.m_Name; } } public int Age // Age property { get { return this.m_Age; } set { this.m_Age = value; } } }
11 
Microsoft 
Indexers 
•Enable array-like access with method-like semantics 
–great for data structure classes, collections, etc. 
People p; // collection of Person objects p = new People(); p[0] = new Person("joe hummel", 40); . . . age = p[0].Age; 
Set 
Get
12 
Microsoft 
Example 
•Implemented like properties, with Get and Set methods: 
public class People { private Person[] m_people; // underlying array . . . public Person this[int i] // int indexer { get { return this.m_people[i]; } set { this.m_people[i] = value; } } public Person this[string name] // string indexer { get { return ...; } } } 
read-only 
read-write
13 
Microsoft 
Part 2 
•Destroying objects and garbage collection…
14 
Microsoft 
Object creation and destruction 
•Objects are explicitly created via new 
•Objects are never explicitly destroyed! 
–.NET relies upon garbage collection to destroy objects 
–garbage collector runs unpredictably…
15 
Microsoft 
Finalization 
•Objects can be notified when they are garbage collected 
•Garbage collector (GC) will call object's finalizer 
public class Person 
{ 
. 
. 
. 
~Person() // finalizer 
{ 
... 
}
16 
Microsoft 
Should you rely upon finalization? 
•No! 
–it's unpredictable 
–it's expensive (.NET tracks object on special queue, etc.) 
•Alternatives? 
–design classes so that timely finalization is unnecessary 
–provide Close / Dispose method for class users to call 
** Warning ** As a .NET programmer, you are responsible for calling Dispose / Close. Rule of thumb: if you call Open, you need to call Close / Dispose for correct execution. Common examples are file I/O, database I/O, and XML processing.
17 
Microsoft 
Part 3 
•Inheritance…
18 
Microsoft 
Inheritance 
•Use in the small, when a derived class "is-a" base class 
–enables code reuse 
–enables design reuse & polymorphic programming 
•Example: 
–a Student is-a Person 
Undergraduate 
Person 
Student 
Employee 
Graduate 
Staff 
Faculty
19 
Microsoft 
Implementation 
•C# supports single inheritance 
–public inheritance only (C++ parlance) 
–base keyword gives you access to base class's members 
public class Student : Person { private int m_ID; public Student(string name, int age, int id) // constructor :base(name, age) { this.m_ID = id; } } 
Student 
Person
20 
Microsoft 
Binding 
•C# supports both static and dynamic binding 
–determined by absence or presence of virtual keyword 
–derived class must acknowledge with new or override 
public class Person { . . . // statically-bound public string HomeAddress() { … } // dynamically-bound public virtual decimal Salary() { … } } 
public class Student : Person { . . . public new string HomeAddress() { … } public override decimal Salary() { … } }
21 
Microsoft 
All classes inherit from System.Object 
StringArrayValueTypeExceptionDelegateClass1MulticastDelegateClass2Class3ObjectEnum1Structure1EnumPrimitive typesBooleanByteInt16Int32Int64CharSingleDoubleDecimalDateTimeSystem-defined typesUser-defined typesDelegate1TimeSpanGuid
22 
Microsoft 
Part 4 
•Interfaces…
23 
Microsoft 
Interfaces 
•An interface represents a design 
•Example: 
–the design of an object for iterating across a data structure 
–interface = method signatures only, no implementation details! 
–this is how foreach loop works… 
public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element }
24 
Microsoft 
Why use interfaces? 
•Formalize system design before implementation 
–especially helpful for PITL (programming in the large) 
•Design by contract 
–interface represents contract between client and object 
•Decoupling 
–interface specifies interaction between class A and B 
–by decoupling A from B, A can easily interact with C, D, …
25 
Microsoft 
.NET is heavily influenced by interfaces 
•IComparable 
•ICloneable 
•IDisposable 
•IEnumerable & IEnumerator 
•IList 
•ISerializable 
•IDBConnection, IDBCommand, IDataReader 
•etc.
26 
Microsoft 
Example 
•Sorting 
–FCL contains methods that sort for you 
–sort any kind of object 
–object must implement IComparable 
object[] students; students = new object[n]; students[0] = new Student(…); students[1] = new Student(…); . . . Array.Sort(students); 
public interface IComparable { int CompareTo(object obj); }
27 
Microsoft 
To be a sortable object… 
•Sortable objects must implement IComparable 
•Example: 
–Student objects sort by id 
public class Student : Person, IComparable { private int m_ID; . . . int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID – other.m_ID; } } 
base class 
interface 
Student 
Person
28 
Microsoft 
Summary 
•Object-oriented programming is *the* paradigm of .NET 
•C# is a fully object-oriented programming language 
–fields, properties, indexers, methods, constructors 
–garbage collection 
–single inheritance 
–interfaces 
•Inheritance? 
–consider when class A "is-a" class B 
–but you only get single-inheritance, so make it count 
•Interfaces? 
–consider when class C interacts with classes D, E, F, … 
–a class can implement any number of interfaces
29 
Microsoft 
References 
•Books: 
–I. Pohl, "C# by Dissection" 
–S. Lippman, "C# Primer" 
–J. Mayo, "C# Unleashed"

More Related Content

What's hot

Dev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented ProgrammingDev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented Programming
Svetlin Nakov
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
Laxman Puri
 
C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
mohamedsamyali
 
麻省理工C++公开教学课程(三)
麻省理工C++公开教学课程(三)麻省理工C++公开教学课程(三)
麻省理工C++公开教学课程(三)
ProCharm
 
Inheritance
InheritanceInheritance
Inheritance
piyush Kumar Sharma
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
Vishnu Shaji
 

What's hot (8)

Dev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented ProgrammingDev Concepts: Object-Oriented Programming
Dev Concepts: Object-Oriented Programming
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Oops concept
Oops conceptOops concept
Oops concept
 
Lecture10
Lecture10Lecture10
Lecture10
 
C# Summer course - Lecture 1
C# Summer course - Lecture 1C# Summer course - Lecture 1
C# Summer course - Lecture 1
 
麻省理工C++公开教学课程(三)
麻省理工C++公开教学课程(三)麻省理工C++公开教学课程(三)
麻省理工C++公开教学课程(三)
 
Inheritance
InheritanceInheritance
Inheritance
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
 

Viewers also liked

Microsoft .Net Framework 2 0
Microsoft .Net Framework 2 0Microsoft .Net Framework 2 0
Microsoft .Net Framework 2 0
Acend Corporate Learning
 
C# Basics
C# BasicsC# Basics
C# Basics
Binu Bhasuran
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
Youssef Mohammed Abohaty
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
Vadym Melnyk
 
Chapter03
Chapter03Chapter03
Chapter01
Chapter01Chapter01
Csc153 chapter 03
Csc153 chapter 03Csc153 chapter 03
Csc153 chapter 03PCC
 
Object Oriented Programming Languages
Object Oriented Programming LanguagesObject Oriented Programming Languages
Object Oriented Programming Languages
Mannu Khani
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
Online
 
Chapter02
Chapter02Chapter02
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02PCC
 
Csc153 chapter 01
Csc153 chapter 01Csc153 chapter 01
Csc153 chapter 01PCC
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
Abou Bakr Ashraf
 
Visula C# Programming Lecture 2
Visula C# Programming Lecture 2Visula C# Programming Lecture 2
Visula C# Programming Lecture 2
Abou Bakr Ashraf
 
C sharp
C sharpC sharp
C sharp
Ahmed Vic
 
.NET and C# introduction
.NET and C# introduction.NET and C# introduction
.NET and C# introduction
Peter Gfader
 
Basics of c# by sabir
Basics of c# by sabirBasics of c# by sabir
Basics of c# by sabir
Sabir Ali
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
SAMIR BHOGAYTA
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
Sunil OS
 

Viewers also liked (20)

Microsoft .Net Framework 2 0
Microsoft .Net Framework 2 0Microsoft .Net Framework 2 0
Microsoft .Net Framework 2 0
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Chapter03
Chapter03Chapter03
Chapter03
 
Chapter01
Chapter01Chapter01
Chapter01
 
Csc153 chapter 03
Csc153 chapter 03Csc153 chapter 03
Csc153 chapter 03
 
Object Oriented Programming Languages
Object Oriented Programming LanguagesObject Oriented Programming Languages
Object Oriented Programming Languages
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Chapter02
Chapter02Chapter02
Chapter02
 
Csc153 chapter 02
Csc153 chapter 02Csc153 chapter 02
Csc153 chapter 02
 
Csc153 chapter 01
Csc153 chapter 01Csc153 chapter 01
Csc153 chapter 01
 
7.data types in c#
7.data types in c#7.data types in c#
7.data types in c#
 
Visula C# Programming Lecture 1
Visula C# Programming Lecture 1Visula C# Programming Lecture 1
Visula C# Programming Lecture 1
 
Visula C# Programming Lecture 2
Visula C# Programming Lecture 2Visula C# Programming Lecture 2
Visula C# Programming Lecture 2
 
C sharp
C sharpC sharp
C sharp
 
.NET and C# introduction
.NET and C# introduction.NET and C# introduction
.NET and C# introduction
 
Basics of c# by sabir
Basics of c# by sabirBasics of c# by sabir
Basics of c# by sabir
 
Introduction To C#
Introduction To C#Introduction To C#
Introduction To C#
 
C# Variables and Operators
C# Variables and OperatorsC# Variables and Operators
C# Variables and Operators
 

Similar to L1

10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
secondakay
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09PCC
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
Ali Aminian
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#Svetlin Nakov
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
anitashinde33
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
Umar Farooq
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
ahmed hmed
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
ChhaviCoachingCenter
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
Muhammed Thanveer M
 
C++ oop
C++ oopC++ oop
C++ oop
Sunil OS
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
nivedita murugan
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Lecture 13, 14 & 15   c# cmd let programming and scriptingLecture 13, 14 & 15   c# cmd let programming and scripting
Lecture 13, 14 & 15 c# cmd let programming and scriptingWiliam Ferraciolli
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
bradringel
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
KavitaHegde4
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
KavitaHegde4
 

Similar to L1 (20)

10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt10 - Encapsulation(object oriented programming)- java . ppt
10 - Encapsulation(object oriented programming)- java . ppt
 
C++ppt. Classs and object, class and object
C++ppt. Classs and object, class and objectC++ppt. Classs and object, class and object
C++ppt. Classs and object, class and object
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
 
Learning C++ - Class 4
Learning C++ - Class 4Learning C++ - Class 4
Learning C++ - Class 4
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)C# (This keyword, Properties, Inheritance, Base Keyword)
C# (This keyword, Properties, Inheritance, Base Keyword)
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Python_Unit_2 OOPS.pptx
Python_Unit_2  OOPS.pptxPython_Unit_2  OOPS.pptx
Python_Unit_2 OOPS.pptx
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
C++ oop
C++ oopC++ oop
C++ oop
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
constructor.ppt
constructor.pptconstructor.ppt
constructor.ppt
 
Lecture 13, 14 & 15 c# cmd let programming and scripting
Lecture 13, 14 & 15   c# cmd let programming and scriptingLecture 13, 14 & 15   c# cmd let programming and scripting
Lecture 13, 14 & 15 c# cmd let programming and scripting
 
Objective-C talk
Objective-C talkObjective-C talk
Objective-C talk
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
Chapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptxChapter 7 - Constructors.pptx
Chapter 7 - Constructors.pptx
 
Chapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdfChapter 7 - Constructors.pdf
Chapter 7 - Constructors.pdf
 

More from lksoo

Lo48
Lo48Lo48
Lo48
lksoo
 
Lo43
Lo43Lo43
Lo43
lksoo
 
Lo39
Lo39Lo39
Lo39lksoo
 
Lo37
Lo37Lo37
Lo37
lksoo
 
Lo27
Lo27Lo27
Lo27
lksoo
 
Lo17
Lo17Lo17
Lo17
lksoo
 
Lo12
Lo12Lo12
Lo12
lksoo
 
T3
T3T3
T3
lksoo
 
T2
T2T2
T2
lksoo
 
T1
T1T1
T1
lksoo
 
T4
T4T4
T4
lksoo
 
P5
P5P5
P5
lksoo
 
P4
P4P4
P4
lksoo
 
P3
P3P3
P3
lksoo
 
P1
P1P1
P1
lksoo
 
P2
P2P2
P2
lksoo
 
L10
L10L10
L10
lksoo
 
L9
L9L9
L9
lksoo
 
L8
L8L8
L8
lksoo
 
L7
L7L7
L7
lksoo
 

More from lksoo (20)

Lo48
Lo48Lo48
Lo48
 
Lo43
Lo43Lo43
Lo43
 
Lo39
Lo39Lo39
Lo39
 
Lo37
Lo37Lo37
Lo37
 
Lo27
Lo27Lo27
Lo27
 
Lo17
Lo17Lo17
Lo17
 
Lo12
Lo12Lo12
Lo12
 
T3
T3T3
T3
 
T2
T2T2
T2
 
T1
T1T1
T1
 
T4
T4T4
T4
 
P5
P5P5
P5
 
P4
P4P4
P4
 
P3
P3P3
P3
 
P1
P1P1
P1
 
P2
P2P2
P2
 
L10
L10L10
L10
 
L9
L9L9
L9
 
L8
L8L8
L8
 
L7
L7L7
L7
 

Recently uploaded

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 

Recently uploaded (20)

Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 

L1

  • 2. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in .NET. C# features full support for the object- oriented programming paradigm…” •Designing your own classes •Destroying objects and garbage collection •Inheritance •Interfaces
  • 3. 3 Microsoft Part 1 •Designing your own classes…
  • 4. 4 Microsoft Motivation •.NET contains thousands of prebuilt classes in the FCL •So why design your own? –to model entities unique to your application domain… •Examples: –employees –customers –products –orders –documents –business units –etc.
  • 5. 5 Microsoft Simple class members •C# supports standard fields, methods and constructors –with standard access control: public, private, protected public class Person { public string Name; // fields public int Age; public Person() // default constructor { this.Name = "?"; this.Age = -1; } public Person(string name, int age) // parameterized ctor { this.Name = name; this.Age = age; } public override string ToString() // method { return this.Name; } }//class
  • 6. 6 Microsoft Basic design rules •Provide constructor(s) •Omit default constructor for parameterized initialization •Override ToString, Equals and GetHashCode •Data hiding: "hide as many details as you can" –enable access when necessary via accessors and mutators –.NET provides a cleaner mechanism via properties…
  • 7. 7 Microsoft Properties •Goal: –to allow our class users to safely write code like this: –provides field-like access with method-like semantics… –… enabling access control, validation, data persistence, screen updating, etc. Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1;
  • 8. 8 Microsoft Observation •Read of value ("Get") vs. Write of value ("Set") Person p; p = new Person("joe hummel", 40); p.Age = p.Age + 1; Get age Set age
  • 9. 9 Microsoft Property implementation •Implementation options: –read-only –write-only –read-write public class Person { private string m_Name; private int m_Age; . . . public string Name { get { ... } } public int Age { get { ... } set { ... } } } read-only read-write
  • 10. 10 Microsoft Example •Simplest implementation just reads / writes private field: public class Person { private string m_Name; private int m_Age; . . . public string Name // Name property { get { return this.m_Name; } } public int Age // Age property { get { return this.m_Age; } set { this.m_Age = value; } } }
  • 11. 11 Microsoft Indexers •Enable array-like access with method-like semantics –great for data structure classes, collections, etc. People p; // collection of Person objects p = new People(); p[0] = new Person("joe hummel", 40); . . . age = p[0].Age; Set Get
  • 12. 12 Microsoft Example •Implemented like properties, with Get and Set methods: public class People { private Person[] m_people; // underlying array . . . public Person this[int i] // int indexer { get { return this.m_people[i]; } set { this.m_people[i] = value; } } public Person this[string name] // string indexer { get { return ...; } } } read-only read-write
  • 13. 13 Microsoft Part 2 •Destroying objects and garbage collection…
  • 14. 14 Microsoft Object creation and destruction •Objects are explicitly created via new •Objects are never explicitly destroyed! –.NET relies upon garbage collection to destroy objects –garbage collector runs unpredictably…
  • 15. 15 Microsoft Finalization •Objects can be notified when they are garbage collected •Garbage collector (GC) will call object's finalizer public class Person { . . . ~Person() // finalizer { ... }
  • 16. 16 Microsoft Should you rely upon finalization? •No! –it's unpredictable –it's expensive (.NET tracks object on special queue, etc.) •Alternatives? –design classes so that timely finalization is unnecessary –provide Close / Dispose method for class users to call ** Warning ** As a .NET programmer, you are responsible for calling Dispose / Close. Rule of thumb: if you call Open, you need to call Close / Dispose for correct execution. Common examples are file I/O, database I/O, and XML processing.
  • 17. 17 Microsoft Part 3 •Inheritance…
  • 18. 18 Microsoft Inheritance •Use in the small, when a derived class "is-a" base class –enables code reuse –enables design reuse & polymorphic programming •Example: –a Student is-a Person Undergraduate Person Student Employee Graduate Staff Faculty
  • 19. 19 Microsoft Implementation •C# supports single inheritance –public inheritance only (C++ parlance) –base keyword gives you access to base class's members public class Student : Person { private int m_ID; public Student(string name, int age, int id) // constructor :base(name, age) { this.m_ID = id; } } Student Person
  • 20. 20 Microsoft Binding •C# supports both static and dynamic binding –determined by absence or presence of virtual keyword –derived class must acknowledge with new or override public class Person { . . . // statically-bound public string HomeAddress() { … } // dynamically-bound public virtual decimal Salary() { … } } public class Student : Person { . . . public new string HomeAddress() { … } public override decimal Salary() { … } }
  • 21. 21 Microsoft All classes inherit from System.Object StringArrayValueTypeExceptionDelegateClass1MulticastDelegateClass2Class3ObjectEnum1Structure1EnumPrimitive typesBooleanByteInt16Int32Int64CharSingleDoubleDecimalDateTimeSystem-defined typesUser-defined typesDelegate1TimeSpanGuid
  • 22. 22 Microsoft Part 4 •Interfaces…
  • 23. 23 Microsoft Interfaces •An interface represents a design •Example: –the design of an object for iterating across a data structure –interface = method signatures only, no implementation details! –this is how foreach loop works… public interface IEnumerator { void Reset(); // reset iterator to beginning bool MoveNext(); // advance to next element object Current { get; } // retrieve current element }
  • 24. 24 Microsoft Why use interfaces? •Formalize system design before implementation –especially helpful for PITL (programming in the large) •Design by contract –interface represents contract between client and object •Decoupling –interface specifies interaction between class A and B –by decoupling A from B, A can easily interact with C, D, …
  • 25. 25 Microsoft .NET is heavily influenced by interfaces •IComparable •ICloneable •IDisposable •IEnumerable & IEnumerator •IList •ISerializable •IDBConnection, IDBCommand, IDataReader •etc.
  • 26. 26 Microsoft Example •Sorting –FCL contains methods that sort for you –sort any kind of object –object must implement IComparable object[] students; students = new object[n]; students[0] = new Student(…); students[1] = new Student(…); . . . Array.Sort(students); public interface IComparable { int CompareTo(object obj); }
  • 27. 27 Microsoft To be a sortable object… •Sortable objects must implement IComparable •Example: –Student objects sort by id public class Student : Person, IComparable { private int m_ID; . . . int IComparable.CompareTo(Object obj) { Student other; other = (Student) obj; return this.m_ID – other.m_ID; } } base class interface Student Person
  • 28. 28 Microsoft Summary •Object-oriented programming is *the* paradigm of .NET •C# is a fully object-oriented programming language –fields, properties, indexers, methods, constructors –garbage collection –single inheritance –interfaces •Inheritance? –consider when class A "is-a" class B –but you only get single-inheritance, so make it count •Interfaces? –consider when class C interacts with classes D, E, F, … –a class can implement any number of interfaces
  • 29. 29 Microsoft References •Books: –I. Pohl, "C# by Dissection" –S. Lippman, "C# Primer" –J. Mayo, "C# Unleashed"