M. SC. (Computer Science)
Course Code : 23CsCmpP121
Course Name : DOT NET
PresentedBy : Priyanka Jadhav
Modern College of Arts,Science and Commerce ,Pune-5
Notes
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
• 2.2.1 Defining classes, class members, Interfaces,
properties
• 2.2.2 Access modifiers, Implementation of class, interface
and properties
• 2.2.3 Concept of hiding base class methods, Overriding
2.2.4 Event Handling
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Defining class
Object of class
Access modifier
Feature of OOPs
Inheritance
Abstraction
Polymorphism
Encapsulation
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
Access modifiers are keywords used to specify the
declared accessibility of a member or a type.
 Access modifiers are an integral part of object-
oriented programming. They support the concept
of encapsulation, which promotes the idea of
hiding functionality. Access modifiers allow you to
define who does or doesn't have access to certain
features.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
It’s special kind of modifiers using which we can
define the scope of a type(class) and it’s members.
1. Private
2. Internal
3. Protected
4. Protected internal
5. Public
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Access modifier
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Inheritance
Inheritance is one of the three foundational principles of
Object-Oriented Programming (OOP) because it allows
the creation of hierarchical classifications. Using
inheritance you can create a general class that defines
traits common to a set of related items. This class can
then be inherited by other, more specific classes, each
adding those things that are unique to it.
In the language of C#, a class that is inherited is called a
base class. The class that does the inheriting is called the
derived class. Therefore a derived class is a specialized
version of a base class.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
E
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Abstraction in C# (and .NET in general) is a fundamental object-
oriented programming concept that involves hiding complex
implementation details and exposing only the necessary parts of an
object. This makes it easier to manage and work with objects by
reducing complexity and increasing reusability.
In C#, abstraction can be achieved using abstract classes and
interfaces.
Abstract Classes
An abstract class cannot be instantiated directly and may contain
abstract methods (methods without a body) that must be
implemented by derived classes. It can also include concrete
methods (methods with a body)..
Interfaces
An interface defines a contract that implementing classes
must follow. Interfaces cannot contain any implementation,
only method, property, event, or indexer declarations.
Example: Visual studio(AbstractClassExample)
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Polymorphism
• Polymorphism is one of the core principles of object-
oriented programming (OOP) and refers to the ability of
different classes to be treated as instances of the same
class through inheritance. In C#, polymorphism allows you
to define methods in a base class and override them in
derived classes. There are two types of polymorphism in
C#:
• Compile-time polymorphism (Method Overloading)
• Run-time polymorphism (Method Overriding)
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Compile-time Polymorphism (Method Overloading)
• Method overloading occurs when you have multiple
methods in the same class with the same name but
different parameters.
Run-time Polymorphism (Method Overriding)
• Method overriding occurs when a base class declares a
method and a derived class overrides this method with its
own implementation.
Example: Visual Studio(PolymorphismExample)
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Encapsulation
Encapsulation is one of the four fundamental principles of object-
oriented programming (OOP). It refers to the bundling of data
(attributes) and methods (functions) that operate on the data into a
single unit or class.
Key Points of Encapsulation
• Data Hiding: Internal object details (data members) are hidden
from the outside world and only accessible through public
methods.
• Controlled Access: Access to the data is controlled through
getters and setters.
Example: Visual studio(EncapsulationExample)
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
E
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Constructors
• In C#, a constructor is a special method of a class that is
automatically called when an instance of the class is created. It
is used to initialize the object’s data members. Constructors
have the same name as the class and do not have a return
type, not even void.
E
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Types of Constructors
Default Constructor:
Automatically provided by C# if no constructors are defined.
Initializes fields to their default values.
Parameterized Constructor:
Takes parameters to allow different ways of initializing an
object.
Copy Constructor:
Initializes an object using another object of the same class.
Static Constructor:
Used to initialize static members of the class.
Key Points
• Default Constructor: No parameters, initializes fields to
default values.
• Parameterized Constructor: Accepts parameters to initialize
fields.
• Copy Constructor: Initializes a new object as a copy of an
existing object.
• Static Constructor: Initializes static members of the class,
called once, before any instance is created or any static
members are referenced.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Function overloading
Function overriding(Virtual function)
Hiding methods
Function overloading
• Function overloading allows multiple methods in the same scope
to have the same name but different signatures (i.e., different
parameter lists)
Example : Function Overloading same as polymorphism
Function overriding
• Function overriding refers to the ability to define a method in a
subclass that has the same name and signature as a method in its
superclass. When a method is overridden, the subclass's version
of the method is called instead of the superclass's version.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Example : Function Overriding
class Animal:
def speak(self):
print("Some generic animal sound")
class Dog(Animal):
def speak(self):
print("Woof!")
# Example usage
animal = Animal()
animal.speak() # Output: Some generic animal sound
dog = Dog()
dog.speak() # Output: Woof!
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Hiding methods
• Hiding methods is often done using the new keyword.
This allows a subclass to define a method with the same
name as a method in its base class, but the base class's
method is hidden rather than overridden. This is different
from method overriding, where the base class method is
overridden and calls the overridden method in the
subclass.
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts
Chapter 2
Introduction to C#
2.2 Object Oriented Concepts

Unit2_4.pptx Object Oriented Concepts .

  • 1.
    M. SC. (ComputerScience) Course Code : 23CsCmpP121 Course Name : DOT NET PresentedBy : Priyanka Jadhav Modern College of Arts,Science and Commerce ,Pune-5
  • 2.
  • 3.
    2.2 Object OrientedConcepts • 2.2.1 Defining classes, class members, Interfaces, properties • 2.2.2 Access modifiers, Implementation of class, interface and properties • 2.2.3 Concept of hiding base class methods, Overriding 2.2.4 Event Handling Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 4.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Defining class Object of class Access modifier Feature of OOPs Inheritance Abstraction Polymorphism Encapsulation
  • 5.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Access modifier Access modifiers are keywords used to specify the declared accessibility of a member or a type.  Access modifiers are an integral part of object- oriented programming. They support the concept of encapsulation, which promotes the idea of hiding functionality. Access modifiers allow you to define who does or doesn't have access to certain features.
  • 6.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Access modifier It’s special kind of modifiers using which we can define the scope of a type(class) and it’s members. 1. Private 2. Internal 3. Protected 4. Protected internal 5. Public
  • 7.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Access modifier
  • 8.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Inheritance Inheritance is one of the three foundational principles of Object-Oriented Programming (OOP) because it allows the creation of hierarchical classifications. Using inheritance you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called the derived class. Therefore a derived class is a specialized version of a base class.
  • 9.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts
  • 10.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts
  • 11.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts
  • 12.
    E Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Abstraction in C# (and .NET in general) is a fundamental object- oriented programming concept that involves hiding complex implementation details and exposing only the necessary parts of an object. This makes it easier to manage and work with objects by reducing complexity and increasing reusability. In C#, abstraction can be achieved using abstract classes and interfaces. Abstract Classes An abstract class cannot be instantiated directly and may contain abstract methods (methods without a body) that must be implemented by derived classes. It can also include concrete methods (methods with a body)..
  • 13.
    Interfaces An interface definesa contract that implementing classes must follow. Interfaces cannot contain any implementation, only method, property, event, or indexer declarations. Example: Visual studio(AbstractClassExample) Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 14.
    Polymorphism • Polymorphism isone of the core principles of object- oriented programming (OOP) and refers to the ability of different classes to be treated as instances of the same class through inheritance. In C#, polymorphism allows you to define methods in a base class and override them in derived classes. There are two types of polymorphism in C#: • Compile-time polymorphism (Method Overloading) • Run-time polymorphism (Method Overriding) Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 15.
    Compile-time Polymorphism (MethodOverloading) • Method overloading occurs when you have multiple methods in the same class with the same name but different parameters. Run-time Polymorphism (Method Overriding) • Method overriding occurs when a base class declares a method and a derived class overrides this method with its own implementation. Example: Visual Studio(PolymorphismExample) Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 16.
    Encapsulation Encapsulation is oneof the four fundamental principles of object- oriented programming (OOP). It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit or class. Key Points of Encapsulation • Data Hiding: Internal object details (data members) are hidden from the outside world and only accessible through public methods. • Controlled Access: Access to the data is controlled through getters and setters. Example: Visual studio(EncapsulationExample) Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 17.
    E Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Constructors • In C#, a constructor is a special method of a class that is automatically called when an instance of the class is created. It is used to initialize the object’s data members. Constructors have the same name as the class and do not have a return type, not even void.
  • 18.
    E Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Types of Constructors Default Constructor: Automatically provided by C# if no constructors are defined. Initializes fields to their default values. Parameterized Constructor: Takes parameters to allow different ways of initializing an object. Copy Constructor: Initializes an object using another object of the same class. Static Constructor: Used to initialize static members of the class.
  • 19.
    Key Points • DefaultConstructor: No parameters, initializes fields to default values. • Parameterized Constructor: Accepts parameters to initialize fields. • Copy Constructor: Initializes a new object as a copy of an existing object. • Static Constructor: Initializes static members of the class, called once, before any instance is created or any static members are referenced. Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 20.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts Function overloading Function overriding(Virtual function) Hiding methods
  • 21.
    Function overloading • Functionoverloading allows multiple methods in the same scope to have the same name but different signatures (i.e., different parameter lists) Example : Function Overloading same as polymorphism Function overriding • Function overriding refers to the ability to define a method in a subclass that has the same name and signature as a method in its superclass. When a method is overridden, the subclass's version of the method is called instead of the superclass's version. Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 22.
    Example : FunctionOverriding class Animal: def speak(self): print("Some generic animal sound") class Dog(Animal): def speak(self): print("Woof!") # Example usage animal = Animal() animal.speak() # Output: Some generic animal sound dog = Dog() dog.speak() # Output: Woof! Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 23.
    Hiding methods • Hidingmethods is often done using the new keyword. This allows a subclass to define a method with the same name as a method in its base class, but the base class's method is hidden rather than overridden. This is different from method overriding, where the base class method is overridden and calls the overridden method in the subclass. Chapter 2 Introduction to C# 2.2 Object Oriented Concepts
  • 24.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts
  • 25.
    Chapter 2 Introduction toC# 2.2 Object Oriented Concepts