Object Oriented Programming Flashcards
Derived Classes/Child Classes
An EXTENSION of a base class Classes that inherit functions and variables from a base class
aka “child” class
Base class/Parent Class
Class that can be EXTENDED by a derived class.
Includes functions and variables that can be inherited by a derived class
Inheritance
Mechanism for EXTENDING a class
-If you extend the class rather than editing it, you will not have to “re-debug” the original code (because it was not changed since the last time you debugged)
-It allows you to reuse existing DEBUGGED code by allowing a class to acquire properties (data and operations) of another class
Multiple Inheritance
Inheritance of properties from multiple classes
-Generally not recommended, but some languages (like C++) will allow it
“Public”
Indicates that the data/operation can interface with the rest of the world
can be accessed from outside of the class
“Private”
Indicates that the data/operation can only interface with member functions of the class in which it was written
“Protected”
Indicates that the data/operation can ONLY interface with the data/operations of a derived class
DO NOT USE THIS UNLESS YOU KNOW THE CLASS IS GOING TO BE INHERITED (otherwise you can get yourself in trouble when it comes to debugging)
Practical applications of inheritance
- Principle of Least Privilege & Role-based access control
The most general base class should have the fewest permissions. As the roles become more specific, they are given more and more privileges.
2.
Reading a class block in a class diagram
Minus (-) inidicates private.
Plus (+) indicates public.
Pound sign (#) indicates “protected”.
The name of the class goes in the top box.
The data/variables of the class go in the middle box.
The operations/functions of the class go in the bottom box.
Inheriting “private” variables/function
Private members of the Base Class are present within objects of the Derived Class but are NOT directly accessible within the Derived Class code
Constructors
Constructors are not inherited.
Representing a derived class in a UML class diagram
Only show what is EXCLUSIVE to the derived class.
NEW variables
NEW functions
Anything that is overwritten
Memory handling of inheritance
When you create a child class, you are also “creating” a parent class inside of that child class. There is a Base class object nested within each Derived class object. The base class object literally comes directly before the derived class in memory. The Base class object “contains” all inherited members
Accessing private members of the base class
If the derived class inherits PUBLIC functions from the base class…. then those functions can still access the private variables of the base class via that public function it just inherited
Constructor inheritance
No such thing. Derived classes do not inherit the constructor from the base class.
You will have to make a new constructor for the derived class.
Order in which the destructors are called
Destructors are called in the upward inheritance order (less abstract constructors come first) Derived class destructors will be called before the base class destructor.
Parameters
Parameters are needed in the function header. It is a DECLARATION of variables that you can expect to find in that function. They require datatypes just like any declaration
Order in which the constructors are called
Constructors are called in the downward inheritance order (more abstract constructors come first) Base class constructors are called before the derived class constructors.