Sunday, 3 July 2016

Class Object and methods



Class

Class is a template, declaration that is used for classifying object.
It encapsulates variable members, functions, structure, properties and many more components.
A class consists of a blueprint of objects that share a common structure and behavior. It is the basic building block of object oriented programming.
In order to create class, the class keyword is used.
Example
               Class demo
               {
                       //place code here
               }

Guideline while creating class

1. The class name should be noun and meaningful.

2. Use either Pascal case or camel case. In camel case, the first letter is small. Ex. camelCase.

In Pascal case first letter is capital. Ex. PascalCase. It is strictly recommended you to use Pascal case for class name and camel case for variable name.
3. Your class name must start with character and should not contain any special character except underscore (_) or digit.
4. Don’t use reserved keyword for class name.


Object

An object means a material thing that can be presented physically.
"Object" refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures.
An object is the representation of a real world entity or concept.
Objects communicate with each other by using messages.
Object is an entity that has state behavior and identity is known as an object.
Two objects may have the same behavior and state, but they can never have the same identity.


An object has three characteristics:
  • State: represents data (value) of an object.
  • Behavior: represents the behavior (functionality) of an object such as deposit, withdraw etc.
  • Identity: Every Object unique identity.
e.g.

Let us now look deep into what are objects. If we consider the real-world we can find many objects around us, Cars, Dogs, Birds, Humans, etc. All these objects have a state and behavior and identity.

If we consider a Elaya object of Human class, then

Elaya has State - name,contact, address.

Elaya has Behavior -walk, talk,Eat,Sleep etc.

Elaya has unique Identity-pan card number or Adhar card number.

If you compare the software object with a real world object, they have very similar characteristics.
Software objects also have a state behavior and identity. A software object's state is stored in fields and behavior is shown via methods.

So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.

Method

A method is a group of statements that together perform a task.
Method is the building block of object-oriented programming. It combines related code together and makes program easier. In C# method declaration, you can declare method by following way:


< Access Specifier> < Return Type> < Method Name> (Parameter list)
 {
    Body
 }
· Access Specifier: This determines the visibility of a variable or a method from another class.
·  Return type: A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.
· Method name: Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
· Parameter list: Enclosed between parentheses, the parameters are used to pass and receive data from a method. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.
· Method body: This contains the set of instructions needed to complete the required activity.

Note: -
·      Parameter list is optional.
·      You can define multiple functions within a class.
·      If you are using return data type instead of void, then must return appropriate value with return keyword.


Practical implementation of class object and methods
Example

An object consists of instance members whose value makes it unique in a similar set of objects.
All the objects used in C# code are of object type.

When an object is instantiated, it is allocated with a block of memory and configured as per the blueprint provided by the class underlying the object. Objects of value type are stored in stack, while those of reference type are allocated in the heap.
using System;

namespace Declaring_Method
{
    //defining class
    class Human
    {
        //defining attributes
        string name, city;
        int age;

        // Creating method for accepting details
        public void acceptdata()
        {
            Console.Write("\nEnter your name:\t");
            name = Console.ReadLine();

            Console.Write("\nEnter Your City:\t");
            city = Console.ReadLine();

            Console.Write("\nEnter your age:\t\t");
            age = Convert.ToInt32(Console.ReadLine());
        }

        // Creating method for printing details
        public void printdata()
        {
            Console.Write("\n\n===================");
            Console.Write("\nName:\t" + name);
            Console.Write("\nCity:\t" + city);
            Console.Write("\nAge:\t" + age);
            Console.Write("\n===================\n");
        }

        static void Main(string[] args)
        {
            //creating object for human class
            Human zyx = new Human();
            //calling method using object
            zyx.acceptdata();
            zyx.printdata();
            Console.ReadLine();
        }
    }
}




No comments:

Post a Comment