Chapter 3 WP With C# Object Oriented Programming
Chapter 3 WP With C# Object Oriented Programming
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
11/10/2021
Object Oriented Programming
3
11/10/2021
Objected Oriented Principles
4
11/10/2021
Example: Abstraction
5
11/10/2021
6 11/10/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
11/10/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
11/10/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
11/10/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
11/10/2021
Class Variables and Class Methods
16
11/10/2021
17
Account
class
variable
count: 3 num: 054 num: 712 num: 036
bal: $19 bal: $240 bal: $941
printCount()
Class
method 11/10/2021
Access Modifiers
18
11/10/2021
Encapsulation
21
accountNumber()
Methods
11/10/2021
Defining a Class
24
11/10/2021
25
Note
Access specifiers specify the access rules for the
11/10/2021
Creating Objects
27
11/10/2021
28
Accessing Objects
Referencing the object’s data
objectReference.data
mycircle.radius
Invoking the object’s method:
objectReference.method
myCircle.findArea()
11/10/2021
Defining Method
30
11/10/2021
Defining Field
31
11/10/2021
Static Members of a C# class
32
11/10/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
11/10/2021
Constructors
37
11/10/2021
39
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
parameterized 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) 11/10/2021
Example
40
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
} 11/10/2021
}
Destructors
41
11/10/2021
42
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.
11/10/2021
Exercise 1:
43
11/10/2021
Exercise 3:
45
11/10/2021
Exercise 5:
47
11/10/2021
Contents
49
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
11/10/2021
Properties in C#
50
11/10/2021
Properties : get and set Accessors
51
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
{ }
} 11/10/2021
52
Example
private int length;
public int Length
{
get
{
return length;
}
set
{
length = value;
}
}
11/10/2021
53
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;
}
} 11/10/2021
55
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; }
11/10/2021
Exercise 7:
56
11/10/2021
Exercises 8:
57
11/10/2021
58
11/10/2021
Contents
59
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
11/10/2021
Indexers in C#
60
Syntax
[access_modifier] [return_type] this [argument_list]
{
get
{
// get block code
}
set
{
// set block code
}
}
11/10/2021
62
11/10/2021
Example
63
11/10/2021
64
11/10/2021
Inheritance
65
11/10/2021
Example: extending existing classes
67
11/10/2021
68 11/10/2021
69
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).
11/10/2021
Examples: Base Classes and Derived Classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
71
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.
11/10/2021
73
11/10/2021
75
Output
11/10/2021
Overriding Methods
76
11/10/2021
77
11/10/2021
78
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
11/10/2021
80
11/10/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();
}
84 } 11/10/2021
Exercise-9
85
11/10/2021
Static Polymorphism
88
11/10/2021
Method Overloading
89
11/10/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();
}
}
}
92 11/10/2021
Operator Overloading
93
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.
11/10/2021
95
11/10/2021
Abstract Classes
97
11/10/2021
98
11/10/2021
99
11/10/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();
}
}
}
101 11/10/2021
Interface
102
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);
}
11/10/2021
104
class classname :
Interfacename {
body of classname
}
11/10/2021
105
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 11/10/2021
Exercise-10
107
Questions?
11/10/2021
109
Thank You
11/10/2021