Chapter 12 - OO Concepts Flashcards
which PL serves as the originator of OO programming
SIMULA 67
3 benefits of inheritance
- a new ADT can “inherit” some or all of its data and behavior from an existing type and modify that type without affecting it
- allows for code reuse without harming original
- is hierarchical in nature and well suited to modeling descendant relationships
what is a derived class?
the class defined using inheritance (subclass is not entirely accurate)
what is a parent class?
the class being inherited from (superclass isn’t entirely accurate)
what is a message protocol or message interface
the entire collection of methods of an object
message passing vs subroutine call
message passing implies a request to an object to execute one of its methods that partially entails operating on the object itself
subroutines typically process data sent by the caller
class variables/methods vs instance variables/methods
class variables/methods only have one copy per class and are shared by all instance of the class
instance variables/methods have one copy for each instance of the class and are only used by the instance to which they belong
1 disadvantage of inheritance
creates interdependencies among classes that complicate maintenance
what is dynamic dispatch?
the dynamic binding of messages to method definitions. AKA the messages are statically coded but the receiving object is determined at runtime.
This creates polymorphism of sorts
What is the way in which dynamic dispatch is most often implemented?
through abstract methods (pure virtual methods in C++)
8 design issues for OO PLs
- the exclusivity of objects
- are subclasses subtypes
- type checking and polymorphism
- single or multiple inheritance allowed
- allocation and deallocation of objects
- dynamic and static binding
- nested classes
- object initialization
What does “exclusivity of objects” mean
it refers to how objects are used in the PL and what kinds of data are represented by objects
3 approaches to the exclusivity of objects
- everything is an object (Ruby)
- add objects to a complete typing system (C++)
- include an imperative-style typing system for primitives but make everything else objects (Java)
1 advantage and 1 disadvantage to everything as an object exclusivity approach
Adv: elegance and purity
dis: slow operations on simple objects
1 advantage and 1 disadvantage of adding objects to complete typing system exclusivity approach
adv: fast operations on simple objects
dis: results in a confusing type system
1 advantage and 1 disadvantage of an imperative style system for primitives and objects for everything else exclusivity approach
adv: fast operations on simple objects and relatively small typing system
dis: still some confusion because of two type systems
4 things that must hold for a derived class to be a subtype
- the “is-a” relationship holds between the base class object and an object of the derived class
- if the derived class is also a parent class, then objects of the derived class must expose all of the members that are exposed by objects of the parent class
- at a minimum, an “is-a” relationship should guarantee that in a client a variable of the derived class type could appear anywhere a variable of the parent class type was legal (syntactically equivalent)
- the derived class objects should be behaviorally equivalent to the parent class objects when used as a parent class object (Liskov substitution principle - aka semantically equivalent)
What must be done to allow overridden method to be type checked statically
must have the same parameter types and return type
2 design issues with multiple inheritance
- complexity = two base classes could name the same variable or have a common ancestor
- efficiency = more of a perceived issue than a real issue
2 advantages of allocating objects on the heap?
- creates a uniform method of creation and access through pointer or reference variables
- simplifies assignment and implicit dereferencing
What is object slicing?
some information is lost about an object that is allocated on the stack
this occurs when an object on the stack adds a data field and then a child class is assigned as a reference to its base and that data is no longer reachable
What is the purpose of a nested class
allows information to be hidden in one class and be visible to another (nested) class
-kind of equivalent to friends in C++
2 design issues of nested classes
- what bits of the nesting class should be visible in the nested class
- should any of the nested class be visible in the nesting class
3 design issues with object initialization
- should an object be initialized manually (aka by the constructor only)?
- should objects be implicitly initialized
- when creating a derived class object, should the base be initialized implicitly or should the programmer explicitly manage that