Skip to main content
deleted 7 characters in body
Source Link
lukasniessen
  • 2.3k
  • 2
  • 22
  • 26

The accepted answer is not a counterexample.

Liskov Principle: Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.

Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.

Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

The accepted answer is not a counterexample.

Liskov Principle: Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.

A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

deleted 7 characters in body
Source Link
lukasniessen
  • 2.3k
  • 2
  • 22
  • 26

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.


 

Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.


 

Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.

Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.

Source Link
lukasniessen
  • 2.3k
  • 2
  • 22
  • 26

Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.


Actually, the accepted answer is not a counterexample for the Liskov principle. A square naturally is a specific rectangle, so it makes perfect sense that inherits from the class rectangle. You simply need to implement it in this way:

@Override public void setHeight(double height) { this.height = height; this.width = height; // since it's a square } @Override public void setWidth(double width) { setHeight(width); } 

So, having provided a good example, this, however, is a counterexample:

class Family: -- getChildrenCount() class FamilyWithKids extends Family: -- getChildrenCount() { return childrenCount; } // always > 0 class DeadFamilyWithKids extends FamilyWithKids: -- getChildrenCount() { return 0; } -- getChildrenCountWhenAlive() { return childrenCountWhenAlive; } 

In this implementation, DeadFamilyWithKids cannot inherit from FamilyWithKids since getChildrenCount() returns 0, while from FamilyWithKids it should always return something greater 0.