Question: Every class, directly or indirectly, inherits from the `Object` class. 

The `Object` class, among the others, has the important method, most often overridden: `toString`.

The question is: doesn't the overriding of this method bring to Liskov Substitution Principle violation with respect to the `Object` class?

I'll do an example.

```
public class Main
{
    public static void main(String[] args)
    {
        Object o = new Object();
        String s = o.toString();
        if (s.indexOf('@') > -1) {
            System.out.println("OK");
        } else {
            System.out.println(":-(");
        }
    }
}

public class MyClass
{
    private int x;

    public string toString()
    {
        return Integer.toString(x);
    }
}

```

Obviously if I replace `new Object()` with `new MyClass()` the behavior of the system changes.

Answer: 
Liskov substitution principle requires only interface compatibility. It does not say anything about underlying behavior. So for example

```
public interface Text {
    String value();
}

public class SimpleText implements Text {

    private final String value;

    public SimpleText(String value) {
        this.value = value;
    }

    @Override
    public String value() {
        return this.value;
    }
}

public class NumberText implements Text {

    private final Number number;

    public NumberText(Number number) {
        this.number = number;
    }

    @Override
    public String value() {
        return String.format("%.3f", this.number.doubleValue());
    }
}

```

You do not care about details of implementation, so you can exchange them this way:

```
//We care only about value() method, not its specific implementation
Text text = new SimpleText("text");
text.value();
text = new NumberText(44);
text.value();

```
Note that if your implementation will throw any exception then it will violate LSP as Object.toString() doesn't throw any exceptions.