You are on page 1of 10

•Classes

•A data structure that encapsulates a set of data and behaviors that belong together as a
logical unit
•A blueprint that is used to create instances or objects at run time
•A class is a reference type
•Click here for class implementation
•A class is usually created to contain members

•Members:
• All methods, fields, constants, properties, and events must be declared within a class;
these are called the members of the class
•Objects of a class
• A class is like a blueprint that specifies what it can do whereas object is a block of memory
that has been allocated and configured according to the blueprint
• An object is referred to as an instance of a class
• An object in C# is always initialized with new operator

• Syntax:
• Student _student = new Student();
• _student.name = “Ali”;
• _student.degree = “BSCS”;
• OR
• Student _student = new Student()
• {
• name = “Fahad",
• degree = “MSCS"
• };
•Encapsulation
•It is sometimes referred to as the first pillar or principle of object-oriented
programming
•A class or struct can specify how accessible each of its members to outside of the
class or struct
•Methods and variables that are not intended to be used from outside of the class or
assembly can be hidden
•Information hiding
•Inheritance
•C# completely supports inheritance; a fundamental characteristic of OOP
•The idea of inheritance implements the IS-A relationship. For example, mammal IS
A animal, dog IS-A mammal hence dog IS-A animal as well, and so on
•When a class derives from a base class, it inherits all the members of the base class
except the constructors
•Unlike C++, C# does not allow multiple inheritance (a class can have directly one
base class)
•A class can indirectly inherit multiple classes such as child class (C) of child class
(B) of base class (A)
•Click here for an example
•Inheritance (cont.)
•Sealed classes:
• Classes cannot inherit a class which is defined as sealed
•Abstract classes:
• abstract modifier indicates that the thing being modified has a missing or
incomplete implementation
• An abstract class contains abstract methods that have a signature definition but no
implementation
• Abstract classes cannot be instantiated
• They can only be used through derived classes that implement the abstract methods
• A non-abstract class derived from an abstract class must include implementations
of all inherited abstract methods
•Inheritance (cont.)
•Abstract classes:
• Abstract method declarations are only permitted in abstract classes

• abstract class Shape{


• public abstract int GetArea();
• }

• class Square : Shape {


• public override int GetArea() { … }
• }
Polymorphism
•Polymorphism is often referred to as the third pillar of object-oriented programming
•Polymorphism is a Greek word that means "many-shaped“

•Types,
• (i) Static polymorphism
• (ii) Dynamic polymorphism
Polymorphism (cont.)
•Static/Compile-time Polymorphism:
• The response to a function is determined at the compile time
• The mechanism of linking a function with an object during compile time is called
early binding or static binding
• C# provides two techniques to implement static polymorphism
• Function overloading (click here)
• Operator overloading

•Dynamic/Run-time Polymorphism
• It is decided at run-time
• Click here for an example
Abstract vs Concrete Classes
•Abstract Classes:
• Abstract classes are the classes which cannot be used to instantiate objects.
• Ex: Shape _shape = new Shape(); //Compile-time error
• These classes are intended to be derived and then provided with the missing
implementations.

•Concrete Classes
• Classes that can be used to instantiate objects are called concrete classes.
Classes Overview
•Classes
•Objects
•Encapsulation
•Inheritance
•Polymorphism
• Static (Compile-time) polymorphism
• Dynamic (Run-time) polymorphism
•Abstract classes
•Sealed classes

You might also like