Explain inheritance and parameterized types Flashcards

1
Q

Polymorphism

Describe inheritance and parameterized types, and how these relate to polymorphism and code reuse.

A

Polymorphism is the name of the phenomenon that allows an entity (object, method, function, etc) to behave as if it had several different types. I.e, it’s the ability of an object to take on many forms. Any object which IS-A subtype of an object type is considered to be polymorphic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Subtype polymorphism

Describe inheritance and parameterized types, and how these relate to polymorphism and code reuse.

A

If S is a subtype of T, then any object “O” of type S can behave like an object of type T.
For every subtype, “O” has a form (i.e Scania is a Truck which is a vehicle, or GoldenRetriever is a Dog, which is a Mammal, which is an Animal). Thus Scania has the behavior of a Truck, and a Vehicle, whilst GoldenRetriever has the behavior of a Dog, a Mammal, and an Animal.
A variable “x” with a static (declared) type T can at runtime point to an object with dynamic type “S”. I.e, it can use any methods defined in T (or any superclasses which T inherits from).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Parametric polymorphism

Describe inheritance and parameterized types, and how these relate to polymorphism and code reuse.

A

When one creates a method or a class with generic code which works for whichever type the parameter is. Simply said, the code is independent of the object’s type, and thus applicable to multiple different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Type constructors, type parameters and type arguments

Describe inheritance and parameterized types, and how these relate to polymorphism and code reuse.

A

Type Constructor
A type constructor is the syntax around creating a collection of a specific type. For example “….[ ]” is the type constructor for arrays. The specified type of the collection is the type argument.

Type Parameters
A blueprint or placeholder for a type declared. I.e, it specified which types can be inputted into a method - it has several bounds.

Wildcards
It’s essentially just a way to specify what a class/method can take in (without the use of arguments). A wildcard can have only one bound (lower or upper), while there is no such thing as a lower bound for a type parameter.

Type Arguments
A type argument is the actual type which is inputted into a method. So while type parameters define what types are allowed to be inputted the type argument is the type that is inputted.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Delegation

Describe inheritance and parameterized types, and how these relate to polymorphism and code reuse.

A

This is when a class (B) contains an instance of another class (C) in order to make use of C’s methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Subtypes and subclasses

A

Subetypes dont need to specify what it is subtyping to. could be a class or a interface..

Subclasses are subbed to classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly