Explain 'Covariance' and 'Contravariance' in the context of method overriding and return types.
Last updated
Covariant return types let an override return a more derived type (e.g. base returns Animal, override returns Cat). Supported in Java 5+, C++, and C#.
Contravariant parameters let an override accept a more general parameter type. Most languages (Java, C#) instead require identical parameters and treat differences as overloads.
Both follow Liskov substitutability: outputs may get more specific, inputs more general, so callers are never surprised. C# generics express this with
out(covariant) andin(contravariant).
- https://www.igmguru.com/blog/oops-interview-questions
- https://tryrehearsal.ai/technical-questions/explain-oops-concepts
- https://github.com/Devinterview-io/oop-interview-questions
- https://www.hirist.tech/blog/top-30-c-oops-interview-questions-and-answers/
- https://www.interviewbit.com/oops-interview-questions/
- https://blog.stackademic.com/dont-just-code-the-15-essential-practices-senior-developers-want-you-to-learn-now-84246fb2f6ed
- https://www.reddit.com/r/ExperiencedDevs/comments/1mpaewh/how_should_i_deal_with_junior_devs_that_debate/
- https://www.scaler.com/topics/oops-concepts-in-cpp/
- https://javatechonline.com/interview-questions-object-oriented-programming/
- https://henko.net/blog/my-favorite-interview-questions/
- https://utkrusht.ai/blog/python-interview-questions
- https://www.digitalocean.com/community/tutorials/oops-interview-questions-answers
- https://www.scribd.com/document/510810151/Fragile-base-class