September 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

C# and VB Object-Oriented Programming in Visual Studio .NET

  • September 18, 2002
  • By Yan Locas, Erik Renaud
  • Send Email »
  • More Articles »

Interfaces

You define interfaces to model common actions. For all of you who have done a little Microsoft COM programming, you've probably heard of interfaces referred to as contracts between two entities. In UML, you draw an interface just like a class but with the interface stereotype (a stereotype allows you to categorize items).

Let's say you have three objects: an Order, an Invoice, and a SalesReport. Each of these classes might have a behavior that allows it to print itself on the local printer, which would make sense. If each class had a behavior called Print, you could call the Print method on instances, and they would print themselves. Unfortunately, calling Print on an Order instance and Print on an Invoice instance are two different actions for the compiler, so reuse of the Print method is not possible. What's more, if the person who implemented the Print method on the Invoice class named it PrintMe rather than Print, the opportunity for reuse is lost.

You can resolve these problems by creating an interface called IPrintable (interface names should be prefixed by a capital "I") with one method called Print. Now any class that can be printed has only to implement the IPrintable interface (realize is the term used in UML), and voilà! Any existing piece of code that knows how to use the IPrintable interface (it's used just as if it were a class), automatically knows that if it calls Print, any underlying class will print itself. This is a powerful way to increase the abstraction level of your system design.

In UML, there are two ways to represent the association between an interface and a class. You can attach a small lollipop to the class, with the interface name written beside it, or you can use a dotted line with a closed arrow pointing to the interface item.

VB programmers are probably already accustomed to using interfaces. Have you ever heard of IDispatch, IUnknown, or ITransaction? VB 6.0 defined an interface as a class. VB.NET introduces the Interface keyword, which gives you the ability to create a true interface, not an imitation. In this example, an interface is both defined and realized in UML:

Here's how the code defines an interface:
VB.NET Code Snippet C# Code Snippet
Public Interface IPerson
    Function GetName() As String
    Function GetPhone() As String

End Interface
public interface IPerson
{
    string GetName();
    string GetPhone();
}

This code snippet realizes an interface:

VB.NET Code Snippet C# Code Snippet
Public Class Customer
   Implements IPerson
   Public ID As Integer
   Public name As String
   Public phone As String

   Public Function GetLastOrder() As
Order

          Return New Order()
   End Function

   Public Function GetName() As String
Implements IPerson.GetName
           Return name
   End Function

   Public Function GetPhone() As String
Implements IPerson.GetPhone
           Return phone
   End Function

End Class
public class Customer : IPerson
{
   public int ID;
   public string name;
   public string phone;

   public Order GetLastOrder()
   {

          return new Order();
   }

   public string GetName()
   {

          return name;
   }
   public string GetPhone()
   {

          return phone;
   }
}

Inheritance

Inheritance describes the hierarchical relationship between classes. You can usually identify these kinds of relationships if the phrase "is a kind of" applies between two classes. For example, a Cat is a kind of Mammal, and a Mammal is a kind of Animal. In the latter example, the Cat is a subclass of Mammal, and Mammal is a superclass of Cat. It's a good practice to limit a class to only one superclass.

In UML, inheritance is noted by a solid line with a closed arrow that points to the superclass. The relationship is called a generalization.

You might ask, "What purpose does inheritance serve?" It allows you to expose, extend, or even alter the attributes and behaviors of the parent class. For example, a Square is a kind of Rectangle in which the height and width must be equal. The Rectangle class already has the height and width attribute, so the Square class specializes the behavior of the Rectangle to make sure both attributes are identical.

The following UML diagram illustrates a class hierarchy with inheritance. The code snippet that follows implements the diagram.

VB.NET Code Snippet C# Code Snippet
Public Class Person
   Public name As String
   Public phone As String

End Class

Public Class Customer
   Inherits Person
   Public ID As Integer
   Public AccountBalance As Decimal

   Public Function GetLastOrder() As
Order

          Return New Order()
   End Function

End Class
public class Person
{
   public string name;
   public string phone;
}

public class Customer : Person
{
   public int ID;
   public Decimal accountBalance;

   public Order getLastOrder()
   {

          return new Order();
   }
}




Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel