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 8005639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:17:11+00:00 2026-06-04T17:17:11+00:00

I am in a situation very similar to what Steve McConnell’s in Code Complete

  • 0

I am in a situation very similar to what Steve McConnell’s in Code Complete has mentioned . Only that my problem is based of Vehicles and Trike happens to be on that by law falls in the category of Cars . Cars had four wheels until now . Any way my domain is unnecessarily complex so it is easy to stick with cats example below.

Be suspicious of classes that override a routine and do nothing inside
the derived routine This typically indicates an error in the design of
the base class. For instance, suppose you have a class Cat and a
routine Scratch() and suppose that you eventually find out that some
cats are declawed and can’t scratch. You might be tempted to create a
class derived from Cat named ScratchlessCat and override the Scratch()
routine to do nothing. This approach presents several problems:

It violates the abstraction (interface contract) presented in the Cat
class by changing the semantics of its interface.

This approach quickly gets out of control when you extend it to other
derived classes. What happens when you find a cat without a tail? Or a
cat that doesn’t catch mice? Or a cat that doesn’t drink milk?
Eventually you’ll end up with derived classes like
ScratchlessTaillessMicelessMilklessCat.

Over time, this approach gives rise to code that’s confusing to
maintain because the interfaces and behavior of the ancestor classes
imply little or nothing about the behavior of their descendants.

The place to fix this problem is not in the base class, but in the
original Cat class. Create a Claws class and contain that within the
Cats class. The root problem was the assumption that all cats scratch,
so fix that problem at the source, rather than just bandaging it at
the destination.

According to the text from his great book above . Following is bad

Parent Class does not have to be abstract

public abstract class Cat {
   public void scratch() {
      System.out.println("I can scratch");
   }
}

Derived Class

public class ScratchlessCat extends Cat {
   @Override
   public void scratch() {
      // do nothing
   }
}

Now he suggests creating another class Claws, but I do not understand how can I use this class to avoid the need for ScratchlessCat#Scratch.

  • 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-06-04T17:17:12+00:00Added an answer on June 4, 2026 at 5:17 pm

    You would still have a scratch() method, but it will not be overridden by the derived classes:

    public class Cat {
      Claw claw_;
      public Cat(Claw claw) {claw = claw_;}
      public final void scratch() {
        if (claw_ != null) {
          claw_.scratch(this);
        }
      }
    }
    

    This allows you to delegate the scratching logic to the contained Claw object, if present (and not scratch if there are no claws). Classes derived from cat have no say in the matter on how to scratch, so no need to create shadow hierarchies based on abilities.

    Also, because the derived classes cannot change the method implementation, there is no problem of them breaking the intended semantics of the scratch() method in the base class’s interface.

    If you take this to the extremes, you might find that you have a lot of classes and not many derivations — most logic is delegated to the composition objects, not entrusted to the derived classes.

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

Sidebar

Related Questions

So I have a situation very similar to this (much simplified) code: import Data.Maybe
My situation is very similar to this question . I have an app that
So situation is following : We have some very old product, that has dozen
I have a very similar situation to the person who asked: Can I serve
I have the next very strange situation and problem: .NET 4.0 application for diagram
Hoping that someone can help clear up this very annoying situation I find myself
I have an situation here that looks very like a SELECT N+1 from the
I know there is this question that is very similar, but I wanted to
Environment: Apache/2.2.11 (Win32) mod_apreq2-20051231/2.6.2-dev mod_perl/2.0.4-dev Perl/v5.10.0 Situation very similar to what's described in this
I have a situation where I have 4-5 very similar classes which I'd like

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.