Chapter 4 OOP
Chapter 4 OOP
Object-Oriented Programming
with C#
Objectives
Public interface
Refers to the set of members that are directly accessible
from an object variable via the dot operator
Is any item declared in a class using the public keyword
May be populated by: methods, properties and
constant/read-only fields
C#’s Encapsulation services
Override methods
Is an instance method declaration includes an override modifier
Overridden base method
Is a method overridden by an override declaration
Is a virtual, abstract, or override method
Override method and overridden base method
Have the same return type
Have the same declared accessibility
C#’s Inheritance and Polymorphic support
base keyword
Used to access the members of a base class from within
a derived class
Used to call constructors of a base class while creating
an instance of a derived class
Using the keyword base in a static method will result in
an error.
C#’s Inheritance and Polymorphic support
override keyword
Used to modify a method
An override method provides a new implementation of the
base method. The base method should be declared as
virtual
Accessibility level of a base method cannot be changed by a
method overriding it
Keyword new, static, virtual cannot be used along with
override modifier
(Read more at page 162, Main book)
C#’s Inheritance and Polymorphic support
virtual keyword
Is used in the definition of a method to support
polymorphism
Used to modify method declaration in a class
Child classes are free to implement their own versions of
virtual method using override keyword
Virtual modifier cannot used with modifiers like static and
override
(Read more at page 162, Main book)
C#’s Inheritance and Polymorphic support
new keyword
Used as an operator or as a modifier
new modifier is used to explicitly hide a member that is
inherited from the base class
It is an error to use both new and override on the same
method
(Read more at page 169, Main book)
C#’s Inheritance and Polymorphic support
Sealing a class
A class is sealed when no class should be allowed to
inherit from that class
Keyword sealed is used to seal a class
(Read more at page 158, Main book)
Q&A