Tuesday, 14 February 2012

Classes and Objects

Class:

Class is the name of type that we use to represent the relation between the data and functions or methods. In the other way, Class is a block of code which combines methods and properties for a group of objects.

Class describes the properties of objects which are present in the class. In the class, objects are categorized according to their state and behavior. By default, classes are private.

Syntax:

Class class name

{

Statements;

}

Example:

Class employee

{

String name;

int id;

Public employee (string name, int id) // constructor

{

This.name=name;

This.id=id;

}

Public void print () //method

{

Console.writeLine (“employee name is”, name);

Console.writeLine (“employee id is”, id);

}

}

Class program

{

Public static void main ()

{

employee s1=new employee ();

s1.print ();

employee s2=new employee ();

s2.print ();

}

}

Object:

Object is an instance or an entity that will represent the components of a class. Objects interact with each other through the functionality of methods in the class.

The ‘new’ operator is used to create object of a class. When we create an object, then system creates memory for data member and methods that are present in the class.

For example, we take ‘animal’ as a class then dog, cat, tiger and other animals are the objects.

No comments:

Post a Comment