Upload
kalieswari-revathi
View
217
Download
0
Embed Size (px)
Citation preview
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 1/64
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 2/64
C#.NET/ 2 of 106
Objectives
Essentials of Object-OrientedProgramming
Classes and Objects Using Encapsulation
C# and Object Orientation
Defining Object-Oriented Systems
The Object Hier archy
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 3/64
C#.NET/ 3 of 106
Objectives
Use Constructors In C#
Use Destructors In C#
Explain the working of Garbage Collector
Discuss Method Overloading
Discuss Operator Overloading
Use Inheritance in C#
Discuss Overriding
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 4/64
C#.NET/ 4 of 106
Objectives
Discuss Polymorphism
Use Virtual Functions
Discuss relationship between polymorphism and
inheritance Discuss Abstract Base classes
Discuss the concept of Interfaces
Use Interfaces Discuss Properties
Discuss Indexers
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 5/64
Object ± OrientedProgramming Overview
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 6/64
C#.NET/ 6 of 106
1. Object ± Oriented Progr amming
History
Object-oriented progr amming has roots that can betr aced to the 1960s:
As har dware and software became increasinglycomplex, quality was often compromised.
Researchers studied ways to maintain software qualityand developed object-oriented progr amming in part toaddress common problems by strongly emphasizing discrete, reusable units of progr amming logic.
The methodology focuses on data r ather than processes, with progr ams composed of self-sufficientmodules (objects) each containing all the information needed to manipulate its own data structure.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 7/64
C#.NET/ 7 of 106
1. Object ± Oriented Progr amming
History
The Object-Oriented methodology focuses on datarather than processes, with progr ams composed of self-sufficient modules (objects) each containing all theinformation needed to manipulate its own data
structure. This is in contr ast to the existing modular
progr amming which had been dominant for manyyears that focused on the function of a module
An object-oriented progr am may thus be viewed as a collection of cooper ating objects, as opposed to theconventional model, in which a progr am is seen as a list of tasks (subroutines) to perform
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 8/64
C#.NET/ 8 of 106
2. OOP Fundamental Concepts
Fundamental Concepts in OOP are: Class
Object
Instance
Method
Message passing
Inheritance
Abstr action
Encapsulation Polymor physm
De-coupling
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 9/64
I. Classes and Objects
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 10/64
C#.NET/ 10 of 106
1. What is a class?
A class define the abstr act char acteristics of a thing (object), including the thing'schar acteristics (its attributes, fields or
properties) and the thing's behaviors (thethings it can do, or methods, operations or features).
For example, the class Dog would consist of
tr aits shared by all dogs, such as: Breed and fur color (char acteristics), and
The ability to bark and sit (behaviors)
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 11/64
C#.NET/ 11 of 106
1. What is a class?
In C#.NET, classes are declared using
the keywor d class, as shown in the
following example:
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 12/64
C#.NET/ 12 of 106
An example: A class with its attributes,
fields, properties, methods,«
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 13/64
C#.NET/ 13 of 106
2. Members of a class
The members of a class type are divided into thefollowing categories: Constants
Fields
Methods Properties
Events
Indexers
Oper ators
Constructors Destructors
Types
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 14/64
C#.NET/ 14 of 106
2.1. Compare Properties and Public
Fields
Properties can be recognizeby IDEs (such as Visual Studio.NET) but public fields
Properties are the hybrid of fields and methods
A property is like a clock-knob: when you tur n it, the
hour hand and the minutehand and the second hand are automatically ad justed
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 15/64
C#.NET/ 15 of 106
2.2. Constructor
Whenever a class or struct is created, itsconstructor is called. A class or struct mayhave multiple constructors that take differentar guments.
Constructors enable the progr ammer to setdef ault values, limit instantiation, and writecode that is f lexible and easy to read.
If you do not provide a constructor for your object, C# will create one by def ault that
instantiates the object and sets member variables to the def ault values
Static classes and structs can also haveconstructors
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 16/64
C#.NET/ 16 of 106
2.3. Destructor
Destructors are used to destruct instances of classes. Destructors cannot be defined in structs. They are only used
with classes.
A class can only have one destructor .
Destructors cannot be inherited or over loaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have par ameters.
The destructor implicitly calls Finalize on the base class of the object.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 17/64
C#.NET/ 17 of 106
2.4. Indexers
Indexers allow you to index a class or a
struct instance in the same way as an
arr ay
An indexer is declared like a property
except that the name of the member is
this followed by a par ameter list written
between the delimiters [ and ]
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 18/64
C#.NET/ 18 of 106
Indexer example
BEFORE
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 19/64
C#.NET/ 19 of 106
Indexer example
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 20/64
C#.NET/ 20 of 106
3. Access Modifiers
Access modifiers are keywor ds used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers: Public: The public keywor d is an access modifier for types and type
members. Public access is the most permissive access level. There areno restrictions on accessing public members
Protected: The protected keywor d is a member access modifier . Aprotected member is accessible from within the class in which it isdeclared, and from within any class derived from the class that declared this member .
Internal: Inter nal members are accessible only within files in the sameassembly. A common use of inter nal access is in component-based development because it enables a group of components to cooper ate in a private manner without being exposed to the rest of the application code.
Private: The private keywor d is a member access modifier . Privateaccess is the least permissive access level. Private members areaccessible only within the body of the class or the struct in which they aredeclared. Nested types in the same body can also access those privatemembers.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 21/64
C#.NET/ 21 of 106
3. What is the meaning of ³partial´
keywor d?
In .NET fr amework 2.0 or higher, Microsoft introducesa very new modifier named ³partial´.
Before, the definition a a class must be placed in asingle source file.
But now, with ³partial´ modifier, It is possible to splitthe definition of a class or a struct, or an interf ace over
two or more source files. Each source file contains a section of the class definition, and all parts arecombined when the application is compiled
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 22/64
C#.NET/ 22 of 106
³partial´ class definition example
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 23/64
C#.NET/ 23 of 106
3. What is the meaning of ³partial´
keywor d?
Reasons to use ³partial´ modifier:
When working on lar ge projects, spreading a class
over separ ate files allows multiple progr ammers to
work on it simultaneously. When working with automatically gener ated source,
code can be added to the class without having to
recreate the source file. Visual Studio uses this
approach when creating Windows Forms, Web
Service wr apper code, and so on. You can create
code that uses these classes without having to edit
the file created by Visual Studio
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 24/64
C#.NET/ 24 of 106
4. What is an Object?
An Object is a patter n of a class.
Exp: The class of Dog defines all possible dogs by listing thechar acteristics and behaviors they can have; the object Lassie is one particular
dog, with particular versions of thechar acteristics. A Dog has fur; Lassiehas brown-and-white fur .
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 25/64
C#.NET/ 25 of 106
4. What is an Object?
In OOP, each object is capable of receiving messages, processing data,and sending messages to other objects
and can be viewed as an independent'machine' with a distinct role or responsibility.
The actions (or "oper ators") on theseobjects are closely associated with theobject.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 26/64
C#.NET/ 26 of 106
5. What is an ³Instance´
The instance is the actual object created
at runtime (and placed in RAM).
The set of values of the fields and
properties of a particular object is called
its state
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 27/64
C#.NET/ 27 of 106
6. Object Data (or Object State)
Object data describes information for
individual objects
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 28/64
C#.NET/ 28 of 106
7. ³static´ keywor d
The static modifier can be used with
classes, fields, methods, properties,
oper ators, events, and constructors
But ³static´ keywor d cannot be used with
indexers, destructors, or types other than
classes.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 29/64
C#.NET/ 29 of 106
7. ³static´ keywor d example
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 30/64
C#.NET/ 30 of 106
7. ³static´ keywor d
The static modifier on a class means that the classcannot be instantiated, and that all of its members arestatic.
A static member has one version regar dless of how many instances of its enclosing type are created.
A constant is implicitly a static member .
A static member cannot be referenced through an instance. Instead, it is referenced through the typename
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 31/64
C#.NET/ 31 of 106
8. Nested Classes
A class defined within a class or struct is
called a nested class
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 32/64
II. Encapsulation
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 33/64
C#.NET/ 33 of 106
1. What is encapsulation?
Encapsulation is the hiding of the inter nal mechanisms and data structures of a softwarecomponent behind a defined interf ace, in such a waythat users of the component (other pieces of software)only need to know what the component does, and cannot make themselves dependent on the details of how it does it.
For example, the Dog class has a bark() method. Thecode for the bark() method defines exactly how a bark
happens (e.g., by inhale() and then exhale(), at a particular pitch and volume). Timmy, Lassie's friend,however, does not need to know exactly how shebarks.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 34/64
C#.NET/ 34 of 106
2. Why must encapsulate?
The pur pose of encapsulation is to achievepotential for change: the inter nal mechanismsof the component can be improved withoutimpact on other components, or the
component can be replaced with a differentone that supports the same public interf ace.
Encapsulation also protects the integrity of the
component, by preventing users from setting the inter nal data of the component into an invalid or inconsistent state.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 35/64
C#.NET/ 35 of 106
2. Why must encapsulate?
Encapsulation reduces system
complexity and thus increases
robustness, by limiting the
inter dependencies between software
components
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 36/64
III. Inheritance
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 37/64
C#.NET/ 37 of 106
1. What is the inheritance?
Inheritance is a way to form new classes (instances of which are called objects) using classes that have already
been defined. The inheritance concept was invented in
1967
The new classes, known as derivedclasses, take over (or inherit) attributesand behavior of the pre-existing classes
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 38/64
C#.NET/ 38 of 106
Inheritance - Examples
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 39/64
C#.NET/ 39 of 106
2. Single and Multiple inheritances
Single inheritance: deriving from one
base class
Multiple inheritance: deriving from two or more base classes
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 40/64
C#.NET/ 40 of 106
3. What are Over-loads?
Over loading is the technical term for
declaring two or more methods in the
same scope with the same name
By specifying different number of
par ameters
By specifying different types of par ameters
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 41/64
C#.NET/ 41 of 106
4. Oper ator Over-loading
Like C++, C# allows you to over load oper atorsfor use on your own classes. This makes itpossible for a user-defined data type to look as
natur al and be as logical to use as a fundamental data type
To over load an oper ator, you write a function
that has the name oper ator followed by thesymbol for the oper ator to be over loaded
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 42/64
C#.NET/ 42 of 106
5. Hiding Base Class Members
Sometimes derived class members have the samename as a corresponding base class member . In thiscase, the derived member is said to be "hiding" thebase class member
When hiding occurs, the derived member is masking the functionality of the base class member . Users of the derived class won't be able to see the hidden member; they'll see only the derived class member
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 43/64
C#.NET/ 43 of 106
7. ³base´ keywor d
The base keywor d is used to access membersof the base class from within a derived class: Call a method on the base class that has been
overridden by another method.
Specify which base-class constructor should becalled when creating instances of the derived class.
A base class access is permitted only in a constructor, an instance method, or an
instance property accessor . It is an error to use the base keywor d fromwithin a static method.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 44/64
C#.NET/ 44 of 106
7. ³base´ keywor d example
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 45/64
C#.NET/ 45 of 106
8. ³sealed´ keywor d
When applied to a class, the sealed modifier
prevents other classes from inheriting from it
You can also use the sealed modifier on a
method or property that overrides a virtual
method or property in a base class. This
enables you to allow classes to derive from
your class and prevent them from overriding
specific virtual methods or properties.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 46/64
C#.NET/ 46 of 106
8. ³sealed´ keywor d example
In the following example, no class can
inherit from class Musician.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 47/64
C#.NET/ 47 of 106
8. ³sealed´ keywor d example
In the following example, Z inherits from Y but
Z cannot override the virtual function F that is
declared in X and sealed in Y.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 48/64
C#.NET/ 48 of 106
8. ³sealed´ keywor d
Notes: When you define new methods or properties in a
class, you can prevent deriving classes fromoverriding them by not declaring them as virtual.
It is an error to use the abstr act modifier with a sealed class, because an abstr act class must beinherited by a class that provides an implementation of the abstr act methods or properties.
When applied to a method or property, the sealed modifier must always be used with override.
Because structs are implicitly sealed, they cannotbe inherited.
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 49/64
C#.NET/ 49 of 106
8. ³new´ keywor d
In C#, the new keywor d can be used as
an oper ator or as a modifier .
new oper ator Used to create objects on
the heap and invoke constructors.
new modifier Used to hide an inherited
member from a base class member .
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 50/64
C#.NET/ 50 of 106
8. ³new´ keywor d examples
In the following example, ³new´ keywor d
is used as an oper ator
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 51/64
C#.NET/ 51 of 106
8. ³new´ keywor d examples
In the following example, ³new´ keywor d
is used as a modifier:
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 52/64
C#.NET/ 52 of 106
9. Why use inheritances?
Enables a series of classes to be related together accor ding to their common parts:
common members only specified once, in base class
can be accessed in objects of all classesderived from the base class
Increases efficiency of production and f lexibility
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 53/64
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 54/64
C#.NET/ 54 of 106
1. What is the polymor phism?
Polymor phism is derived from two Greek wor ds. Poly (meaning many) and morph (meaning forms). Polymor phism means manyforms
Understanding Polymor phism is crucial to any OO language
professional , be it a Java , C++ or C# progr ammer
If a Dog is commanded to speak(), it may emit a bark, while if a Pig is asked to speak(), it may respond with an oink. Both inheritspeak() from Animal, but their subclass methods override themethods of the superclass, known as overriding polymor phism
We implement polymor phism by using virtual and overridekeywor ds
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 55/64
C#.NET/ 55 of 106
Polymor physm example
V mt hình thc, i su vào ca hàm Speakphi có kiu là Animal,và khi hàm này cgi, thì phng thc
Speak ca Animal ó sc gi và s không in
ra ni dung gì
Nhng thc t, khi truyn i s
u vào cho hàm Speak, lúc là Pig,lúc là Dog, thì hàm Speak li lúc thìgi hàm Speak ca Pig, lúc thì li
gi hàm Speak ca Dog => Chính vìlý do này, hàm Speak c gi là
³a hình´
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 56/64
C#.NET/ 56 of 106
3. ³override´ and ³virtual´ keywor ds
³override´ and ³virtual´ keywor ds are used toimplement polymor phism in C#.NET
The override modifier is required to extend or
modify the abstr act or virtual implementation of an inherited method, property, indexer, or event
The virtual keywor d is used to modify a method, property, indexer, or event declar ation and allow for it to be overridden in aderived class
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 57/64
C#.NET/ 57 of 106
4. Why polymor phism?
Why include polymor phism in a
progr amming language?
Because it significantly reduces
progr amming effort. In particular,
polymor phism allows you to write one
method for a variety of types, r ather than to write one method per type
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 58/64
V. Abstractions:Abstract Base Classes &
Interfaces
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 59/64
C#.NET/ 59 of 106
1. What is the abstr action?
Abstr action (from the Latin abs, meaning away
fr om and t raher e, meaning to draw ) is the
process of taking away or removing
char acteristics from something in or der toreduce it to a set of essential char acteristics
In object-oriented progr amming, abstr action is
one of three central principles (along with
encapsulation and inheritance)
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 60/64
C#.NET/ 60 of 106
1. What is the abstr action?
Through the process of abstr action, a
progr ammer hides all but the relevant
data about an object in or der to reduce
complexity and increase efficiency
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 61/64
C#.NET/ 61 of 106
2. Abstr act base classes
Abstr act base classes are classes that cannotbe instantiated, and are frequently either partially implemented, or not at all implemented.
In C#, the abstract modifier is used to declarean abstr act class
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 62/64
C#.NET/ 62 of 106
3. Interf aces
An interf ace is a collection of method definitions (without implementations) and constant values.
An interf ace is like a pure abstr act baseclass
Notes:
A class that implements a particular interf ace must implement the memberslisted by that interf ace
4 Wh t k thi
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 63/64
C#.NET/ 63 of 106
4. Why must make everything
abstr act?
The importance of abstr action is derived from
its ability to hide irrelevant details and from the
use of names to reference objects.
Abstr action is essential in the construction of progr ams. It places the emphasis on what an
object is or does r ather than how it is
represented or how it works. Thus, it is the
primary means of managing complexity inlarge programs
5 Diff b t Ab t t
8/7/2019 T3 - Object Oriented Programming in C#
http://slidepdf.com/reader/full/t3-object-oriented-programming-in-c 64/64
5. Differences between Abstr act
Classes Vs. Interf aces
The biggest difference between an
abstr act class and an interf ace is: A
class may implement an unlimited
number of interfaces, but may inheritfrom only one abstract