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

  • SEARCH
  • Home
  • 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 7439541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:43:15+00:00 2026-05-29T10:43:15+00:00

Suppose the following class hierarchy with a base class and some general case class

  • 0

Suppose the following class hierarchy with a base class and some general case class that inherits from the base:

class BaseClass:
    def f(self, a, b):
        #do something
        pass

class GeneralCase(BaseClass):
    def f(self, a, b):
        BaseClass.f(self, a, b)
        #do something else

Now, suppose that we have a special case, in which (among others) one of the arguments of function f is predetermined and constant. One
way to implement this is to remove this argument from the argument list as follows:

class SpecialCase1(GeneralCase):
    def f(self, a):

Pro’s: clean, explicit

Con’s: different method signature might cause problems when objects of different classes are used

Option 2: set default value and assert it is not changed

class SpecialCase2(GeneralCase):
    def f(self, a, b=PREDEFINED_VALUE):
        assert b == PREDEFINED_VALUE
        GeneralCase.f(self, a, PREDEFINED_VALUE)

Pro’s: same signature

Con’s: confusing interface: “Why do we have parameter b? what happens if I change it?”

What approach would you preffer and why?

  • 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. Editorial Team
    Editorial Team
    2026-05-29T10:43:16+00:00Added an answer on May 29, 2026 at 10:43 am

    It shouldn’t be a subtype at all (cf. Liskov Substitution Principle).

    There’s a property of GeneralCase, namely:

    There’s a method f with two parameters … which does yadda yadda yadda

    This property should hold for all subtypes of GeneralCase (or going duck-typed, anything that should quack like a GeneralCase regardless of class relationships). If it doesn’t hold, all code handling such objects would have to be aware of it and code around it. That’s an unacceptable burden, and largely unnecessary.

    If that property isn’t true for some special case, that special case shouldn’t be a subtype. It may be highly related, but either you need a more general GeneralCase or it’s not really a special case of GeneralCase.

    If you want code reuse, use mixins. If there’s code that can operate on both GeneralCase and SpecialCase (e.g. because it doesn’t use that method), you can define a more general interface that doesn’t include that method (probably implicitly, we don’t need no interface keyword) and thus steers clear of the aforementioned trouble.

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

Sidebar

Related Questions

Suppose we have the following class hierarchy: class ClassA: @property def foo(self): return hello
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Suppose the following class public class Message { // some code } And a
In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public
Suppose I have the following class hierarchy: Class A {...} Class B : A
I have the following class hierarchy: NSViewController MyGeneralViewController ViewControllerA ViewControllerB MyGeneralViewController holds some common
Suppose I need to add unit tests for the following class from legacy code
Suppose I have the following class hierarchy: class A { int foo; virtual ~A()
Suppose I have the following hierarchy: class A { public: A() private: int aa;
Suppose you have the following object hierarchy: class Vehicle { public: virtual ~Vehicle() {}

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.