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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:53:52+00:00 2026-06-14T04:53:52+00:00

Assume a class named Sprinter : public class Sprinter { protected int travelMeters; public

  • 0

Assume a class named Sprinter:

public class Sprinter {

    protected int travelMeters;

    public void run(int seconds) {
        this.travelMeters = 9 * seconds;
    }

    public int getTravelMeters(){
        return travelMeters;
    }
}

And a SprintGenius type inheriting Sprinter:

class SprintGenius extends Sprinter {

    public void run(int seconds) {
        this.travelMeters = 10 * seconds;
    }
}

Logically, 2 unit-tests class have to be created, one for each type.

Within Sprinter unit-test, I would end up with:

@Before
public void setUp() {
  Sprinter sprinter = new Sprinter();
}

public void testSprinterShouldRun90metersWithin10Seconds() {
  sprinter.run(10);
  assertEquals(sprinter.getTraveledMeters(),90);
}

Within SprintGenius unit-test, I would end up with:

@Before
public void setUp() {
  Sprinter sprinter = new SprintGenius();
}

public void testSprinterShouldRun100metersWithin10Seconds() {
  sprinter.run(10);
  assertEquals(sprinter.getTraveledMeters(),100);
}

In both test above, I would test the amount of traveled meters within 10 seconds.

Obviously, these both tests would be green.

However, what about the violation of Liskov Substitution principle?

Indeed, any client code should expect any sprinter to run exactly 10 meters within 9 seconds.

3 solutions (the first two solutions being signaled RULES to ALL team’s developer and would have to be admit and keeped, even if not everybody well master the concept of Liskov)

1) In Sprinter class, duplicating each test but this time based on a Sprinter sprinter = new SuperGenius() and expect the 90 meters. => what should fail and this is precisely what we’d want! => prevent from violating Liskov principle.

2) In SprintGenius class, always add a similar “clone” of each test based on the base class, based on the exactly same expectations.
So, if you have 2 distinct tests, we’d end up with 4 tests. 2 declaring Sprinter as a Sprinter and 2 declaring Sprinter as a SprintGenius.

3) Never inheriting from concrete class (I imagine that it was your first reaction by reading this post:)), prefer composition if it fits! So that this issue doesn’t happen.

What is the best-practice to prevent from Liskov Substitution principle violation based on the fact that many many developers ignore Liskov principle and are often tempted to inherit from concrete classes instead of using another better way like composition or different inheritance hierarchy?

I don’t want to be trouble by the fact that a developer inherits from my written class (without telling me..), injecting it within a shared huge list of heterogeneous Sprinter list and facing me with ‘hello the weird behavior !’ and hours of debug time…

I don’t want of course to declare ALL my concrete classes ‘final’ 🙂

  • 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-14T04:53:53+00:00Added an answer on June 14, 2026 at 4:53 am

    This is not violation of the Liskov Substitution principle’s compliance. This is the bad design. Both of your classes doesn’t different in behaviour, but in data. So, you should have only one class and clients should expect any sprinter to run given distance proportionally to it’s speed.

    So you should add speed property and have single class with concrete behaviour.

    After that, you can think about creating realy extended classes with new behaviurs and think about testing.

    With this speed parameter, Liskov Substitution principle should not be broken for another kinds of runners, even if they run in different speeds.

    Your question is like: my class extending a person doesn’t pass the test because I’ve changed the name of a person from “Peter” to “Robert”.

    This is bad example for such question. For proper example I think yes, is quit good practice to test it, but it is extremly defensive approach. Propably you can use your time given for creating tests better. Also, that test will be outdated in very short time, it’s hard to add test for ever new subclass just to be sure that old behaviours works properly.

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

Sidebar

Related Questions

Let's assume this class in C#: public class LimitedList<T> : List<T> { private int
Assume this model class : public class Car extends Vehicle implements Visitable { .....
Let's assume I have a file named Main.java with the following code: public class
So let's assume I have a class named ABC that will have a list
Assume that int array arrayName is a member of class className, How can I
assume we have : class Dictionary { int n; int *ints; char **strs; inline
Consider the DUPoint class, whose declaration appears below. Assume this code appears in a
let's assume I've got a UserControl like this: <UserControl x:Class=SomeNamespace.SomeClass> <Grid> <TextBlock Name=SampleTextBlock />
I have a BL class named:A , DTO class named:DTO . Now assume I
Assume I have a class called MyClass that has two properties (int Id and

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.