Object-Oriented Programming Flashcards
What are the major programming paradigms?
Imperative
–uses statements to change a program’s state
Declarative
–describes the desired result, system figures out how to achieve it
What are the tyes of imperative programming? (3)
- Procedural
- OOP
- Recursive Programming
What are the tyes of declarative programming? (3)
- Functional Programming
- Logic Programming
- Constraint Programming
What type of programming does Python use?
What about java?
-tends toward functional, but supports procedural and object-oriented
-primarily object-oriented, but added constructs to allow
functional programming
Describe the features of objects (3, one depends on the language)
An object:
–collects together related data (“fields”, “attributes”)
–collects together subroutines to work on that data (“methods”, “member functions”, etc.)
–may (depending on the language) allow hiding the data members and/or function members, restricting access to them to its subroutines only
What are the features of OOP languages? (5)
Encapsulation
– related data and procedures are kept together, and data or even
procedures may be hidden from outside access
Classes (definition of data/procedures)
– class variables (belong to the class as a whole, e.g. “static” in Java)
Objects (instances of a Class)
– instance variables (belong to a particular object)
Composition
– objects can contain other objects as members
Inheritance or Delegation
What is not a defining feature of OOP but almost always present?
Inheritance
What is inheritance?
means that a subclass can use subroutines or data members defined in an ancestor class
What is delegation?
means that a member of an object (“receiver”) is evaluated in the context of some other object (“sender”)
– can be done by passing the sender to the receiver as a parameter
Describe polymorphism? What allows it?
Polymorphism
– Inheritance allows Polymorphism
– a single function call in the source code can invoke different functions at runtime depending on the type of the object
– implemented by having a table of function pointers associated withevery object (one table per class)
What is SOLID?
Acronym for five principles of Object-Oriented Design
– SRP: Single Responsibility Principle
– OCP: Open/Closed Principle
– LSP: Liskov Substitution Principle
– ISP: Interface Segregation Principle
– DIP: Dependency Inversion Principle
What is a singleton?
A class which never has more than a single object instantiated
How do you implement a singleton? (2)
- hiding the class constructor –
- providing a static function that returns the sole instanc
What’s an adapter? (1)
Convert the interface of a class to look like another class’s interface
- What’s an iterator? (1)
- How is it implemented?
- allows traversal of a container object and accessing the container’s elements
- typically implemented as a class with functions such as init(), next(), and getItem()
What are the benefits of an iterator? (2)
- avoids exposing the container’s data structure
- can define new traversal operations without changing container’s interface (algorithms and containers are decoupled by iterators)
For python, what are two supported functions and one other entity related to iterating class objects?
- it = obj.__iter__() return an iterator object
- (often self) – it.__next__() get next item
- raise StopIteration if no next item
What is the “Strategy” design pattern? (3)
- instead of “class Foo extends Bar” [Java] or class Foo(Bar) [Python], Foo has a member of type Bar which it invokes as needed
- this allows runtime selection of behavior
- The Strategy pattern decouples behaviors from the classes that use the behaviors
Example: Java’s “interface”/”implements” are a form of this design pattern
Give an example of something that implements the strategy design pattern
– Java’s “interface”/”implements” are a form of this design pattern
What’s a useful way to think of when to use inheritance?
think of inheritance as “is-substitutable-for”, not “is-a” (Liskov substitution principle)
What’s an interface?
An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class) (e.g. methods)
DIP – Dependency Inversion Principle (2) How is it achieved? (idea and term for it)
● High-level modules should not depend on low-level modules; both should depend on abstractions
● Abstractions should not depend on details; details should depend on abstractions
● Achieved by not directly instantiating dependencies (i.e. no “new MyDependency()” anywhere in the class)
– typically use Dependency Injection (in its basic form, an instance of the dependent class is passed in by the caller)
What’s polymorphism?
objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface
What’s the test for a polymorphic object?
If the object successfully passes multiple is-a or instanceof tests, it’s polymorphic. As I’ve described in my post about inheritance, all Java classes extend the class Object. Due to this, all objects in Java are polymorphic because they pass at least two instanceof checks.
Describe the concept of factories
Use a factory object and a factory method
- a Factory Object is an object that can create other objects
- a Factory Method is a method of an object (or class) which returns new instances of one or more classes
Are constructors polymorphic? What about factories?
- constructors are not polymorphic
- factories can be polymorphic – a single function could return objects of multiple different types
What’s an abstract factory?
A class does not create objects itself, but instead delegates the creation to another class which is specified at run-time