.
NET Programming
Language
OOP Characteristics,Overload, Override, Static
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use
Agenda
1. OOP Characteristics
2. Method Overloading, Overriding
3. Static
4.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 2
Lesson Objectives
Understand aboutThis is a Characteristics,
OOP sample text that you can edit.
Overload,
01 Override, Static You can change font(size, color, name),
or apply any desired formatting.
OOP Characteristics
02 ✓ Inheritance
✓ Polymorphism
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 3
Section 1
OOP Characteristics (cont.)
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4
OOP Characteristics
▪ Abstraction:
✓ extracting only the required information from
objects
▪ Encapsulation
✓ Details of what a class contains need not be visible Abstraction
Polymorphism
to other classes and objects that use it
▪ Inheritance
Inheritance Encapsulation
✓ creating a new class based on the attributes and
methods of an existing class
▪ Polymorphism
✓ the ability to behave differently in different
situations
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 5
Inheritance
▪ Inheritance enables you to create new classes that reuse, extend, and
modify the behaviour defined in other classes.
✓ The class whose members are inherited is called the base class,
✓ The class that inherits those members is called the derived class.
✓ A derived class can have only one direct base class (single inheritance)
✓ Inheritance is transitive (multi-level inheritance)
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 6
Inheritance syntax
▪ To inherit from a class, use the : symbol
▪ Syntax
[<Modifier>] class <class name> : <parent class name>
▪ Example:
public class Avenger : Baby
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 7
Example
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 8
Inheritance Rules
▪ Rule 1: The execution of any child class starts by invoking its parent class's
default constructor by default.
▪ Rule 2: The child class can access its parent class's members but a parent
class can never access its child class member.
▪ Rule 3: In the same way an object of a class can also be assigned to its
parent class variable and make it as a reference, but in this scenario we are
also using the parent reference.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 9
Inheritance Rules
▪ Rule 4: Each and every class we define in .NET languages has an implicit
parent class (the class object) defined in the system namespace.
▪ Rule 5: Derived class cannot wider accessible than super class
Super class Derived class
internal internal
public public
internal public
public internal
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 10
Inheritance Rules
▪ If you don't want other classes to inherit from a class, use the sealed
keyword
▪ Use base keyword to access members of the base class from within a
derived class
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 11
Casting Objects
▪ An assignment of derived class object to ▪ An assignment of base class object to
a base class reference in C# inheritance derived class object is known as Down-
is known as up-casting. casting.
▪ The up-casting is implicit and any explicit ▪ Down-Casting is required in C#
typecast is not required. programming
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 12
Polymorphism
▪ Polymorphism is the ability to
behave differently in different
situations.
▪ It is basically seen in programs
where you have multiple methods
declared with the same name but
with different parameters and
different behaviour.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 13
Section 2
Method Overloading, Overriding
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 14
Method Overloading
▪ Method Overloading is the common way of implementing polymorphism.
▪ It is the ability to redefine a function in more than one form.
▪ A user can implement function overloading by defining two or more
functions in a class sharing the same name.
▪ C# can distinguish the methods with different method signatures.
▪ Constructor is considered as a special method => can have constructor
overloading
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 15
Method Overloading
▪ Overloading types:
✓ Difference number of parameters
✓ Difference data type of parameters
✓ Difference order of parameters with difference data type
✓ CANNOT: difference return values
16
Example
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 17
DO and AVOID
DO AVOID
DO try to use descriptive parameter names AVOID arbitrarily varying parameter
to indicate the default used by shorter names in overloads.
overloads. If a parameter in one overload represents
the same input as a parameter in another
overload, the parameters should have the
AVOID being inconsistent in the ordering
of parameters in overloaded members.
Parameters with the same name should
appear in the same position in all
overloads.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 18
DO and DON’T
DO DON’T
DO make only the longest overload DO NOT use ref or out modifiers to overload
virtual (if extensibility is required). members.
Shorter overloads should simply call
through to a longer overload.
DO allow null to be passed for optional DO NOT have overloads with parameters at
arguments. the same position and similar types yet with
different semantics.
DO use member overloading rather
than defining members with default
arguments.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 19
Method Overriding
▪ Method Overriding is a technique
that allows the invoking of
functions from another class (base
class) in the derived class
▪ Creating a method in the derived
class with the same signature as a
method in the base class is called
as method overriding.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 20
Method Overriding
▪ In C# we usually use 3 keywords for Method Overriding:
▪ virtual
✓ use within base class method. It is used to modify a method in base
class for overridden that particular method in the derived class.
▪ override
✓ use with derived class method. It is used to modify a virtual or abstract method
into derived class which presents in base class.
▪ base
✓ used to access members of the base class from derived class.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 21
Method Overriding
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 22
Rules
▪ Method overriding is possible only in derived classes.
✓ Because a method is overridden in the derived class from base class.
▪ A method must be a virtual or non-static method for override.
▪ Both the override method and the virtual method must have the same
access level modifier.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 23
Rules
▪ Overriding method can be override then override…
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 24
Summary: overload vs override
1. Creating more than one method 1. Creating a method in the derived
or function having same name class with the same signature as
but different signatures or the a method in the base class is
parameters in the same class is called as method overriding
called method overloading. 2. It is called runtime polymorphism
2. It is called the compile time 3. It must have same method name
polymorphism as well as the signatures or the
3. It has the same method name parameters.
but with different signatures or
the parameters
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 25
Summary: overload vs override
4. Method overloading doesn’t need 4. Method overriding needs
inheritance inheritance
5. Method overloading is possible in 5. Method overriding needs hierarchy
single class only level of the classes i.e. one parent
6. Access modifier can be any class and other child class.
7. Method overloading is also called 6. Access modifier must be public.
early binding. 7. Method overriding is also called
late binding.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 26
Section 3
Static
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 27
Static concept
▪ Use the static modifier to declare a static member, which belongs to the
type itself rather than to a specific object.
▪ static in use:
✓ To implement common functions. Example: Methods in Math or Convert class
✓ To provide some statistics data like: count number of user, ….
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 28
Static
static
static static static
method field class
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 29
static method
▪ A static method in C# is a method that keeps only one copy of the method
at the Type level, not the object level.
▪ That means, all instances of the class share the same copy of the method
and its data.
▪ The last updated value of the method is shared among all objects of that
Type.
▪ Static methods are called by using the class name, not the instance of the
class.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 30
static field
▪ Use static field create a single field that is shared among all objects created
from a single class.
▪ We can access the field by using the name of the class
▪ Static field can be used inside static method
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 31
static class
▪ Static classes cannot contain Instance Constructors.
▪ Static classes contain only static members.
▪ Static classes cannot be instantiated.
▪ Static classes are sealed. That means, you cannot inherit other classes
from instance classes.
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 32
THANK YOU!
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use