Tuesday, 18 June 2013

What is Class? and What is Object?


·        What is Class?
In object-oriented programming,
Class is a category of objects or we can say group name. For example, there might be a class called shape that contains objects which are circles, rectangles, and triangles. The class defines all the common properties of the different objects that belong to it.

Simply, A set or category of things having some property or attribute in common and differentiated from others by kind, type, or quality.

One other example, a class named Animal Contains Cat, Dog, Donkey etc.

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.


In .NET languages,
Classes are templates used for defining new types. Classes describe both the properties and behaviors of objects.
Properties contain the data that are exposed by the class.
Behaviors are the functionality of the object, and are defined by the public methods (also called member functions) and events of the class.
Collectively, the public properties and methods of a class are known as the object interface. Classes themselves are not objects, but instead they are used to instantiate (i.e., create) objects in memory.
               
·        What is Object?
An object doesn't exist until an instance of the class has been created; the class is just a definition. When the object is physically created, space for that object is allocated in RAM. It is possible to have multiple objects created from one class.
Let’s take one basic example for full guide of class and object.
The basic building blocks of object-oriented programming are the class and the object. A class defines the available characteristics and behaviour of a set of similar objects and is defined by a programmer in code.
A class is an abstract definition that is made concrete at run-time when objects based upon the class are instantiated and take on the class's behaviour.
As an analogy, let's consider the concept of a 'vehicle' class. The class developed by a programmer would include methods such as Steer(), Accelerate() and Brake(). The class would also include properties such as Colour, NumberOfDoors, TopSpeed and NumberOfWheels. The class is an abstract design that becomes real when objects such as Car, RacingCar, Tank and Tricycle are created, each with its own version of the class's methods and properties.

   





Now we going to view how class created and work in asp.net application.
First open any website. Let in my case it is ClassAndObject
Right click on WebApplication1 from solution explorer and Click on Add->and Class. And provide class name Here I provided ‘Vehicle’.
Here you can see I already created class Vehicle and it is in new Vehicle.cs file.


frmVehicle.axpx code file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="frmVehicle.aspx.cs" Inherits="WebApplication1.frmVehicle" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    Property of
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem>Car</asp:ListItem>
                        <asp:ListItem>RacingCar</asp:ListItem>
                        <asp:ListItem>Tank</asp:ListItem>
                        <asp:ListItem>Tricycle</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>Value of
                    Obj.Colore=</td>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>

            <tr>
                <td>Value of
                    Obj.NumberOfDoors=</td>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>

            <tr>
                <td>Value of
                    Obj.NumberOfWheels=</td>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>

            <tr>
                <td>Value of
                    Obj.TopSpeed=</td>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>


        </table>
    </div>
    </form>
</body>
</html>






 frmVehicle.aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class frmVehicle : System.Web.UI.Page   //This is the frmVehicle class inherited from System.Web.UI.Page class
    {                                                      //By defult created at the time of new page create in Asp.Net

        Vehicle Obj=new Vehicle();  //Here I created one new Object named Obj of Vehicle class
        protected void Page_Load(object sender, EventArgs e)
        {
            Obj.MethodVehicleClass(this);  //Calling method of Vehicle class using Object 'Obj.MethodName()'

            DropDownList1_SelectedIndexChanged(sender, e);
        }

     




    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Vehicle Car = new Vehicle(); //Here I created one new Object named Car of Vehicle class
            Car.Colore = "Red";
            Car.NumberOfDoors = 2;
            Car.NumberOfWheels = 4;
            Car.TopSpeed = "60Mph";

            Vehicle RacingCar = new Vehicle();//Here I created one new Object named RacingCar of Vehicle class
            RacingCar.Colore = "Yellow";
            RacingCar.NumberOfDoors = 0;
            RacingCar.NumberOfWheels = 4;
            RacingCar.TopSpeed = "200Mph";


            Vehicle Tank = new Vehicle(); //Here I created one new Object named Tank of Vehicle class
            Tank.Colore = "Green";
            Tank.NumberOfDoors = 1;
            Tank.NumberOfWheels = 12;
            Tank.TopSpeed = "40Mph";

            Vehicle Tricycle = new Vehicle();//Here I created one new Object named Tricycle of Vehicle class
            Tricycle.Colore = "Orange";
            Tricycle.NumberOfDoors = 0;
            Tricycle.NumberOfWheels = 2;
            Tricycle.TopSpeed = "50Mph";



            switch (DropDownList1.SelectedValue.ToString())
            {
                case "Car":

                    Obj = Car;
                    break;
                case "RacingCar":
                    Obj = RacingCar;
                    break;
                case "Tank":
                    Obj = Tank;
                    break;
                case "Tricycle":
                    Obj = Tricycle;
                    break;



            }



            Label1.Text = Obj.Colore;
            Label2.Text = Obj.NumberOfDoors.ToString();
            Label3.Text = Obj.NumberOfWheels.ToString();
            Label4.Text = Obj.TopSpeed;
        }
    }
}





Vehicle.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;


namespace WebApplication1
{
    public class Vehicle  //Class created by me with name Vehicle which will access by other class frmVehicle class.
    {

        public string Colore;  //public beacuse to access from out side of class
        public int NumberOfDoors;
        public int NumberOfWheels;
        public string TopSpeed;


        public void MethodVehicleClass(System.Web.UI.Page VehPage)
        {
            
            VehPage.Response.Write("Hi I am Vishal Hirpara and this Message is printed by method of Vehicle class from frmVehicle class's PageLoad method");
            VehPage.Response.Write("<br> ------------------------");

        }

    }
   
}
                          

Output like
 



1 comment: