Is List a subclass of List? Why are Java generics not implicitly polymorphic?

I’m a bit confused about how Java generics handle inheritance / polymorphism. Assume the following hierarchy – Animal (Parent) Dog – Cat (Children) So suppose I have a method doSomething(List<Animal> animals). By all the rules of inheritance and polymorphism, I would assume that a List<Dog> is a List<Animal> and a List<Cat> is a List<Animal> – … Read more

What are the nuances of scope prototypal / prototypical inheritance in AngularJS?

The API Reference Scope page says: A scope can inherit from a parent scope. The Developer Guide Scope page says: A scope (prototypically) inherits properties from its parent scope. So, does a child scope always prototypically inherit from its parent scope? Are there exceptions? When it does inherit, is it always normal JavaScript prototypal inheritance? … Read more

What are the differences between type() and isinstance()?

What are the differences between these two code snippets? Using type(): import types if type(a) is types.DictType: do_something() if type(b) in types.StringTypes: do_something_else() Using isinstance(): if isinstance(a, dict): do_something() if isinstance(b, str) or isinstance(b, unicode): do_something_else() 7 To summarize the contents of other (already good!) answers, isinstance caters for inheritance (an instance of a derived … Read more

Why do Python classes inherit object?

Why does the following class declaration inherit from object? class MyClass(object): … 6 Is there any reason for a class declaration to inherit from object? In Python 3, apart from compatibility between Python 2 and 3, no reason. In Python 2, many reasons. Python 2.x story: In Python 2.x (from 2.2 onwards) there’s two styles … Read more

Difference between Inheritance and Composition

They are absolutely different. Inheritance is an “is-a” relationship. Composition is a “has-a”. You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would’ve been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now … Read more