Web Design that taps into the haromny and vision of your dreams.

Introduction to Object Oriented Programming

What is Object Oriented Programming anyway?

Written By on in C#

1,221 words, estimated reading time 6 minutes.

The object-oriented approach to programming tries to represent abstract or concrete things of the real world in a type of computer code called classes. This article is designed to give you an overview of the concept of Object Oriented Programming.

What is Object Oriented Programming?

Object Oriented Programming is a branch of computer programming which is centred around objects rather than actions, data rather than logic. Objects are modelled after things in the real world, or creating data models. By modelling data and things, a set of related properties and actions can be created and interacted with.

Object Oriented Programming takes the view that the objects we want to manipulate are more important than the logic required to manipulate them.

What is a class?

A class can be thought of as a blueprint for a noun, e.g. a person, place or thing. Noun's have attributes that describe them such as Size, Mass, Temperature, Speed and so on. These attributes are called fields.

A noun can also have actions or processes associated with it. For example a car could have an action of Accelerate, which would affect the attribute of Speed. These actions are called methods.

A class cannot be used by itself, but it is used to instantiate (create) an instance of an object, which shares all the same fields and methods. Because it is a blueprint, you can create many, many instances, all with the same fields and methods. An instance of a class is called an object. One thing you must keep in mind is that each object that is created from a class is unique; each instance of a class will have its own values for the fields, totally separate from the values of the other objects.

This concept of creating one class that can be reused many times is the foundation of Object Oriented Programming and effective code reuse.

A class should be as specific to the task as possible and only contain nouns dedicated to the task for which the class is to perform.

Why use Classes?

Now, let's have a look at where Object Oriented Programming really comes into its own. There is a concept known as inheritance, where one class can inherit all the methods and fields of a parent, or base, class and also add new functionality, or replace existing methods. This allows a new class to be more focused, targeted towards the object being modelled.

To put that into English with the car example, every car manufacturer uses the same base class. This class defines a basic car with four wheels, a steering wheel, brakes, headlights, seats etc... However if every manufacturer only used this blueprint, every car produced would be exactly the same - what would be the point?

What we can do is take the functionality from the base class and create another car class and add functionality to it. We can add central locking, water temperature and paint. A different manufacturer may use trim colour and oil temperature and ABS brakes. We can override the methods from the base class, so that the basic seats are now heated leather seats, the headlights are HID.

Classes can also use other classes. Our base car class can use a base engine class. Our new car class can override this base engine class to use a super turbo charged jet powered engine if we wish.

Classes in C#

A C# application is in essence a collection of classes, structures (shortened to struct) and types.

Firstly, classes and structs are essentially the same, apart from two key differences.

  • Classes are a reference type, structs are value types
  • Classes can use inheritance whereas structs cannot.

You can read more about the differences between reference and value types in the data types article.

Defining a Class

A class is introduced using the class keyword, followed by the name of the class you are creating. Because a class is a block of code, it requires the use of curly brackets (braces).

Let's use the example above and create a Car class. I've called the class called baseCarClass because we will be using this class as a base for new classes in future tutorials.

In the example we identified several fields and methods associated with a car class.

We are going to create a sample application using the baseCarClass, so create a new console project, and copy the following code into the window.

class baseCarClass
{
}

We can now add in variable declarations for some of the properties of the base car, for example paint colour, number of seats and direction.

The .Net Framework, nor the compiler are affected by the order in which you declare fields or methods; however it greatly improves readability if you declare fields and properties at the start of a class, followed by its methods.

class baseCarClass
{
  public string Colour;
  public int numberOfSeats;
  public int direction;
  public string modelName;
}

So far all we have done is create a blueprint for a class. We shall now see how to create an actual car object.

A class is a user-defined type so we use the same syntax as declaring an int (for example), but a class also needs initialising with the new keyword. This wills instantiate an instance of the class on the heap.

In the Main method of the console application, create an instance of the baseCarClass like this:

static void Main()
{
  baseCarClass myCar = new baseCarClass();
}

We can now set the various attributes of the car.

In your Main method, create an instance of the baseCarClass:

static void Main()
{
  baseCarClass myCar = new baseCarClass();
  myCar.Colour = "blue";
  myCar.numberOfSeats = 4;
  myCar.modelName = "Demo Car MKI";
}

Using the class as a blueprint means that we can create multiple cars with different values for each of the properties.

static void Main()
{
  baseCarClass myCar = new baseCarClass();
  myCar.Colour = "blue";
  myCar.numberOfSeats = 4;
  myCar.modelName = "Demo Car MKI";
 
  baseCarClass myNewCar = new baseCarClass();
  myNewCar.Colour = "yellow";
  myNewCar.numberOfSeats = 2;
  myNewCar.modelName = "Sports Car MKI";
}

Classes as a Data Type

At this stage our car does not have an engine, so it's pretty useless. There are many different types of engine that a car can have and we are going to model a base engine as we have created a base
car. This way our cars can have a variety of engines.

class baseEngineClass
{
  public string name;
  public int numberOfCylinders;
  public int MPG;
  public int acceleration;
}

We can add one class to another in the same way as we add an integer or string. A class is simply another data type.

Let's add the engine to our car class.

class baseCarClass
{
  public string Colour;
  public int numberOfSeats;
  public int direction;
  public string modelName;
  public baseEngineClass  engine;
}

Now all that is left to do is create an engine and give it to a car.

static void Main()
{
  baseCarClass myCar = new baseCarClass();
  myCar.Colour = "blue";
  myCar.numberOfSeats = 4;
  myCar.modelName = "Demo Car MKI";
 
  myCar.engine = new baseEngineClass();
  myCar.engine.name = "Slow Engine"
  myCar.engine.numberOfCylinders  = 4;
  myCar.engine.MPG = 50;
  myCar.engine.acceleration = 2;
}

This has been a very basic introduction to Object Oriented Programming using C#, in the next tutorial we will look at class inheritance and see how we can extend the car and engine classes further and make more unique cars. We will also look at class constructors which will allow the all of the car and engine properties to be set inside the class so that your application code does not have to get involved.

Last updated on: Friday 23rd June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me