Jump to content


Photo

Problem With Interface


  • Please log in to reply
5 replies to this topic

#1 QHauICT

QHauICT

    New Member

  • Members
  • Pip
  • 7 posts

Posted 07 March 2014 - 09:05 PM

Im having HFC# 3rd edition, at page 305 we have this line : " Any class that implements an interface that inherits from IWorker must implement its methods and props. But at page 314, IScaryClown inherits from IClown, but the class ScaryScary, which implements IScaryClown, has no props or methods of IClown. Why is that?

#2 ClockEndGooner

ClockEndGooner

    Active Member

  • Members
  • PipPip
  • 11 posts
  • Gender:Male
  • Location:Lehigh Valley, Pennsylvania
  • Interests:C, C++, C#,JavaScript/Node.js, desktop, mobile and web application development, OOD & OOP, continuous integration and unit testing.

Posted 09 March 2014 - 06:30 AM

Greetings;

The ScaryScary class does implement the IClown as ScaryScary is a subclass or derived from the FunnyFunny class; it is the FunnyFunny class, which the ScaryScary class implements the FunnyThingIHave property accessor and Honk() method on ScaryScary class's behalf.

The class declaration of ScaryScary first declares that ScaryScary is derived from the FunnyFunny class, and also implements the IScaryClown interface.

//
// From the text: "Since ScaryScary is a subclass of FunnyFunny and FunnyFunny
// implements IClown, ScaryScary implements IClown too.
//
class ScaryScary : FunnyFunny, IScaryClown
{
    //
    // ...
    //
}

Perhaps looking at this from another perspective might help. If the ScarryScarry class was not derived from or inherited the FunnyFunny class, the public string FunnyThingIHave property accessor and the Honk() method, which are required to provide a complete implementation of the IClown interface, would have to be implemented or written in as part of the ScaryScary class. A ScaryScary class that was not a subclass of FunnyFunny would look like this;

interface IClown 
{
    string FunnyThingIHave { get; }
    void Honk();
}

interface IScaryClown : IClown 
{
    string ScaryThingIHave { get; }
    void ScareLittleChildren();
}

//
// This version of the ScaryScary class only implements both the IClown and IScaryClown
// interfaces.  Since this version of the ScaryScary class is not a subclass of the FunnyFunny
// class, it has to provide its own implementations of the FunnyThingIHave property and the
// Honk() method.
//
class ScaryScary : IClown, IScaryClown 
{   
    #region ScaryScary Class Data Members

    private string funnyThingIHave;
    private int numberOfScaryThings;

    #endregion // ScaryScary Class Data Members

    public ScaryScary(string funnyThingIHave, int numberOfScaryThings)
    {
        this.funnyThingIHave = funnyThingIHave;
        this.numberOfScaryThings = numberOfScaryThings;
    }

    #region IClown Interface Implementation

    public string FunnyThingIHave
    {
        get
        {
            return funnyThingIHave;
        }
    } 
	
    public void Honk()
    {
        Console.WriteLine(this.FunnyThingIHave);
    }

    #endregion // IClown Interface Implementation

    #region IScaryClown Interface Implementation

    public string ScaryThingIHave 
    {
        get 
        {
            return "I have " + this.numberOfScaryThings + " spiders"; 
        }
    }

    public void ScareLittleChildren() 
    {
        Console.WriteLine("Boo! Gotcha!");
    }

    #endregion // IScaryClown Interface Implementation
}

void Main()
{
    ScaryScary fingersTheClown = new ScaryScary("big shoes", 14);
	
    IClown someFunnyClown = fingersTheClown as IClown;
    someFunnyClown.Honk();
	
    IScaryClown someOtherScaryClown = someFunnyClown as ScaryScary;
    someOtherScaryClown.Honk();
    Console.WriteLine(someOtherScaryClown.ScaryThingIHave);
    someOtherScaryClown.ScareLittleChildren();
	
    Console.ReadKey();	
}

The Output of running the program would resemble the following;

big shoes
big shoes
I have 14 spiders
Boo! Gotcha!
This is a good example of inheritance, which is one of the key concepts in object-oriented programming as it shows how inheritance covers inheritance of data values through properties, but inheritance of behavior through methods or member functions as well.

I hope this was of help and interest.

Edited by ClockEndGooner, 09 March 2014 - 06:35 AM.

ClockEndGooner


#3 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 821 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 09 March 2014 - 07:42 AM

Because ScaryScary is a subclass of FunnyFunny, which implements IClown. This is explained in the solution to the exercise on the next page:

Since ScaryScary is a subclass of FunnyFunny and FunnyFunny implements IClown, ScaryScary implements IClown too.


If a class implements an interface, that means it has all of the members of that interface. Any of its subclasses automatically implement that interface too, and they inherit all of those members.

Andrew
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com

#4 QHauICT

QHauICT

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 March 2014 - 02:11 AM

Thanks for your hints, i think i figured that out :)

#5 QHauICT

QHauICT

    New Member

  • Members
  • Pip
  • 7 posts

Posted 12 March 2014 - 02:14 AM

Greetings;

The ScaryScary class does implement the IClown as ScaryScary is a subclass or derived from the FunnyFunny class; it is the FunnyFunny class, which the ScaryScary class implements the FunnyThingIHave property accessor and Honk() method on ScaryScary class's behalf.

The class declaration of ScaryScary first declares that ScaryScary is derived from the FunnyFunny class, and also implements the IScaryClown interface.

//
// From the text: "Since ScaryScary is a subclass of FunnyFunny and FunnyFunny
// implements IClown, ScaryScary implements IClown too.
//
class ScaryScary : FunnyFunny, IScaryClown
{
    //
    // ...
    //
}

Perhaps looking at this from another perspective might help. If the ScarryScarry class was not derived from or inherited the FunnyFunny class, the public string FunnyThingIHave property accessor and the Honk() method, which are required to provide a complete implementation of the IClown interface, would have to be implemented or written in as part of the ScaryScary class. A ScaryScary class that was not a subclass of FunnyFunny would look like this;

interface IClown 
{
    string FunnyThingIHave { get; }
    void Honk();
}

interface IScaryClown : IClown 
{
    string ScaryThingIHave { get; }
    void ScareLittleChildren();
}

//
// This version of the ScaryScary class only implements both the IClown and IScaryClown
// interfaces.  Since this version of the ScaryScary class is not a subclass of the FunnyFunny
// class, it has to provide its own implementations of the FunnyThingIHave property and the
// Honk() method.
//
class ScaryScary : IClown, IScaryClown 
{   
    #region ScaryScary Class Data Members

    private string funnyThingIHave;
    private int numberOfScaryThings;

    #endregion // ScaryScary Class Data Members

    public ScaryScary(string funnyThingIHave, int numberOfScaryThings)
    {
        this.funnyThingIHave = funnyThingIHave;
        this.numberOfScaryThings = numberOfScaryThings;
    }

    #region IClown Interface Implementation

    public string FunnyThingIHave
    {
        get
        {
            return funnyThingIHave;
        }
    } 
	
    public void Honk()
    {
        Console.WriteLine(this.FunnyThingIHave);
    }

    #endregion // IClown Interface Implementation

    #region IScaryClown Interface Implementation

    public string ScaryThingIHave 
    {
        get 
        {
            return "I have " + this.numberOfScaryThings + " spiders"; 
        }
    }

    public void ScareLittleChildren() 
    {
        Console.WriteLine("Boo! Gotcha!");
    }

    #endregion // IScaryClown Interface Implementation
}

void Main()
{
    ScaryScary fingersTheClown = new ScaryScary("big shoes", 14);
	
    IClown someFunnyClown = fingersTheClown as IClown;
    someFunnyClown.Honk();
	
    IScaryClown someOtherScaryClown = someFunnyClown as ScaryScary;
    someOtherScaryClown.Honk();
    Console.WriteLine(someOtherScaryClown.ScaryThingIHave);
    someOtherScaryClown.ScareLittleChildren();
	
    Console.ReadKey();	
}

The Output of running the program would resemble the following;

big shoes
big shoes
I have 14 spiders
Boo! Gotcha!
This is a good example of inheritance, which is one of the key concepts in object-oriented programming as it shows how inheritance covers inheritance of data values through properties, but inheritance of behavior through methods or member functions as well.

I hope this was of help and interest.

thanks, that helps, it took a while for me to work that out

#6 AndrewStellman

AndrewStellman

    Andrew Stellman

  • O'Reilly Author
  • PipPipPipPipPipPipPipPipPipPipPip
  • 821 posts
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 12 March 2014 - 11:46 AM

ClockEndGooner -- very cool, thanks for the thorough (and correct!) explanation!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users