ĐẠI HỌC FPT CẦN THƠ
Object-Oriented Programming
Lession 3
2
Objectives
Explain about Explain about OOP
Explain Explain classes and objects
Define and describe Define and describe methods
Explain about Explain about the access modifiers
Define and describe Define and describe inheritance
Explain about Explain about method overriding
Explain about Explain about polymorphism and abstraction
Lession 3: Object-Oriented Programming - Huong Hoang Luong
Objectives
◆ Discuss more new features in OOP :
▪ Properties, Auto-implemented properties
▪ Static class and Extension Method
▪ Anonymous Type and Default contructor
▪ Readonly members and Default interface methods
▪ Using declarations
▪ Expression-bodied members
▪ Record type
▪ Object Initialize
▪ Read-only auto-properties and Init-Only Properties
Lession 3: Object-Oriented Programming - Huong Hoang Luong 3
4
Object-Oriented Programming(OOP)
Programming languages are based on two fundamental concepts: data
and ways to manipulate data. This approach had several drawbacks such
as lack of re-use and lack of maintainability
To overcome these difficulties, OOP was introduced, which focused on
data rather than the ways to manipulate data
The object-oriented approach defines objects as entities having a defined
set of values and a defined set of operations that can be performed on
these values
Abstraction, encapsulation, polymorphism, and inheritance are the core
principles of object-oriented programming
Lession 3: Object-Oriented Programming - Huong Hoang Luong
5
OOP Paradigm
Procedure-Oriented Program
Class A
{
data1
data1
data2 Modifiers Function1 ()
Function1 (data1)
Function2 ()
Function2 (data1) }
Function3 (data2)
Function4 (data2)
Class B
{
data2
Object = Data + Methods
Modifiers Function3 ()
Basic Concepts Function4()
- Encapsulation Particular }
- Inheritance methods:
- Polymorphism Constructors
Lession 3: Object-Oriented Programming - Huong Hoang Luong
6
Object-Oriented Programming
OOP is a powerful concept that solves many problems found in software
development. OOP is not the holy grail of programming but it can help
in writing code that is easy to read, easy to maintain, easy to update,
and easy to expand
An object can inherit the properties of another object using the concept
of inheritance. Hence, we can say that object-oriented programming is
organized around data and the operations that are permitted on the
data
When we do object-oriented programming, we start with identifying the
entities we need to operate on, how they relate to each other, and how
they interact. This is a process called data modeling and the result of
this is a set of classes that generalize the identified entities
Lession 3: Object-Oriented Programming - Huong Hoang Luong
7
Classes and Objects
◆ A class is a user-defined blueprint or prototype from which objects are created.
Basically, a class combines the fields and methods(member function which
defines actions) into a single unit
Lession 3: Object-Oriented Programming - Huong Hoang Luong
8
Classes and Objects
◆ An object is an instance of the class and represents a real-life entity. To
initialize an object in C#, we use a new keyword followed by the name of the
class that the object will be based on
Lession 3: Object-Oriented Programming - Huong Hoang Luong
9
Member Visibility
◆ There are five access specifiers: private, public, protected, internal, and
protected internal. By default, the members are private to the class
▪ public: The type or member can be accessed by any other code in the same assembly or
another assembly that references it
▪ private: The type or member can be accessed only by code in the same class or struct
▪ protected: The type or member can be accessed only by code in the same class, or in a class
that is derived from that class
Lession 3: Object-Oriented Programming - Huong Hoang Luong
Member Visibility
▪ internal: The type or member can be accessed by any
code in the same assembly, but not from another
assembly
▪ protected internal: The type or member can be
accessed by any code in the assembly in which it's
declared, or from within a derived class in another
assembly
▪ private protected: The type or member can be accessed
only within its declaring assembly, by code in the same
class or in a type that is derived from that class
Lession 3: Object-Oriented Programming - Huong Hoang Luong 10
OOP-Encapsulation
◆ Encapsulation is defined as binding data and code that
manipulates it together in a single unit
◆ Data is privately bound within a class without direct
access from the outside of the class
◆ All objects that need to read or modify the data of an
object should do it through the public methods that a
class provides
◆ This characteristic is called data hiding and makes code
less error-prone by defining a limited number of entry
points to an object’s data
Lession 3: Object-Oriented Programming - Huong Hoang Luong 11
12
OOP-Encapsulation
Lession 3: Object-Oriented Programming - Huong Hoang Luong
13
Read-only auto properties
& Init-Only properties
◆ When we write a property only with "get", it automatically becomes a Read Only
property or we can use Init-Only properties
Lession 3: Object-Oriented Programming - Huong Hoang Luong
14
OOP-Inheritance
◆ Inheritance is a mechanism through which a class can inherit the properties
and functionalities of another class
◆ Other classes can inherit these functionalities and data of the parent class as
well as extending or modifying them and adding additional functionalities and
properties.
◆ There are three types of inheritance supported in C#:
Lession 3: Object-Oriented Programming - Huong Hoang Luong
15
OOP-Inheritance
Lession 3: Object-Oriented Programming - Huong Hoang Luong
16
OOP-Polymorphism
◆ Ability allows many versions of a method based on overloading and overriding
methods techniques
Lession 3: Object-Oriented Programming - Huong Hoang Luong
17
virtual : provide a default implementation.
Can be overridden if necessary
abstract: sub-classes MUST override
Besides virtual and abstract, C# provide new
keyword that applies to methods. What does
that mean?
Lession 3: Object-Oriented Programming - Huong Hoang Luong
18
OOP-Interface
◆ An interface contains definitions for a group of related functionalities that a
non-abstract class or a struct must implement
◆ An interface cannot be instantiated but can only be inherited by classes or
other interfaces
◆ An interface may not declare instance data such as fields, auto-implemented
properties, or property-like events. Interface names begin with a capital “I”
Lession 3: Object-Oriented Programming - Huong Hoang Luong
19
OOP-Interface
Lession 3: Object-Oriented Programming - Huong Hoang Luong
20
Interface Inheritance
Lession 3: Object-Oriented Programming - Huong Hoang Luong
21
Default Interface Methods
◆ C# allows to add a method with their implementation to the interface without
breaking the existing implementation of the interface, such type of methods is
known as default interface methods(virtual extension methods)
Lession 3: Object-Oriented Programming - Huong Hoang Luong
22
The is and as Operators
The is operator is used to check if the run-time type of an object is compatible
with the given type or not whereas as operator is used to perform conversion
between compatible reference types or Nullable types
The is operator returns true if the given object is of the same type whereas as
operator returns the object when they are compatible with the given type
The is operator returns false if the given object is not of the same type
whereas as operator return null if the conversion is not possible
The is operator is used for only reference, boxing, and unboxing conversions
whereas as operator is used only for nullable, reference and boxing
conversions
Lession 3: Object-Oriented Programming - Huong Hoang Luong
23
The is and as Operators
Lession 3: Object-Oriented Programming - Huong Hoang Luong
24
Static Constructor
◆ Prevent static field to be reset
◆ A given class (or structure) may
define only a single static constructor
◆ A static constructor executes exactly
one time, regardless of how many
objects of the type are created
◆ A static constructor does not take an
access modifier and cannot take any
parameters
◆ The static constructor executes
before any instance-level
constructors
Lession 3: Object-Oriented Programming - Huong Hoang Luong
25
Static Class
Classes that cannot be instantiated or inherited are known as
classes and the static keyword is used before the class name
that consists of static data members and static methods
They can only contain static members
It is not possible to create an
They cannot be instantiated or inherited
instance of a static class using the and cannot contain instance
new keyword. The main features constructors. However, the developer
can create static constructors to
of static classes are as follows: initialize the static members
Lession 3: Object-Oriented Programming - Huong Hoang Luong
26
Static Class
No instance created
Lession 3: Object-Oriented Programming - Huong Hoang Luong
27
Extension Method
◆ Extension methods allow you to extend an existing type with new
functionality without directly modifying those types
◆ Extension methods are static methods that have to be declared in a static
class
◆ We can declare an extension method by specifying the first parameter with
the this keyword
◆ The first parameter in this method identifies the type of objects in which the
method can be called
◆ The object that we use to invoke the method is automatically passed as the
first parameter
Lession 3: Object-Oriented Programming - Huong Hoang Luong
28
Extension Method
Lession 3: Object-Oriented Programming - Huong Hoang Luong
29
Expression-bodied Members
◆ Expression body definitions let you provide a member's implementation in a
very concise, readable form
◆ We can use an expression body definition whenever the logic for any
supported member, such as a method or property, consists of a single
expression
◆ An expression body definition has the following general syntax:
member => expression;
◆ Members can be: Method, Read-only property, Property, Constructor,
Finalizer and Indexer
Lession 3: Object-Oriented Programming - Huong Hoang Luong
30
Expression-bodied Members
Lession 3: Object-Oriented Programming - Huong Hoang Luong
31
Anonymous Type
◆ Is basically a class with no name and is not explicitly defined in code
◆ Uses object initializers to initialize properties and fields. Since it has no name,
we need to declare an implicitly typed variable to refer to it
◆ Anonymous types are class types that derive directly from object, and that
cannot be cast to any type except object
Lession 3: Object-Oriented Programming - Huong Hoang Luong
32
Object Initialize
◆ Object initializers let we assign values to any accessible fields or properties of
an object at creation time without having to invoke a constructor followed by
lines of assignment statements
◆ The object initializer syntax enables you to specify arguments for a constructor
or omit the arguments
class Customer {
public string Name { get; set; }
public int Age { get; set; }
}
Customer c = new Customer(); Can be combined with any
c.Name = "Jack"; constructor call
c.Age = 20;
var c = new Customer(){ Name = "Jack", Age = 20};
Lession 3: Object-Oriented Programming - Huong Hoang Luong
33
Readonly member and const keyword
In a field declaration, readonly indicates that assignment to the field can
only occur as part of the declaration or in a constructor in the same class
A readonly field can't be assigned after the constructor exits
A const field can only be initialized at the declaration of the field(a
compile-time constant)
A readonly field can be assigned multiple times in the field declaration
and in any constructor(a run-time constants)
Lession 3: Object-Oriented Programming - Huong Hoang Luong
34
Readonly member and const keyword
Lession 3: Object-Oriented Programming - Huong Hoang Luong
35
Record type
◆ Records type is a new reference type that we can create instead of classes or
structs
◆ Records are distinct from classes in that record types use value-based equality
◆ We define a record by declaring a type with the record keyword
Lession 3: Object-Oriented Programming - Huong Hoang Luong
36
Record type
Lession 3: Object-Oriented Programming - Huong Hoang Luong
37
Using Declarations
◆ With the using declaration, the objects are disposed automatically. Its scope is
automatically defined from the object’s declaration statement to the end of the
current code block
Lession 3: Object-Oriented Programming - Huong Hoang Luong
Summary
◆ Explain about OOP
◆ Explain classes and objects
◆ Define and describe methods
◆ Explain about the access modifiers
◆ Explain method overriding
◆ Define and describe inheritance
◆ Explain about polymorphism
◆ Explain about abstraction
Lession 3: Object-Oriented Programming - Huong Hoang Luong 38
Summary
◆ Discuss more new features in OOP :
▪ Properties, Auto-implemented properties
▪ Static class and Extension Method
▪ Anonymous Type and Default contructor
▪ Readonly members and Default interface methods
▪ Using declarations
▪ Expression-bodied members
▪ Record type
▪ Object Initialize
▪ Read-only auto-properties and Init-Only Properties
Lession 3: Object-Oriented Programming - Huong Hoang Luong 39
ĐẠI HỌC FPT CẦN THƠ