Tuesday, September 25, 2012

Implement Interfaces & Use Abstract Classes





Interfaces & Abstract Classes

  • Write Interface definitions and class definitions that implement them.
  • Write abstract class definitions and derived classes that inherit from them.
Interfaces:
  • An interface just declares methods, which must be provided by any class that implements the interface.
  • A virtual method must have a body. An method declared in an interface does not.
Generally your best tool for separation of interface and implementation.
  • The interface is public.
  • Implementation is private.
  • Users see only the interface.
How to Define an Interface:

interface IShape
{
void Draw();
double Area();
}

Restrictions:
An interface never includes implementation.
Consequently:
You cannot instantiate an instance.

  • No fields
  • No constructors (or destructors)
  • No access modifiers (public, private). methods automatically public
  • No type definitions inside interface definition.  enums, structs, classes, etc.
  • Cannot inherit from a class or a struct.
Classes and structs must have implementations.
Interface IShape:
interface IShape
{
void Draw();
double Area();
}

Implementing an Interface:

Inherit an interface just like a base class
class Square : IShape
{
private double Size; // Length of each side
public Square (double size)
{
Size = size;
}

public double Area()
{
return Size * Size;
}
public void Draw()
{
Console.WriteLine ("Square Draw method called");
}
}
Example: Using a Class that Implements an Interface
class Program
{
static void Main(string[] args)
{
Square sq = new Square(10);
double area = sq.Area();
Console.WriteLine ("Area = " + area);
sq.Draw();
Console.ReadLine();
}
}

Output:
But an interface cannot be instantiated
class Class1
{
static void Main(string[] args)
{
IShape sq;
sq = new IShape();
}
}
This gets a compile error.

This compiles and runs:
class Program
{
static void Main(string[] args)
{
IShape sq = new Square(10);
double area = sq.Area();
Console.WriteLine("Area = " + area);
sq.Draw();
Console.ReadLine();
}
}
Program in Action

Abstract Classes:

Often you will have several different classes that implement a given interface:

e.g. Circle, Square, Triangle, ...

The classes may have some methods that are identical

Examples: String Name();
int ID();

Replicating functionality in multiple classes is a bad idea.
Solution is to define an abstract class.

Provides implementation for methods that are common to derived classes.
Like an interface, cannot be instantiated. Exists only to be inherited.

Derived classes inherit from the abstract class.

Implement methods that are different for each kind of derived class.

An Abstract Class:

abstract class Default_Shape
{
protected String name;
static int next_id = 1;
protected int id;
protected Default_Shape(String name_)
{
name = name_;
id = next_id++;
}

public int ID()
{
return id;
}
public String Name()
{
return name; }

Some Derived Classes:

class Square : Default_Shape, IShape
{
private double Size; // Length of each side
public Square(double size, String name_) : base(name_)
{
Size = size;
}
public double Area()
{
return Size * Size;
}

Square
public void Draw()
{
Console.WriteLine("Square Draw() called." +
" ID = " + id +
" Size = " + Size);
}
Class Circle
class Circle : Default_Shape, IShape // Same bases
{
private double radius; // Unique field
public Circle (double radius_,
String name_) : base (name_)
{
radius = radius_;
}

public double Area()
{
return Math.PI*radius*radius;
}

public void Draw()
{
Console.WriteLine ("Circle Draw() called." +
" ID = " + id.ToString() +
" radius = " + radius.ToString() );
}
}

Using the Derived Classes:

Square sq1; // Declarations

Circle c1;
IShape ishape1;
IShape ishape2;
sq1 = new Square(1, "sq1"); // Instantiation
c1 = new Circle (1, "c1");
ishape1 = new Square(10, "ishape1");
ishape2 = new Circle(10, "ishape2");
sq1.Draw(); // Method calls
c1.Draw();
ishape1.Draw();
ishape2.Draw();

Output:
Working with Multiple Interfaces.In C#, a class can have at most one base class
BUT can implement any number of interfaces

Syntax:
class Square : Default_Shape, IShape, IDisposable, IComparable
{
...
}

A class must implement all methods in all interfaces that it inherits.

Summary:

Inheritance is one of the key concepts of object oriented programming.
  • A class can inherit
  • A base class
  • Any number of interfaces Functionality common to multiple classes can be factored out to an abstract base class that all inherit.
  • Avoid replication.

No comments:

Post a Comment