Wednesday, April 18, 2018

Lesson 09: Basic Concepts of Object Oriented Programming – Part I



Basic Concepts of Object Oriented Programming – Part I

C# is an object oriented language; and in this section of our journey to learn C#, we are going to see the object orientation features of C#. First, before we delve into the nuts and bolts of object orientation aspects of C#, we are going to imbibe some basic theoretical concepts of any object oriented programming language and then we will slowly dive into the practical aspects of the object orientation pieces of C#.


We will begin by defining the term object oriented programming. From Wikipedia, the free encyclopedia:
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

 
Classes and Objects

Object oriented programming is a programming pattern based on an object. An object is an instance of a class; and a class is a software constructs that serves as a blueprint or prototype for creating objects. Hence, before objects come to life by being available in a computer memory for some kind of functionality, they need to be defined and modeled using a class.

Hence, a class can be defined as a user defined type; it is a plan used to create real world objects such as receipts, cars, computers, printers and so on.

Members of a Class

A class can contain two types of members - data and code. The data members of a class are the information that a class stored; and they are also termed as the attributes, properties, fields or member variables of a class. These are the variables that a class stores in it; and their values determine the state of an object at a particular point and time.

Take for instance a Student as a class. A student can have such attributes as student id, first name, last name and age. These are the data members of the student class.

On the other hand, the action that an object performs are stored in a code member of a class and it is also termed as member method or member function. A student can take certain actions. For instance, a student can register for a course and he or she can take an exam and finally a student can receive his or her final result. These are some instances of methods, functions or actions a student object undergoes.

We can summarize the above discussion of the members of a class using a picture as illustrated in the figure given below.

The following are some real world illustrations of classes that might further assist you in understanding the concepts behind a class.
















Creating a class
In C#, to create a class we use the class keyword and we put the definition of the class in a curly brace as shown below.
      
       class Student
       {
             // member fields and methods of the class are put here
}

To add member fields to the class we proceed as follows:

       class Student
       {
        // member fields or member variables
        public int id;
        public string firstName;
        public string lastName;
        public byte age;
       }


Finally we can add behavior or method to the class as follows:



       class Student
       {
        // member fields
        public int id;
        public string firstName;
        public string lastName;
        public byte age;

        // member method
        public String GetStudentInformation()
        {
            return "First Name: " + firstName + "\n" +
                    "Last Name: " + lastName + "\n" +
                    "Age: " + age;
        }
       }


We are going to learn more about the public keyword and other access modifiers in the tutorials to come. For now just understand that it is used in order to access the members of the class from outside of the class itself. Besides, we are going to learn in depth the ins and outs of methods in the coming tutorials. For now, take heed how the skeleton of a class looks like.

Now, I will show you how to create a class for the above computer class picture and your job is to create a class for each of the remaining pictures.


/*
* Author: Temesghen Tekeste
* Creating a class in C#.
* mail: mail.temesghen.tekeste@gmail.com
* */
using System;

namespace OOPsConcepts
{
    class Computer
    {
        // Attributes of the Computer class
        public int serialNumbere;
        public string brand;
        public int memoryCapacity;
        public int hardDiskCapacity;
        public string operatingSystemInstalled;
        public bool isOn;

        //Methods of the Computer class
        public bool IsOn()
        {
            return isOn;
        }

        public void TurnOn()
        {
            if (!isOn)
            {
                isOn = true;
                Console.WriteLine("Computer is turned on.");
            }

        }
        public void TurnOff()
        {
            isOn = false;
            Console.WriteLine("The computer is turned of.");

        }

        public void Sleep()
        {
            Console.WriteLine("The computer is put to sleep mode.");
        }

        public void Hibernate()
        {
            Console.WriteLine("The computer is put to hibernate mode.");
        }
    }
}

Vital Note:
Member variables of a class follow the naming convention of variables in C#; and by convention, method names follow pascal casing; i.e., the first letter of each word is capitalized as in GetStudentInformation() and ShowResult().

Summary:
  • A class is a blueprint for creating objects.
  • An object is an instance of a class.
  • A class can have member variables and member methods.
  • Member variables are also called fields, attributes or properties of a class; and they represent the data that the object of a class stores.
  • Member methods are the behavior of the class and they represent the actions that an object of a class performs.
  • To create a class in C#, we use the class keyword.
  • The body of a class is surrounded by curly braces, { }.


References: