Wednesday, February 8, 2012

Classes and Objects in Object Oriented Programming

All Programming Fundamentals! In my previous post, I wrote about four principles of Object Oriented Programming. Today, we will take it one step further and discuss the concept of a Class and an Object. We will also discuss types of classes.

Class is a template that exhibit certain characteristics. For example, blueprint of a house defines the number of rooms, doors, room size etc. You can use this blueprint to build one or more houses. Similarly, class is a blueprint that can be used to build objects. Each class must have a name, one or more attributes or properties, one or more methods that can be used to perform operations on the object(s) created from this class. For example, A class called "Student" may have the following format.

Class Name
Properties
Methods

An object is an instance of the class. In your application, you can declare an instance of the class and assign it to a variable which will then allow you to access the public properties to read or set the values and use the public methods to perform operations. 

We will talk more about the Public vs. Private vs. Protected Properties/Methods in future post, but for now just remember that any object can access public methods and properties of another object (there are some caveats to it, such as they must be in the same assembly or the consuming object's assembly must reference the assembly where the object resides and they must be imported in the class either by Imports or Using statement on top of the class - more on that later).

Some classes don't need to be instantiated before they can be used. Their public methods and properties are always available without having to instantiate and create an object. I guess you could say that your application automatically creates an instance of the class for all other classes to use.

Remember, while you can have many instances of the class that can be instantiated and one instance will not share the other instance. For example, you can have two instances (objects) of "Student" class called "Student 1" and "Student 2". The properties of "Student 1" will be different than that of "Student 2". Classes that can't be instantiated by definition only have one instance hence only one set of properties.

In C#,  a class declared with STATIC keyword cannot be instantiated. In VB.NET, you don't have a shared class per se, but a Module is closest to the static class. 

In a static class, all methods must be declared static. However a non-static class can have both static and non-static members. Static members will be available regardless of whether the class is instantiated and only one copy of static member will exist, while there can be multiple instances of non-static members. In VB.NET, you can declare methods with keyword SHARED  which will have the same behavior as STATIC in C#.

Constructors
Constructors allows non-static classes to be instantiated. In VB.NET, the constructor is declared as a Public Sub New. In C#, the constructor is same name as the class name. You can have more than one constructor per class. (See example below)


Example:

C#
using System;
namespace 6thGrade
{
public class Student
{
   private int _age;
   private string _name;

   // Default constructor:
   public Student() 
   {
      _name = "N/A";    // assigning a value "N/A" to private string variable name.
   }

   // Another Constructor:
   public Student(string name, int age) 
   {
      this._name = name; // assigning a value passed to the constructor to variable name.
      this._age = age;  // assigning a value passed to the constructor to variable age.
   }

   // Printing method:
   public void PrintStudent() 
   {
      Console.WriteLine("{0}, {1} years old.", name, age);
   }
}
}

VB.NET

Imports System
Namespace 6thGrade
Public Class Student

     Private _age As Int32
     Private _name As String

    Public Sub New()     // default constructor
          name="N/A"
    End Sub
    
    Public Sub New(ByVal name As String, ByVal age As Int32)
          _name=name
          _age=age
    End Sub
    
    Public Sub PrintStudent()
       Console.Writeline(name + ", " + age)
    End Sub
End Class
End Namespace

STATIC CLASS 

C#
using System;
namespace 6thGrade
{
public static class Student
{
   private int _age;
   private string _name;

  //static method
public static void studentinfo(string name, int age)
{
_name=name;
_age = age;
}

   // Printing method:
   public static void PrintStudent() 
   {
      Console.WriteLine("{0}, {1} years old.", name, age);
   }
}
}

VB.NET

Imports System
Namespace 6thGrade
Public Class Student                          // No Static equivalent in VB.NET

     Private _age As Int32
     Private _name As String

    Public Sub New()     // default constructor
          name="N/A"
    End Sub
    
Public Shared Sub studentinfo(ByVal name As String, ByVal age As Int32)
{
_name=name
_age = age
}
Public Shared Sub PrintStudent()
       Console.Writeline(name + ", " + age)
    End Sub
End Class
End Namespace

We will talk more about classes and associations, compositions, property/method accessibility etc in subsequent posts.

Thank you and as always, your comments are welcome!


No comments:

Post a Comment