Object Oriented Programming Flashcards
(14 cards)
What is a class?
A class is a definition or blueprint of the fields (attributes) and methods (actions and functions) of an entity.
For example cats have a name, number of remaining lives, and colour (fields)
and they can meow, eat, and sleep (methods).
we may decide to model real world cats using a class.
Note that cats here is a general definition of a cat, not a specific cat.
What is an object?
An object is an instance of a class.
For example, if we have a class “Cat”, and instance of this class might have the name jerry, 2 more remaining lives, and the colour blue.
This cat can meow, eat, and sleep just like all cats, but its fields now have specific values that make it an individual.
What is encapsulation?
The fields of an instance should only be read or written by methods of that instances class.
That is, all fields should be kept private, where methods can be either public or privates.
To access the specific properties of an instance, you should use a method of the form get_property()
this is an important concept because if we allow the fields to be changed willy nilly, then odd things can happen. for example, a cat can only lose one life at a time, so we can clearly define a function that will reduce the number of remaining lives, rather than allow other programmers to reduce the number of lives by a silly amount,( such as 12)
What is abstraction?
Abstraction is the process of hiding aspects of the object that are outside the current scope. it is useful for organising complicated code that has many levels of things happening.
What is inheritance?
The concept of a class being able to use the fields and methods of an ancestor class. e.g. an animal can eat and sleep. a cat is a type of animal, and so it can eat and sleep as well. but it can do other things that are unique to cats, such as meow and purr.
sub-type = descendent super-type = ancestor
parent and child are immediate only.
What is a Compositional Relationship?
A car is composed of many parts including a steering wheel, but it is not a type of steering wheel, and so it should not inherit from the steering wheel class.
What is Multiple Inheritance?
The ability to inherit from multiple classes.
What does it mean that objects are polymorphic?
x.foo() means that you have to look in the x class for method foo()
What are instance members?
fields and methods of an instance.
What are class members?
also called static fields or static methods. these are fields and methods that are accessible outside of an instance. please note that these are not really a part of OOP as they are more like normal variables and functions that are just under the namespace of a class, which is good, but not OOP :)
What is the constructor?
The method that sets up the instance of a class upon creation.
This can range from simply setting fields to default values to something far more complex like setting up a compute graph in tensor flow.
What is an interface?
A set of methods or actions that can be shared between otherwise unrelated classes. e.g. a cat and a fighter jet can both move and stop, but it doesn’t make sense for them to inherit from the same ancestor.
What is an abstract class?
A class that is not intended to be instantiated. It is only intended to be inherited.
E.g. mammal, we would probably instantiate as a cat rather than a mammal. but it is still useful (reusable code) to have it as an abstract class from which cat, dog, human, can inherit members.
What is prototypical inheritance?
A slightly different form of inheritance where inheritance is defined by links between objects, rather than formal classes. in practices, this results in very similar functionality to normal inheritance but a different syntax.