CH 3
CH 3
CHAPTER 3
C#
OBJECT ORIENTED PROGRAMMING
CONCEPTS
1. Introduction
2. Classes & Objects
3. Constructors & Destructors
4. Functions / Methods in C#
5. Properties and Indexer in C#
6. Inheritance and Polymorphism
7. Interfaces
01/24/2021
Object Oriented Programming
3
01/24/2021
Objected Oriented Principles
4
01/24/2021
Example: Abstraction
5
01/24/2021
6 01/24/2021
What Exactly is OOP?
7
Identifying an Object?
You can also think of other non physical things as objects:-
such as a bank account
A bank account is not something that can be physically touched
but intellectually we can consider a bank account to be an object.
OOP is a method of programming that involves the
creation of intellectuals objects that model a business
problem we are trying to solve.
In creating an OO program we define the properties of a
class of objects and then create individual objects from the
class
01/24/2021
Benefits of OOP Approach
8
Better abstraction
Modeling information and behavior together
Better maintainability
More comprehensible, less fragile software
Better usability
Classes as encapsulated components that can be used
in other systems
01/24/2021
What is Object?
9
Example
Dog
Attributes:breed, color, hungry, tired, etc.
Behaviors: eating, sleeping, etc.
Bank Account
Attributes:account number, owner, balance
Behaviors: withdraw, deposit
01/24/2021
What is Classes?
11
objects.
A class can be thought of as a template used to
number: 054
When the program runs there
balance: $19
will be many instances of the
Instance #2
account class. number: 712
balance: $941
Methods can only be invoked .
Instance Variable and Instance Methods
15
01/24/2021
Class Variables and Class Methods
16
01/24/2021
17
Account
class
variable
count: 3 num: 054 num: 712 num: 036
bal: $19 bal: $240 bal: $941
printCount()
Class
method 01/24/2021
Access Modifiers
18
01/24/2021
Encapsulation
21
accountNumber()
Methods
01/24/2021
Defining a Class
24
01/24/2021
25
Note
Access specifiers specify the access rules for the
01/24/2021
Creating Objects
27
01/24/2021
28
Accessing Objects
Referencing the object’s data
objectReference.data
mycircle.radius
Invoking the object’s method:
objectReference.method
myCircle.findArea()
01/24/2021
Defining Method
30
01/24/2021
Defining Field
31
01/24/2021
Static Members of a C# class
32
01/24/2021
Static Method/Function
33
1. Introduction
2. Classes & Objects
3. Functions / Methods in C#
4. Constructors & Destructors
5. Properties and Indexer in C#
6. Inheritance and Polymorphism
7. Interfaces
01/24/2021
Constructors
35
01/24/2021
37
Types of Constructors
Default Constructor
A constructor with no parameters is called a default
constructor (initializes all numeric fields to zero and all
string and object fields to null inside a class)
Parameterized Constructor
A constructor have at least one parameter is called a
paramerized constructor. (helps you to assign initial value to an
object at the time of its creation )
Copy Constructor
This constructor will creates an object by copying variables
from another object. (initialize a new instance to the values of
an existing instance) 01/24/2021
Example
38
class Employee
Employee u1= new Employee();
{
private name, location; Employee u2 = new
//Default Constructor Employee(“Moges”,”AA”);
public Employee()
{ Employee u3 = new Employee(u1) ;
name = "Alex Len";
location = "San Francisco";
}
//Parameterized Constructor
public Employee(string a, string b)
{
name = a;
location = b;
}
public Employee(Employee s)
{
name= s.name;
location= s.location
} 01/24/2021
Destructors
39
01/24/2021
40
Important Points:
A Destructor is unique to its class i.e. there cannot be more than
one destructor in a class.
A Destructor has no return type and has exactly the same name
as the class name (Including the same case).
It is distinguished apart from a constructor because of the Tilde symbol
(~) prefixed to its name.
A Destructor does not accept any parameters and modifiers.
It cannot be defined in Structures. It is only used with classes.
It cannot be overloaded or inherited.
It is called when the program exits.
The destructor will invoke automatically, whenever an instance
of class is no longer needed.
01/24/2021
Exercise 1:
41
01/24/2021
Exercise 3:
43
01/24/2021
Exercise 5:
45
01/24/2021
Contents
47
1. Introduction
2. Classes & Objects
3. Functions / Methods in C#
4. Constructors & Destructors
5. Properties in C#
6. Indexer in C#
7. Inheritance and Polymorphism
8. Interfaces
01/24/2021
Properties in C#
48
01/24/2021
Properties : get and set Accessors
49
Properties contain
getters (get{}) to retrieve the value of the underlying field and
setters (set{}) to set the value of the underlying field
Syntax of Properties
<acces_modifier> <return_type> <property_name>
{
get
{ }
set
{ }
} 01/24/2021
50
Example
private int length;
public int Length
{
get
{
return length;
}
set
{
length = value;
}
}
01/24/2021
51
We can also apply some additional logic in get and set, as in the
below example.
public int Length
{
get { return length/2; }
set {
if (value > 0)
length = value;
else
length = 0;
}
} 01/24/2021
53
Auto-implemented Property
property declaration has been made easy if
you don't want to apply some logic in get or
set.
The following is an example of an auto-
implemented property:
public int Length{ get; set; }
01/24/2021
Exercise 7:
54
01/24/2021
Exercises 8:
55
01/24/2021
Contents
57
1. Introduction
2. Classes & Objects
3. Functions / Methods in C#
4. Constructors & Destructors
5. Properties in C#
6. Indexer in C#
7. Inheritance and Polymorphism
8. Interfaces
01/24/2021
Indexers in C#
58
Syntax
[access_modifier] [return_type] this [argument_list]
{
get
{
// get block code
}
set
{
// set block code
}
}
01/24/2021
60
01/24/2021
Example
61
01/24/2021
62
01/24/2021
Inheritance
63
01/24/2021
Example: extending existing classes
65
01/24/2021
66 01/24/2021
67
Subclasses
When a new class is developed a programmer can
define it to be a subclass of an existing class.
Subclasses are used to define special cases,
extensions, or other variations from the originally
defined class.
Examples:
SavingsAccount and CheckingAccount can be derived
from the Account class (see the next slide).
01/24/2021
Examples: Base Classes and Derived Classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
69
Extending Classes
When you create a class that is an extension or child of another
class, you use a single colon (:) between the derived class name
and its base class name
Inheritance works in one direction
The keyword protected provides you with an intermediate
level of security
A protected data field or method can be used within its own class or in
any classes extended from that class, but it cannot be used by “outside”
classes
C# and Java support single inheritance, meaning that a
derived class can have only one parent class.
01/24/2021
71
01/24/2021
73
Output
01/24/2021
Overriding Methods
74
01/24/2021
75
01/24/2021
76
Note
1. The virtual keyword is used to modify a method,
property, indexer, or event declared in the base class
and allow it to be overridden in the derived class.
2. The override keyword is used to extend or modify a
virtual/abstract method, property, indexer, or event
of base class into derived class.
3. The new keyword is used to hide a method, property,
indexer, or event of base class into derived class
01/24/2021
78
01/24/2021
/* superclass Person */ /* subclass Student extending the Person class */
class Person class Student : Person
{ {
public override void method1()
public virtual void method1()
{
{
Console.WriteLine("Student class method 1");
Console.WriteLine("Person class method 1");
}
} public new void method2()
public void method2() {
{ Console.WriteLine(“Student class method 2");
Console.WriteLine("Person class method 2"); }
}
} }
class Program
{
public static void Main(String[] args)
{
Person s1 = new Person();
s1.method1();
s1.method2();
class Program
{
public static void Main(String[] args)
{
Student s1= new Student(); Student s2= new Student(7); Student s3 = new Student(4,5);
Console.ReadKey();
}
82 } 01/24/2021
Exercise-9
83
01/24/2021
Static Polymorphism
86
01/24/2021
Method Overloading
87
01/24/2021
using System;
namespace PolymorphismApplication
Output
{
class Printdata {
void print(int i) { Printing int: 5
Console.WriteLine("Printing int: “ + i ); Printing float: 500.263
}
Printing string:Hello C++
void print(double f) {
Console.WriteLine("Printing float: " + f);
}
void print(string s) {
Console.WriteLine("Printing string: “ + s);
}
static void Main(string[] args) {
Printdata p = new Printdata();
p.print(5);
p.print(500.263);
p.print("Hello C++");
Console.ReadKey();
}
}
}
90 01/24/2021
Operator Overloading
91
Syntax
access_specifier return_type operator Operator_symbol (parameters)
{ // Code }
Example
public static Box operator+ (Box b, Box c) {
Box box = new Box();
box.length = b.length + c.length;
box.breadth = b.breadth + c.breadth;
box.height = b.height + c.height;
return box;
}
The above function implements the addition operator (+) for a user-defined
class Box. It adds the attributes of two Box objects and returns the resultant
Box object.
01/24/2021
93
01/24/2021
Abstract Classes
95
01/24/2021
96
01/24/2021
97
01/24/2021
using System;
namespace PolymorphismApplication { Output
abstract class Shape {
public abstract int area();
Rectangle class area :
}
Area: 70
class Rectangle: Shape {
private int length;
private int width;
public Rectangle( int a = 0, int b = 0) {
length = a;
width = b;
}
public override int area () {
Console.WriteLine("Rectangle class area :");
return (width * length);
}
}
class RectangleTester {
static void Main(string[] args) {
Rectangle r = new Rectangle(10, 7);
double a = r.area();
Console.WriteLine("Area: “+a);
Console.ReadKey();
}
}
}
99 01/24/2021
Interface
100
Declaring Interface
You define an interface by using the interface keyword
It is similar to class declaration.
Interface statements are public by default.
Following is an example of an interface declaration
interface Iaddition
{
int add(int x, int y);
}
01/24/2021
102
class classname :
Interfacename {
body of classname
}
01/24/2021
103
Limitations of interface
Interfaces cannot have data members.
They cannot define constructors, destructors .
No member can be declared static.
Difference and similarities between interface and abstract
class
Similarities
Neither can be instantiated
Neither can be sealed
Differences
Interfaces cannot contain any implementation
Interfaces cannot declare non-public members
Interfaces cannot extend non-interfaces 01/24/2021
Exercise-10
105
Questions?
01/24/2021
107
Thank You
01/24/2021