Linked Questions
196 questions linked to/from What is an example of the Liskov Substitution Principle?
-2 votes
2 answers
343 views
Understanding Liskov Substituion Principle [duplicate]
My sample program like below; public class Animal { public virtual string MakeSound() { return "General Sound"; } } public class Dog : Animal {...
3 votes
0 answers
176 views
polymorphism: why would you assign a interface reference to a subclass of superclass that implements the interface [duplicate]
i'm trying to understand the basics of polymorphism. I do understand why it could be useful to assign a superclass to a sub class or a sub to a super. what i don't understand quite well is the logic ...
0 votes
1 answer
96 views
What does Liskov Substitution Principle preserve? [duplicate]
As I've read the substitution of objects of a concrete type by instances of a subclass of that concrete type must preserve a program's correctness, a program's invariants. I'd like to know what ...
715 votes
14 answers
329k views
What is Turing Complete?
What does the expression "Turing Complete" mean? Can you give a simple explanation, without going into too many theoretical details?
182 votes
21 answers
164k views
Assignment in an if statement
I have a class Animal, and its subclass Dog. I often find myself coding the following lines: if (animal is Dog) { Dog dog = animal as Dog; dog.Name; ... } For the variable Animal ...
215 votes
13 answers
57k views
What is an invariant?
The word seems to get used in a number of contexts. The best I can figure is that they mean a variable that can't change. Isn't that what constants/finals (darn you Java!) are for?
204 votes
10 answers
306k views
What is the difference between syntax and semantics in programming languages?
What is the difference between syntax and semantics in programming languages (like C, C++)?
111 votes
12 answers
21k views
Design patterns to avoid [closed]
A lot of people seem to agree, that the Singleton pattern has a number of drawbacks and some even suggest avoiding the pattern entirely. There's an excellent discussion here. Please direct any ...
80 votes
12 answers
31k views
How to design extensible software (plugin architecture)? [closed]
I need some resources that talk about how to design your software to be extensible, i.e. so that other people can write add-ons/plug-ins that adds functionality to it. What do you recommend? Any ...
72 votes
6 answers
12k views
Liskov substitution principle - no overriding/virtual methods?
My understanding of the Liskov substitution principle is that some property of the base class that is true or some implemented behaviour of the base class, should be true for the derived class as well....
51 votes
7 answers
24k views
What are the differences between functions and methods in Swift?
I always thought functions and methods were the same, until I was learning Swift through the "Swift Programming Language" eBook. I found out that I cannot use greet("John", "Tuesday") to call a ...
56 votes
5 answers
90k views
Mocking a class vs. mocking its interface
For a unit test, I need to mock several dependencies. One of the dependencies is a class which implements an interface: public class DataAccessImpl implements DataAccess { ... } I need to set up ...
43 votes
5 answers
24k views
Can anyone provide an example of the Liskov Substitution Principle (LSP) using Vehicles?
The Liskov Substitution Principle states that a subtype should be substitutable for that type (without altering the correctness of the program). Can someone please provide an example of this principle ...
33 votes
10 answers
21k views
Inheritance or composition: Rely on "is-a" and "has-a"?
When I design classes and have to choose between inheritance and composition, I usually use the rule of thumb: if the relationship is "is-a" - use inheritance, and if the relationship is "has-a" - use ...
30 votes
8 answers
2k views
What is wrong with testing an object to see if it implements an interface?
In the comments of this answer it is stated that "checking whether the object has implemented the interface , rampant as it may be, is a bad thing" Below is what I believe is an example of this ...
43 votes
4 answers
20k views
What is the difference between covariance and contra-variance in programming languages?
Can anyone explain the concept of covariance and contravariance in programming language theory?
36 votes
8 answers
5k views
Why avoid subtyping?
I have seen many people in the Scala community advise on avoiding subtyping "like a plague". What are the various reasons against the use of subtyping? What are the alternatives?
60 votes
2 answers
9k views
The type system in Scala is Turing complete. Proof? Example? Benefits?
There are claims that Scala's type system is Turing complete. My questions are: Is there a formal proof for this? How would a simple computation look like in the Scala type system? Is this of any ...
34 votes
3 answers
15k views
The difference between liskov substitution principle and interface segregation principle
Is there any core difference between Liskov Substitution Principle (LSP) and Interface Segregation Principle (ISP)? Ultimately, both are vouching for designing the interface with common ...
11 votes
11 answers
22k views
Why can't a derived class pointer point to a base class object without casting?
I have seen few Pet and Dog type examples for this type of basic question here and here, but they do not make sense to me, here is why. Suppose we have the following class structure class Pet {}; ...
30 votes
5 answers
4k views
Why does PHP allow "incompatible" constructors?
Here's a couple of snippets: Overriding constructor method has an extra parameter. class Cat { function __construct() {} } class Lion extends Cat { function __construct($param) {} } ...
21 votes
10 answers
8k views
When is it appropriate to use virtual methods?
I understand that virtual methods allow a derived class to override methods inherited from a base class. When is it appropriate/inappropriate to use virtual methods? It's not always known whether or ...
22 votes
5 answers
7k views
How to implement SOLID principles into an existing project
I apologize for the subjectiveness of this question, but I am a little stuck and I would appreciate some guidance and advice from anyone who's had to deal with this issue before: I have (what's ...
23 votes
4 answers
15k views
Are Interfaces Compatible With Polymorphism
I am having trouble with the concept of interfaces interacting with polymorphic types (or even polymorphic interfaces). I'm developing in C# and would appreciate answers staying close to this ...
21 votes
6 answers
1k views
Selectively disable subsumption in Scala? (correctly type List.contains)
List("a").contains(5) Because an Int can never be contained in a list of String, this should generate an error at compile-time, but it does not. It wastefully and silently tests every String ...
13 votes
4 answers
12k views
Is it good practice to override non abstract methods?
I have a situation where I need to modify the super class method to have a subclass specific logic, but the methods logic is same for all other subclasses. I have two options: 1) make the method ...
8 votes
3 answers
4k views
Java - Use Class parameter in method parameter
I have following class: public class Publisher<T> { private static final Class[] SUPPORTED_CLASSES = new Class[]{T1.class, T2.class}; public Publisher() { if(Arrays.asList(...
12 votes
3 answers
12k views
How can I wrap text around a non rectangular image?
Is it possible to wrap text around a non rectangular image? I want text around maps of different countries to wrap around the country's shape in such a way that the text always keeps the same ...
16 votes
2 answers
9k views
How to unit test subclasses
What is the best way to unit test subclasses? Let's assume there's a base class for which I've already written tests and there are some number of subclasses that override some of the parent's ...
14 votes
3 answers
2k views
Unintrusive way to enforce size constraint on std::vector?
I want to have a certain kind of std::vector that cannot have more than const int MAX_LENGTH elements. I understand that I cannot override std::vector non-virtual functions, which I'd need to do to ...