Monday 17 December 2012

2.1.1 OOAD Concepts


What is an Interface?
Interface is the blueprint of the system. An Interface contains only the signatures of the methods, delegates or events. i.e.) an interface does not contain any definition. The implementation of the methods is done in the class that implements the interface.
Example:
//Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.Interface
{
interface IAccount
{
decimal GetAccountBalance();
//decimal GetTextBalance();                    
}
class PrePaid : IAccount
{
public decimal GetAccountBalance()
{
Console.WriteLine("PrePaid:GetAccountBalance is executed");
//write code here
// For testing I hardcoded the value
return 10;
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
IAccount accountObj = new PrePaid();
accountObj.GetAccountBalance();
Console.ReadKey();
}
}
}
//End
Note:
  1. Interfaces cannot contain constructors.
  2. Interfaces cannot contain fields.
  3. The access modifier does not allow in the interface. i.e.) by default access modifier is public.
  4. All the methods in the interface should be implemented the class which implements the interface. For example if you uncomment the //decimal GetTextBalance(); then you get the below error.
HowToBecomeAnArchitect.OOAD.Interface.PrePaid does not implement interface member HowToBecomeAnArchitect.OOAD.Interface.IAccount.GetTextBalance().
What is an Abstract class?
Abstract class is a special kind of class which cannot be instantiated. Abstract class contains one or more concrete methods.
Example:
//Start
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HowToBecomeAnArchitect.OOAD.AbstractClass
{
abstract class Account
{
public abstract decimal GetAccountBalance();
public decimal GetTextBalance()
{
// Write code here
//For testing I hardcoded the value
return 100;
}
}
class PrePaid : Account
{
public override decimal GetAccountBalance()
{
Console.WriteLine("PrePaid:GetAccountBalance is executed");
//write code here
// For testing I hardcoded the value
return 10;
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("Main method is started");
Account accountObj = new PrePaid();
accountObj.GetAccountBalance();
Console.ReadKey();
}
}
}
//End
Note:
  1. If you define the method is as an abstract method in the abstract class then you need to implement that method with override in the class which implements the abstract class.
  2. Non abstract methods should have the implementation in the abstract class.
  3. Abstract class contains the fields.
  4. An abstract method is implicitly a virtual method.
  5. Abstract class contains the access modifiers.
From the above examples, now, we can answers
  1. When to use Interface or Abstract class in our projects?
  2. What is the difference between an Interface and an Abstract class?
Please click the below link to know more about other  Static classes and access modifiers.

1 comment:

  1. Nice post. I learn something more challenging on different blogs every day.I just want to ask you I am doing a project concerning design and that i have to be compelled to apprehend sure things. chiefly categories in faculty that you just have to be compelled to fancy become associate designer, conjointly the earnings of associate designer and what style of design you're employed in.Thanks.
    Web Development Company

    ReplyDelete