Monday, 3 February 2014

OOPS-Objects, object attributes and methods

Object-oriented programming

In this part of the C# tutorial, we will talk about object oriented programming in C#.
There are three widely used programming paradigms: procedural programming, functional programming and object-oriented programming. C# supports both procedural and object-oriented programming.

Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs.
There are some basic programming concepts in OOP:
  • Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance
 The abstraction is simplifying complex reality by modeling classes appropriate to the problem.
The polymorphism is the process of using an operator or function in different ways for different data input. The encapsulation hides the implementation details of a class from other objects.
The inheritance is a way to form new classes using classes that have already been defined.


Objects

Objects are basic building blocks of a C# OOP program. An object is a combination of data and methods. The data and the methods are called members of an object. In an OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages and process data.
There are two steps in creating an object. First, we define a class. A class is a template for an object. It is a blueprint which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.

using System;

public class Being {}

public class Objects
{
    static void Main()
    {
        Being b = new Being();
        Console.WriteLine(b);
    }
}

In our first example, we create a simple object.
public class Being {}
This is a simple class definition. The body of the template is empty. It does not have any data or methods.
Being b = new Being();
We create a new instance of the Being class. For this we have the new keyword. The b variable is the handle to the created object.
Console.WriteLine(b);
We print the object to the console to get some basic description of the object. What does it mean, to print an object? When we print an object, we in fact call its ToString() method. But we have not defined any method yet. It is because every object created inherits from the base object. It has some elementary functionality which is shared among all objects created. One of this is the ToString() method.
$ ./simpleobject.exe 
Being
We get the object class name.

Object attributes

Object attributes is the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.
 
using System;

public class Person 
{
    public string name;
}

public class ObjectAttributes
{
    static void Main()
    {
        Person p1 = new Person();
        p1.name = "Rama";

        Person p2 = new Person();
        p2.name = "Devi";

        Console.WriteLine(p1.name);
        Console.WriteLine(p2.name);
    }
}

In the above C# code, we have a Person class with one member field.
public class Person 
{
    public string name;
}

We declare a name member field. The public keyword specifies that the member field will be accessible outside the class block.
Person p1 = new Person();
p1.name = "Rama";

We create an instance of the Person class and set the name variable to "Jane". We use the dot operator to access the attributes of objects.
Person p2 = new Person();
p2.name = "Devi";

We create another instance of the Person class. Here we set the variable to "Beky".
Console.WriteLine(p1.name);
Console.WriteLine(p2.name);

We print the contents of the variables to the console.
$ ./person.exe 
Rama 
Devi 

We see the output of the program. Each instance of the Person class has a separate copy of the name member field.

Methods

Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods bring modularity to our programs.
Methods are essential in the encapsulation concept of the OOP paradigm. For example, we might have a Connect() method in our AccessDatabase class. We need not to be informed, how exactly the method Connect() connects to the database. We only have to know, that it is used to connect to a database. This is essential in dividing responsibilities in programming, especially in large applications.
Objects group state and behavior. Methods represent the behavioral part of the objects.
 
using System;

public class Circle 
{
    private int radius; 

    public void SetRadius(int radius)
    {
        this.radius = radius;
    }

    public double Area()
    {
        return this.radius * this.radius * Math.PI;
    }
}

public class Methods
{
    static void Main()
    {
        Circle c = new Circle();
        c.SetRadius(5);

        Console.WriteLine(c.Area());
    }
}

In the code example, we have a Circle class. We define two methods.

private int radius;

We have one member field. It is the radius of the circle. The private keyword is an access specifier. It tells that the variable is restricted to the outside world. If we want to modify this variable from the outside, we must use the publicly available SetRadius() method. This way we protect our data.

public void SetRadius(int radius)
{
    this.radius = radius;
}

This is the SetRadius() method. The this variable is a special variable which we use to access the member fields from methods. The this.radius is an instance variable, while the radius is a local variable, valid only inside the SetRadius() method.
Circle c = new Circle();
c.SetRadius(5);

We create an instance of the Circle class and set its radius by calling the SetRadius() method on the object of the circle. We use the dot operator to call the method.
public double Area()
{
    return this.radius * this.radius * Math.PI;
}

The Area() method returns the area of a circle. The Math.PI is a built-in constant.
$ ./circle.exe 
78.5398163397448

Running the example gives this outcome.

No comments:

Post a Comment