O'Reilly Forums: Problem With Interface - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem With Interface

#1 User is offline   QHauICT 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-March 14

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?
0

#2 User is offline   ClockEndGooner 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 28-November 13

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.

This post has been edited by ClockEndGooner: 09 March 2014 - 06:35 AM

0

#3 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 756
  • Joined: 08-October 08
  • 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:

Quote

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
0

#4 User is offline   QHauICT 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-March 14

Posted 12 March 2014 - 02:11 AM

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

#5 User is offline   QHauICT 

  • New Member
  • Pip
  • Group: Members
  • Posts: 7
  • Joined: 07-March 14

Posted 12 March 2014 - 02:14 AM

View PostClockEndGooner, on 09 March 2014 - 06:30 AM, said:

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
0

#6 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 756
  • Joined: 08-October 08
  • 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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users