Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 154167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:55:50+00:00 2026-05-11T09:55:50+00:00

the summary of my question is I have 2 classes which share a common

  • 0

the summary of my question is ‘I have 2 classes which share a common interface, however the two classes have different behaviour with regard to their properties. One interface throws an exception if the parameter values are invalid the other updates its internal state to handle the invalid values. Is it okay for the two classes to share the same interface or should two interfaces be defined to indicate to the developer that the two have different behaviours?’

I’ll try to clarify with some code. I have the interface defind below.

 public interface IStationDictionary {     bool this[string stationId] { get; set; } } 

And a class that implements the interface, this class is used to set the output port on a digital IO board.

 public class DigitalStationAdapter : IStationDictionary {     public bool this[string stationId]     {         get { return ports[stationId].Value; }         set { ports[stationId].Value = value; }     }      public void AddDigitalStation(string stationId, DigitalIoPort port)     {         ports.Add(stationId, port);     }      private IDictionary<string, DigitalIoPort> ports = new Dictionary<string, DigitalIoPort>(); } 

I also have a classes that records which stations have had values changed so that those changes can be propegated to the digital IO.

 public class StationTransitions : IStationDictionary {     public bool this[string stationId]     {         get         {             bool result = false;              if(changes.ContainsKey(stationId))                 result = changes[stationId];              return result;         }         set         {             if(!changes.ContainsKey(stationId))                 changes.Add(stationId, value);             else                 changes[stationId] = value;         }     }      public IDictionary GetChanges()     {         IDictionary<string, bool> result = changes;          changes = new Dictionary<string, bool>          return result;     }      private IDictionary<string, bool> changes = new Dictionary<string, bool>(); } 

So, while both classes implement the same interface DigitalStationAdapter will throw a KeyNotFoundException if you try to access the indexer with a stationId not in the dictionary. Whereas, StationTransitions will succeed, i.e. different behaviour. Is this okay, I thought that interfaces are used to define behaviour as well as structure?

Keith.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:55:51+00:00Added an answer on May 11, 2026 at 9:55 am

    This is fine. Think of it this way: The key to determining whether any inheritance relationship is ‘ok’ is the Liskov substitution principle. This requires that code expecting a base class should be able to use a derived class without knowing it. This implies that preconditions for a method cannot be strengthened in a derived class but can be weakened. Interfaces are just a special case of base classes.

    In your case, the interface’s implicit precondition would be that input be valid as you define it. All code that expects the base class should assume this precondition and avoid passing invalid data or be prepared to deal with the exception. In the concrete class that updates its internal state to deal with the invalid input, you are weakening a precondition. This is acceptable as far as the Liskov substitution principle goes.

    On the other hand, if you were to make the class that automatically deals with the invalid input w/o throwing a base class, and make the class that throws a descendant, this would be bad, because code expecting the base class could not use the derived class as if it were the base class. It would have to know about the derived class to deal with the possible exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Also: Hidden Features of ASP.NET Hidden Features of VB.NET? Hidden… May 11, 2026 at 3:40 pm
  • added an answer Storing data in a database, or in cookies or some… May 11, 2026 at 3:40 pm
  • added an answer Don't make your primary keys 'mean something' other than identifying… May 11, 2026 at 3:40 pm

Related Questions

Okay, my dilemma is this. I have an admin page that I use to
The specification of HTML4.01 ( http://www.w3.org/TR/html401/struct/tables.html#adef-summary ) states that the table summary attribute should
I have a site that I am currently working on in ASP.NET 2.0 using
I'm in the process of teaching myself WPF, and I have run into a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.