Lesson 1 Introduction To Object Oriented Programming
Lesson 1 Introduction To Object Oriented Programming
As computers increase in processing power, the software they execute becomes more complex. This increased complexity comes at a
cost of large programs with huge codebases that can quickly become difficult to understand, maintain and keep bug-free.
Object-oriented programming (OOP) tries to alleviate this problem by creating networks of objects, each like a small software
'machine'. These objects are naturally smaller entities, simplifying the development task of each unit. However, when the objects co-
operate in a system, they become the building blocks of much more complex solution.
Consider the motor car. If a car were designed as a single machine, it would be seen as hugely complex with lots of opportunities for
failure. However, when broken down into its constituent parts, such as wheels, pedals, doors, etc. the individual design items become
simpler. Each part (or object) is created independently, tested carefully and then assembled into the final product.
The car example is analogous to the object-oriented software. Rather than writing a huge program to create, for example, a project
management system, the solution is broken into real-world parts such as project, task, estimate, actual, deliverable, etc. Each of these
can then be developed and tested independently before being combined.
The basic building blocks of object-oriented programming are the class and the object. A class defines the available characteristics and
behavior of a set of similar objects and is defined by a programmer in code. A class is an abstract definition that is made concrete at
run-time when objects based upon the class are instantiated and take on the class’ behavior.
The basic syntax for the creation of a new class in C# is very simple. The keyword ‘class’ followed by the name of the new class is
simply added to the program. This is then followed by a code block surrounded by brace characters {} to which the class’ code will be
added. To demonstrate, let’s check the Dog class example:
namespace OOPExample
{
class Dog
{
}
}
The Dog class is empty in this example. To make it usable, we need to define its properties (something that will describe the class).
The class properties can be defined by declaring class-scoped variables using the private access modifier as can be seen below:
namespace OOPExample
{
class Dog
{
// class properties
private string _color;
private string _breed;
}
}
Here, we declared the properties _color and _breed that will describe the Dog class. However, since the properties are declared as
private, it is not accessible outside the Dog class. We need to define the access modifiers for the two properties. The access modifiers
are declared public in order for it to become accessible outside the Dog class. Let’s check the updated program code below:
namespace OOPExample
{
class Dog
{
// class properties
private string _color;
private string _breed;
}
}
Note: The datatype of the access modifiers must match the datatype of the properties they represent.
Our Dog class is now ready for implementation. To test it, we need to create another class (Program in our example) that contains the
Main method:
namespace OOPExample
{
class Program
{
static void Main(string[] args)
1 | Page
{
// object instantiation
Dog dog = new Dog();
Console.ReadKey();
}
}
}
In this example, we created an object named dog. Since dog is an object of the class Dog, all properties defined inside Dog is
accessible to dog using the notation object.Accessor as can be seen in the commands dog.Color and dog.Breed. You cannot directly
access the properties _color and _breed since they are private to the Dog class. Thus, writing dog._color and dog._breed will result to
an error. Sample output of the program is presented below:
C# also allows creation of auto-implemented properties. Auto-implemented properties will create the class-scoped variables
automatically behind the scenes so there’s no need for you to define these private variables. Applying auto-implemented properties,
the Dog class will be modified similar to the one below:
namespace OOPExample
{
class Dog
{
// class properties
public string Color {get;set;}
public string Breed {get;set;}
}
}
Adding Methods
A method defines the behavior of a class. To illustrate how to define a method, we will modify the Dog class to include a Bark( )
method that will display the string "Au Au" when called. The updated Dog class is presented below:
namespace OOPExample
{
class Dog
{
// class properties
public string Color {get;set;}
public string Breed {get;set;}
To call the method, you need to call it using the notation object.MethodName( ) as can be seen in the updated Program class:
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
namespace OOPExample
{
class Circle
{
// class properties
public string Color { get; set; }
public double Radius { get; set; }
Program.cs
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
// instantiates circle as an object of the class Circle
Circle circle = new Circle();
Console.ReadKey();
}
}
}
Sample Output:
Employee.cs
namespace OOPExample
{
class Employee
{
// class properties
public string LastName { get; set; }
public string FirstName { get; set; }
public double RatePerHour { get; set; }
// method definition for ComputeSalary()
public double ComputeSalary(int hoursWorked)
{
double salary;
salary = RatePerHour * hoursWorked;
3 | Page
return salary;
}
}
}
Program.cs
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
int hoursWorked;
Console.ReadKey();
}
}
}
Sample Output:
Payroll Information
Name: Reyes, Jessa
Salary: 22,020
namespace OOPExample
{
class StringManipulator
{
// class property
public string Str { get; set; }
Program.cs
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
int startingIndex;
Console.ReadKey();
}
}
}
namespace OOPExample
{
class Smartphone
{
// class properties
public string Model { get; set; }
public double InternalMemory { get; set; }
public double RemainingMemory { get; set; }
public bool isOn { get; set; }
Program.cs
5 | Page
namespace OOPExample
{
class Program
{
static void Main(string[] args)
{
int choice;
string appName;
double memoryRequirements;
switch(choice)
{
case 1:
phone.TurnOn();
break;
case 2:
phone.TurnOff();
break;
case 3:
Console.Write("\nEnter application name to be installed: ");
appName = Console.ReadLine();
Console.Write("\nEnter size of application in MB: ");
memoryRequirements = Convert.ToDouble(Console.ReadLine());
phone.InstallApplication(appName, memoryRequirements);
break;
case 4:
Console.Write("\nEnd of program...");
Console.ReadKey();
break;
default:
Console.Write("\nInvalid choice!");
break;
}
} while (choice!=4);
}
}
}
6 | Page