Showing posts with label Classes. Show all posts
Showing posts with label Classes. Show all posts

Monday, February 13, 2012

Difference between an Interface and an Abstract class

All Programming Fundamentals! It is rather easy to articulate the difference between the two, but I have seen people stumble when asked to explain it. So today, we will try to cover this topic and provide an example to clarify some of the key differences.

 Abstract Class
An Abstract Class is just like any other class with a few key differences. As we discussed in my previous post, non-static classes can be instantiated except with one type of class - an Abstract Class. In other words, an abstract class is one that cannot be instantiated and can only be used as a parent class in other derived (concrete) classes. Since this class cannot be instantiated and you cannot use a non-static class without instantiating it, you cannot use the abstract class by itself. You must inherit from it in other class(es) to use it.

An abstract class may have one or more methods that are completely implemented but it must have at least one abstract method that must be implemented in the derived class(es). This is what makes the abstract class - abstract.

So, in a nutshell an abstract class is just like any other class except that it cannot be instantiated and must have one or more abstract (unimplemented) methods.

Interface
An Interface is not a class. It is a contract and all classes that implement the interface must implement all the properties/methods declared in an Interface.

Unlike an abstract class, an interface cannot have actual implementation of any property/method and also all the properties / methods must be declared public.

Since an interface cannot have any implementation, all the classes inheriting this interface must implement all the methods defined in the interface. Abstract class on the other hand can have fully implemented methods and derived classes can simply use that implementation.

If an abstract class has all the methods defined as abstracts, then both Interface and Abstract class is the same.

Why would you use one over the other?

Both interfaces and abstract classes are good if you want to keep the same structure in your classes inheriting from it. But abstract class allows you to implement one or more common method that can be used as is by all the classes. One advantage that interface have over abstract class is that at least in C# and VB.NET (also true in Java but not in C++) a class can only inherit from one class, while it can implement multiple interfaces.

Interfaces are generally used to define the abilities of a class i.e. what a class can do. For example - IComparable interface. This interface defines "Compare To" signature which can be implemented in the class to compare the instance of an object with another object of the same type. Any class that implements IComparable interface must be capable of doing so.

Abstract classes on the other hand generally implement or define the core capability of the class. For example a MemoryStream class can inherit from a Stream abstract class that implements a Serialize method to serialize the content.

You generally want to use interface if various classes only share certain features, although they may be of different type. For example, a class called Car and a class called Plane may implement the same IMovable interface. You want to use an abstract class when both classes are of same or similar kind. For example a class "BMW" and a class "Lexus" can inherit from the same abstract class called "Car".

If you modify an interface and add a new method, you must modify all the classes implementing that interface to implement that method. You can however modify an abstract class and as long as you fully implement a new method, you don't have to touch other classes inheriting from it.

Let's see both Interface and Abstract class in action.

Abstract Class

Using System;
namespace AbstractInterface
{
    public abstract class Student  
           // notice keyword abstract. In VB.NET the equivalent keyword is MustInherit
     { 
        protected string studentnum;     //protected variables
        protected string studentname;


      public abstract String StudentNumber 
              // notice the keyword abstract. The property is not fully implemented.
           {
             get;
             set;
           }
      public abstract String StudentName
        {
           get;
           set;
        }
     public String GetStudentInfo() // notice this method is fully implemented
      {
        return "Student Number: " + studentnum + " Name: " + studentname;
      }
     public abstract String EnrollmentStatus(); // notice this method is not implemented.


    }
}
   
Concrete Class


using system;
namespace AbstractInterface
{
    public class GraduateStudent : Student // this class inherits from Student class.
   {
     public GraduateStudent()   //constructor
      {
       }
      public override String StudentNumber
           // remember abstract class didn't implement this property so we have to implement it here.
           //   Notice keyword override
        {
           get
            {
               return studentnum;
            }
            set
            {
               studentnum = value;
            }
        }
        public override String StudentName
         
        {
           get
            {
               return studentname;
            }
            set
            {
               studentname = value;
            }
        }


      public new String GetStudentInfo() // this method is implemented in base class.
      {
        return base.GetStudentInfo();
      }


    }
    public override String  EnrollmentStatus () // notice this method is not implemented in base class


      {
        return studentname + " is enrolled."
      }


    }
}

Now lets see an Interface Example

Interface


Public interface IStudent // As a convention, interfaces are prefixed with "I"
{


 String StudentNumber
              // No need to specific "public" since interface can only have public properties/methods.
           {
             get;
             set;
           }
      String StudentName
        {
           get;
           set;
        }
     String GetStudentInfo();
     
     String EnrollmentStatus();
   
          // notice no implementation of any method or property
   }

Concrete Class using Interface


using system;
namespace AbstractInterface
{
     public class Student2 : IStudent    // implements interface


      protected string studentnum;     //protected variables
      protected string studentname;


     public  Student2  ()   //constructor
        {
        }
      public String StudentNumber
           // must be implemented here, otherwise compiler will throw error.
         
        {
           get
            {
               return studentnum;
            }
            set
            {
               studentnum = value;
            }
        }
        public String StudentName
       
        {
           get
            {
               return studentname;
            }
            set
            {
               studentname = value;
            }
        }


      public String GetStudentInfo() // implemented here
      {
        return "Student Number: " + studentnum + " Name: " + studentname;
      }


    }
    public String  EnrollmentStatus ()


      {
        return studentname + " is enrolled."
      }


   // notice - all properties and methods declared in interface must be implemented. Every class inheriting       //from this interface must implement all of them.


    }


}

Hopefully this will help clarify some of the differences between an Interface and an Abstract class. As always, your comments are welcome.

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!