midterm Flashcards
(146 cards)
what are records?
a struct
- a bundle of information
- several types of data stored together
the set values at any given time defines the ______ of a structure
state
the main point of records/structs is to:
treat a number of pieces of data of different types as a unit (that can be passed as a single parameter for example)
instance variables are
new copies of variables in each specific object that is created
ADT’s keep the interface separate from the implementation. The point of this abstraction is to…
remove users from the burden of understanding how things are dealt with inside the data structure
Example: we have a stack (ADT) implemented in a non-OO language, such as C, using an array.
What are 2 potential issues?
- Nothing to stop outside code from changing stack internals (no encapsulation)
- The code isn’t as generic as it could be (the type of the elements inside the stack has to be defined at compile-time)
What we want from an ADT is independence of interface and implementation
_________ are sent to an object (by calling its methods) and then the object responds
messages
classes define: (2)
- the state information of each object of that class (instance variables – data members)
- the methods (function members) used by each object of that class
the class itself may have state and behaviours as well (class variables, class methods)
Subclasses can ____, _______, ________ any of the methods of its superclass
use, modify, replace
which keyword is used to define subclasses in Java?
extends
eg.
public class Adult extends Person {}
A subclass constructor can also explicitly call one of the superclass constructors but it must be declared where?
*in Java: as the first statement
Java eg.
public Adult(String name, int age, int s) {
super(name, age);
sin = s;
}
*in C++: declared in initializer
- no “super” keyword
*in JS: before the first use of “this” (usually ends up being the first statement)
how can an overridden superclass method be used?
Java:
super.func(…)
C++:
SuperClass::method()
In some languages a class can have more than one superclass. This is called:
multiple inheritance
If binding happens during runtime, it’s called ________
dynamic
- unlike static things which occur at compile time
T/F:
The class of any object in a variable is fixed at compile time
False. OO languages support dynamic class binding
Java example:
GraphicalObject go = new Button();
The static type of go is:
The dynamic type is:
static type: GraphicalObject
dynamic type: Button
To safely cast in Java what keyword should you use
instanceof
eg.
if (account1 instanceof SavingsAccount) {
a2 = (SavingsAccount) account1;
}
POLYMORPHISM = _____ + _______
DYNAMIC CLASS BINDING
+
DYNAMIC METHOD-MESSAGE MAPPING
OBJECT-ORIENTED PROGRAMMING = ___ + ____ + ____
ENCAPSULATION
+
INHERITANCE
+
POLYMORPHISM
If we have dynamic class binding, we also need “Dynamic ____________”
(also known as dynamic dispatch)
(Dynamic) Method-Message Mapping
- Where in the hierarchy do we start when we send a message to an object?
- must jump to dynamic type and move our way up to find the right method to invoke
OOP advantages: (4)
- Often matches the problem domain nicely
- mimicry of physical (concrete) OBJECTS - software and code reusability
- Within a program, inheritance allows common behaviours to be implemented once
- between programs - A common set of classes for standard tasks can be used by many programs - understandability and maintenance
- ease of design
- eg. subdividing the problem
what is shadowing?
when you redefine the same instance variables in subclasses
with shadowing do you get the version of the static or dynamic type
static type.
this is the opposite of what we get when we override methods
int x = 1;
int* intPtr = &x ; /* points to _________ */
intPtr = 5; / follow pointer, assign 5 to _________ */
the location of x
what it points to (x)