chapter three oop concept
chapter three oop concept
Object-Oriented
Fundamentals in C#.NET
By Deresse Demeke
By Deresse Demeke 1
Objects and Classes
• In OOP, you use objects in your programs to encapsulate the data
associated with the entities with which the program is working.
• For example, a human resources application needs to work with
employees. Employees have attributes associated with them that
need to be tracked. You may be interested in such things as the
employee names, addresses, departments, and so on.
• Although you track the same attributes for all employees, each
employee has unique values for these attributes.
• In the human resources application, an Employee object obtains and
modifies the attributes associated with an employee.
• In OOP, the attributes of an object are referred to as properties.
By Deresse Demeke 2
contd
• Along with the properties of the employees, the human
resource application also needs an established set of
behaviors exposed by the Employee object.
• For example, one employee behavior of interest to the
human resources department is the ability to request time
off.
• In OOP, objects expose behaviors through methods. The
Employee object contains a RequestTimeOff method that
encapsulates the implementation code.
By Deresse Demeke 3
contd
• The properties and methods of the objects used in OOP are defined
through classes.
• class is a blueprint that defines the attributes and behaviors of the
objects that are created as instances of the class.
Defining Classes
• Let’s walk through the source code needed to create a class
definition. The first line of code defines the code block as a class
definition using the keyword class followed by the name of the
class.
• The body of the class definition is enclosed by an open and closing
curly bracket({ …..}).
• Example class Employee{…….
}
By Deresse Demeke 4
Creating Class Properties
• The Private keyword ensures that these instance variables
can be manipulated only by the code inside the class.
• Here are the instance variable definitions:
➢ private int _empID;
➢ private string _name;
• When a user of the class (client code) needs to query or
set the value of these instance variables, public
properties are exposed to them.
• Inside the property block of code are a Get block and a
Set block.
By Deresse Demeke 5
contd
• The Get block returns the value of the private instance
variable to the user of the class. This code provides a
readable property.
• The Set block provides a write-enabled property; it
passes a value sent in by the client code to the corresponding
private instance variable.
• Here is an example of a property block:
public string Name{
get { return _name; }
set { _name = value; }
}
By Deresse Demeke 6
contd
• There may be times when you want to restrict access to a
property so that client code can read the property value
but not change it.
• By eliminating the Set block inside the Property
block, you create a read-only property.
• The following code shows how to make the EmployeeID
property read-only:
1. public int EmployeeID
2. {
3. get { return _empID; }
4. }
By Deresse Demeke 7
Data Encapsulation
• Data encapsulation means that the client code does not have
direct access to the data.
• When working with the data, the client code must use clearly
defined properties and methods accessed through an instance
of the class.
• The following are some of the benefits of
encapsulating the data in this way:
1. Preventing unauthorized access to the data.
2. Ensuring data integrity through error checking.
3. Creating read-only or write-only properties.
4. Isolating users of the class from changes in the
implementation code.
By Deresse Demeke 8
Example
1. public string Password
2. {
3. get { return _password; }
4. set
5. {
6. if (value.Length >= 6)
7. {
8. _password = value;
9. }
10. else
11. {
12. throw new Exception("Password must be at least 6 characters");
13. }
14. } By Deresse Demeke 9
Creating Class Methods
• Class methods define the behaviors of the class.
• For example, the following defines a method for the Employee class that verifies employee
logins:
• public void Login(string loginName, string password){
• if (loginName == "Jones" & password == "mj"){
• _empID = 1;
• Department = "HR";
• Name = "Mary Jones";
• }
• else if (loginName == "Smith" & password == "js"){
• _empID = 2;
• Department = "IS";
• }
• else{
• throw new Exception("Login incorrect.");
• }
• }
By Deresse Demeke 10
Using Constructors
• In OOP, you use constructors to perform any processing that needs
to occur when an object instance of the class becomes instantiated.
• The class constructor method is named the same as the class.
• When an object instance of a class is instantiated by client code, the
constructor method is executed.
• The following constructor is used in the Employee class to initialize
the properties of an object instance of the Employee class.
• public Employee(int empID){
• if (empID == 1)
•{
• _empID = 1;
• Department = "IT";
•}
•}
By Deresse Demeke 11
Overloading Methods
• You overload methods in a class by defining multiple
methods that have the same name but contain different
signatures.
• A method signature is a combination of the name of
the method and its parameter type list.
• If you change the parameter type list, you create a
different method signature.
• For example, the parameter type lists can contain a
different number of parameters or different parameter
types.
• The compiler will determine which method to execute by
examining the parameter type list passed in by the client.
By Deresse Demeke 12
Student class definition
• using System;
• using System.Collections.Generic;
• using System.Linq;
• using System.Text;
• using System.Threading.Tasks;
• namespace ChapterTwo
• {
• public class Student
• {
• private string name;
• private string id;
• private string department;
• private static int _count = 0;
• public Student(string name, string id, string department)
• {
• _count++;
• this.name = name;
• this.id = id;
• this.department = department;
• }
By Deresse Demeke 13
• public string Name
• {
• get { return name; }
• set { name = value; }
• }
• public string Id
• {
• get { return id; }
• set { id = value; }
• }
• public string Department
• {
• get { return department; }
• set { department = value; }
• }
• public void countstudent()
• {
• Console.WriteLine(_count);
• }
• }
• }
By Deresse Demeke 14
The StudentDriver class
• namespace ChapterTwo
• {
• using System;
• public class StudentDriver
• {
• public static void Main(string[] args)
• {
• Student stud = new Student("Abebe", "ITR/0012/2010",
"IT");
• Console.WriteLine("*******Here below is the detail of
student******");
• Console.WriteLine(string.Format("My name is {0} \n " +
• " My ID number is {1} \n" +
• " I'm departmen of {2} student"
• ,stud.Name, stud.Id,stud.Department));
• st.countstudent();
• }
• }
• }
By Deresse Demeke 15
Inheritance in c#
• Inheritance allows a new class to extend an existing class.
By Deresse Demeke 16
• Inheritance involves a base class and a derived class.
• The base class is the general class and the derived class is the
specialized class.
• You can think of the derived class as an extended version of the
base class.
• The derived class inherits fields, properties, and methods from
the base class without any of them having to be rewritten.
• Furthermore, new fields, properties, and methods may be added
to the derived class, and that is what makes it a specialized
version of the base class.
By Deresse Demeke 17
Base Class and Derived Class Constructors
By Deresse Demeke 19
Two essential ingredients of polymorphic behavior:
• The ability to define a method in a base class and then define a
method with the same name in a derived class.
• When a derived class method has the same name as a
base class method, it is often said that the derived class method
overrides the base class method.
• The ability to call the correct version of an overridden method,
depending on the type of object that is used to call it.
• If a derived class object is used to call an overridden method,
then the derived class’s version of the method is the one that
executes.
• If a base class object is used to call an overridden method, then
the base class’s version of the method is the one that executes.
By Deresse Demeke 20
Virtual and override key word
• Notice the virtual keyword that appears in the method
header of the base/superclass.
• The virtual keyword declares that a derived class is
allowed to override this method.
• The override keyword declares that this
method overrides a method in the base class.
By Deresse Demeke 21
The “Is a” Relationship Does Not Work in Reverse
• It is important to understand that the “is a” relationship does
not work in reverse.
Although the statement “a dog is an animal” is true, the statement
“an animal is a dog”is not true.
• This is because all dogs are animals, but not all animals are dogs.
So, the following statement will not compile:
Dog myDog = new Animal("Dog");
• You cannot assign an Animal reference to a Dog
variable.
• This makes sense because Dog objects have capabilities
that go beyond those of an Animal object.
By Deresse Demeke 22
Contd….
By Deresse Demeke 23