Click here to Skip to main content
15,909,498 members
Home / Discussions / C#
   

C#

 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 2:59
Krishna Varadharajan8-Mar-13 2:59 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Keith Barrow8-Mar-13 3:22
professionalKeith Barrow8-Mar-13 3:22 
Sorry to steal Pete's thunder.


You can't use an interface for the implementation of common functionality, all an interface does is define what is available not how it is actually achieved. Lets take a simple example, modelling a cats which can for simplicity only look around or make a noise the Interface would look like:

C#
public interface IFeline
{
   LookAround();
   MakeNoise();
}


OK, lets say I want to model a Tiger and a Cat. Both Implement IFeline, both look around in the same way, but a cat purrs but a Tiger roars. LookAround is common to both and should be in a base class. I don't ever want to create and instance of the base class, so I create an abstract one which encapsulate what is common:

C#
public abstract class Feline: IFeline
{
    public LookAround()
    {
        Console.WriteLine("Looking");
    }
    

    public abstract MakeNoise(); //This is needed to stop the compiler complaing
}


Now I can implement the differing functionality:

ch
public class Cat: Feline
{
    public MakeNoise()
    {
        Console.WriteLine("Purrr");
    }
}

public class Tiger: Feline
{
    public MakeNoise()
    {
        Console.WriteLine("Roar");
    }
}


Hope this helps!

GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 3:41
Krishna Varadharajan8-Mar-13 3:41 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Keith Barrow8-Mar-13 4:30
professionalKeith Barrow8-Mar-13 4:30 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 4:40
Krishna Varadharajan8-Mar-13 4:40 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Keith Barrow8-Mar-13 4:49
professionalKeith Barrow8-Mar-13 4:49 
AnswerRe: When to use & when not to use ( Abstract Class & Interface ) Pin
GuyThiebaut8-Mar-13 2:40
professionalGuyThiebaut8-Mar-13 2:40 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 3:04
Krishna Varadharajan8-Mar-13 3:04 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
GuyThiebaut8-Mar-13 3:28
professionalGuyThiebaut8-Mar-13 3:28 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 3:57
Krishna Varadharajan8-Mar-13 3:57 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
GuyThiebaut8-Mar-13 4:16
professionalGuyThiebaut8-Mar-13 4:16 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 4:57
Krishna Varadharajan8-Mar-13 4:57 
AnswerRe: When to use & when not to use ( Abstract Class & Interface ) Pin
PIEBALDconsult8-Mar-13 3:42
mvePIEBALDconsult8-Mar-13 3:42 
AnswerRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Shameel8-Mar-13 4:08
professionalShameel8-Mar-13 4:08 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 4:33
Krishna Varadharajan8-Mar-13 4:33 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Shameel8-Mar-13 5:01
professionalShameel8-Mar-13 5:01 
GeneralRe: When to use & when not to use ( Abstract Class & Interface ) Pin
Krishna Varadharajan8-Mar-13 5:53
Krishna Varadharajan8-Mar-13 5:53 
AnswerRe: When to use & when not to use ( Abstract Class & Interface ) Pin
jschell8-Mar-13 12:41
jschell8-Mar-13 12:41 
QuestionWhy SqlCommandBuilder Pin
Krishna Varadharajan8-Mar-13 1:50
Krishna Varadharajan8-Mar-13 1:50 
AnswerRe: Why SqlCommandBuilder Pin
Eddy Vluggen8-Mar-13 3:01
professionalEddy Vluggen8-Mar-13 3:01 
GeneralRe: Why SqlCommandBuilder Pin
Krishna Varadharajan8-Mar-13 3:22
Krishna Varadharajan8-Mar-13 3:22 
GeneralRe: Why SqlCommandBuilder Pin
Eddy Vluggen8-Mar-13 4:59
professionalEddy Vluggen8-Mar-13 4:59 
GeneralRe: Why SqlCommandBuilder Pin
Krishna Varadharajan8-Mar-13 5:55
Krishna Varadharajan8-Mar-13 5:55 
GeneralRe: Why SqlCommandBuilder Pin
Eddy Vluggen8-Mar-13 7:12
professionalEddy Vluggen8-Mar-13 7:12 
AnswerRe: Why SqlCommandBuilder Pin
PIEBALDconsult8-Mar-13 3:38
mvePIEBALDconsult8-Mar-13 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.