Object Oriented Programming Flashcards

Learn OOP with C# .NET

1
Q

OOP = 3 PILLARS

A
  1. Encapsulation 2. Inheritance 3. Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is gang of 4

A

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book (Design Patterns: Elements of Reusable Object-Oriented Software) for explaining the concept of Design Pattern in Software development. These four authors are collectively known as Gang of Four (GOF).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Are patterns always good

A

gang-of-4 software design patterns not covered - using patterns tend to make the code more complicated and thus harder to maintain.

goal should be simplicity - not“how can i apply a pattern to this problem”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

WHAT IS SOLID

A

SOLID is an acronym that deals with the following five object oriented design principles:

S: Single responsibility principle - a class should be responsible for doing one thing and doing that one thing quite well.

O: Open / closed principle - a software entity like an interface should be open for extension, but closed for modification.

L: Liskov substitution principle - a method must be able to accept a derived object in place of a base class object when specified as a method parameter.

I: Interface segregation principle - better to define many client-specific interfaces than one general-purpose interface.

D: Dependency inversion principle - decouples high and low level modules by using an abstraction like an interface.

* also of note: use composition over inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

WHAT IS OOP

A
  • model representing concepts as objects that have data fields and behaviors (methods)
  • objects are an instance of classes and structs - used to interact with one another
  • OOP supports abstraction, encapsulation, inheritance, and polymorphism.
  • kind of also includes generics, containers and OO principles / patterns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WHAT IS SEPARATION OF CONCERNS

A
  • P-BL-D: presentation, business logic, data access layers
  • splitting functionality so change in may has little to no impact on other layer.
  • MVC is an example
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

WHAT DOES DONT REPEAT YOURSELF mean…

A

consolidating functionality into a single system, assembly, class or method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

WHAT IS AN OBJECT

A
  • an INSTANCE if the class or struct
    • state, behavior, identity
    • class methods, properties, fields, events can be accessed.
  • NEW keyword needed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

WHAT IS OOP MESSAGE PASSING?

A
  • objects passing messages to each other to perform high level functions
  • how the method performs the function and the class variables it uses are all encapsulated withing the method and the class
  • think private

This concept is in sharp contrast to the concept of exposing the inner details of a class to outside user exposing functionality. Having methods or fields exposed makes maintenance easier for updates/changes to code base and prevents duplication but still a concern.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

WHAT IS ENCAPSULATION

A

group of RELATED properties, methods or other members are treated as SINGLE OBJECT.

hiding unimportant details from the user of a class - to better focus on core capability of that class

fields and methods hidden by marking them as private - if marked public they are considered the PUBLIC INTERFACE for how other developers will use that class. Usually one or more constructors used to create and instantiate the class INTO an object.

encapsulation supported through use of classes and structs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

WHAT IS POLYMORPHISM

A

use derived class in place of a base class.

  1. define a virtual method in base class
  2. override that method in a derived class

at runtime if the object is a derived class then the DERIVED class’s method is called instead of the base class method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

WHAT IS INHERITANCE?

A

Base class fields, methods, properties and events are available to derived class IF declared as public, protected, or internal protected

private fields still present in derived class but NOT ACCESSIBLE

only single inheritance supported.

interfaces may also be inherited

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

WHAT IS COMPOSITION?

A

class contains another class, not inheriting from it.

person class composed of 2 properties with their own class - when the instance of Person is destroyed the the composed objects die along with it

class Person

{

string firstName { get; set; }

string lastName {get; set; }

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

WHAT DOES THE TERM HAS-A RELATIONSHIP MEAN WHEN DESIGNING A CLASS

A

When dealing on how to design a class, if another object HAS-A relationship with that class then composition should be used instead of inheritance.

for example a car has-a wheel -> composition should be used here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

WHAT DOES IS-A MEAN WHEN DESIGNING A CLASS

A

when deciding on how to design a class, if another object is-a specialized type of that class, then inheritance should be used instead of composition.

a house is a building, inheritance should be used

is-a has closer ties to its parent object so will be inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Difference between LOOSE and TIGHT coupling

A

coupling is speaking to the dependency of one class on another

tight = you asked for banana and you got a jungle that has a gorilla holding a banana.

  • when one class changes it affects changes on other classes
  • one class with 2 many responsibilities
  • one concern spread over many classes rather then consolidated into its own class
  • concrete class used - preferable to use interfaces

interfaces make testing better and more maintainable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

WHAT IS ASSOCIATION

A
  • relationship between objects where each object has its own life cycle - none of the objects own any other object.
  • contractor has 20 jobs going and each job has 10 employees on it. If 1 employee dead, i job is dead = contractor still has independent lifecycle.

Association is implemented via dependency injection. Usually by constructor but can be done via set property or method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

WHAT IS AGGREGATION

A
  • a specialized form of association
  • all objects have own life-cycle BUT one object uniquely owns another object.
  • an object can only be owned by one parent at a time
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

WHAT IS DEPENDENCY INJECTION?

A
  • design principle - injecting dependency into a class vs. the class creating it.
  • usually injected in the constructor but a set property could be used if the dependency is not yet available at creation time.

works hand in hand with inversion of control principle for resolving dependencies.

https://www.youtube.com/watch?v=mCUNrRtVVWY&t=82s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

WHAT IS INVERSION OF CONTROL

A
  • design principle - frameworks lower level modules control the processing
  • IOC and DI all about removing dependencies from the code.

DI passes an interface or abstract base class so that different implementations can be used without having to recompile the code.

IOC and DI are similar in that DI uses IOC to allow code to call interfaces without caring about the actual implementation class. Only the interface is important.

21
Q

WHAT IS SINGLE RESPONSIBILITY RULE

A

class should be responsible for doing one thing and doing that one thing well.

22
Q

WHAT DOES OPEN CLOSED PRINCIPLE MEAN

A
  • software entity like interface, class, method should be open for extension BUT closed for modification.
  • interface is example: interface releases to large body of users and then a new method is added, a method’s calling arguments are changes or a method is deleted. All the code that implements this interface would also have to change.
  • adding OCP for interfaces it guarantees backwards compatibility.
23
Q

WHAT IS THE LISKOV SUBSTITUTION PRINCIPLE

A

method must be able to accept a derived object in place of a base class object when specified as a method parameter

24
Q

WHAT IS THE INTERFACE SEGREGATION PRINCIPLE

A

client specific interfaces better then one general purpose interface that includes the signature of many methods

25
Q

WHAT IS DEPENDENCY INVERSION PRINCIPLE

A
  • related to loose coupling

In the past, conventional dependency relationships were estabolished between high-level modules to low-level modules. this is known as top-down structure. the dependency inversion principles changes this relationship to one of equals were both the high level modules and low level modules are independent of one another and subject to the following rules:

A. high level should not depend on low level modules. Both should depend on abstractions like interfaces.

B. Abstractions should not depend on details. details should depend on abstractions like interfaces.

26
Q

WHAT IS A CONSTRUCTOR

A

method used to initialize the state of an object. Invoked at time of object creation.

  1. constructor name must = class name
  2. must have NO RETURN TYPE
  3. May take parameters, including default parameters.

If no constructor defined then compiler will provide a default constructor - which takes NO parameters. Default constructor intiializes all fields to their default values of zero for value types like int and null for reference types.

27
Q

WHAT IS VIRTUAL KEYWORD USED FOR

A

indicates that a method may be overridden in a derived class.

if a method with the same signature is defind in a derived class and the base class has not marked the method as virtual then the base class method is hidden in the derived class.

28
Q

WHAT IS THE OVERIDE KEYWORD FOR

A

provides a new implementation in a derived class for a virtual member definded in a base class. An inherited virtual method, property, indexer, or event may be extended in a derived class by using the override keyword.

29
Q

Difference between static and dynamic binding?

A

STATIC BINDING

  • early binding, compiler knows at COMPILE time that a certain method will be called.

DYNAMIC BINDING

  • late binding, compiler CANNOT determine which method will be called at compile time and this can only be determined at runt time.
30
Q

WHAT IS UML

A

Unified Modeling Language

  • standard way to visualize the design of a system through the use of diagrams
31
Q

WHAT IS CODE REFACTORING MEAN?

A

improve the internal structure of the code after it has already been written to simplify, improve coherency and remove any code smells.

NO CHANGE IN FUNCTIONALITY

easier to understand and maintain

32
Q

WHAT IS SINGLETON PATTERN AND WHEN SHOULD IT BE USED

A

restrictes the instantiation of a class to a single object.

  • done by declaring constructor as private and providing a static method to return the object - if the instance does not exist one is created.

IT SHOULD NEVER BE USED

  1. restricts scalling of applications
  2. resource bottleneck if designed wrong
  3. has come across issues when dealing with multi-threaded application that are tyring to access a singleton resource but get into deadlocks.
33
Q

DIFFERENCE BETWEEN CLASS AND AN OBJECT?

A

CLASS

blueprint of object creation and what capabilities object will posses
objects are instantiated from class and reside in memory when app is run

OBJECT

created from class.

34
Q

DIFFERENCE BETWEEN ABSTRACTION AND ENCAPSULATION

A

ABSTRACTION

  • process of generalization, takes a concrete implementation and makes it applicable to different but related types of data.
  • done by seperating interface from the implementation and involces hiding the implementation by using abstract classes and interfaces.

ENCAPSULATION

  • hiding the internal details or mechanics of how an object does something.
    example: public method might be using several private class variables, call private methods, use complex algorithms to achieve result used for public method.

the user of the class does not need to see any of this.

35
Q

IS IT A GOOD IDEA TO EXPOSE CLASS FIELDS AS PUBLIC SO THAT OTHER OBJECTS WILL BE ABLE TO USE THEM TO PERFORM SPECIALIZED FUNCTIONALITY?

A

NO

public fields should never be used.

  1. violates concept for encapsulation. if some specialized functionality needs to be performed then that functionality should be encapsulated by the class so that other users might have a chance to use it.
  2. exposing fields as public = risks damaging state of object

if a common field needs to be exposed then its is best to expose it as a PROPERTY

a property may be specified as public when reading the value and as private when setting the value

public class Myclass {
 public int confirmed { get; private set; }
 public Myclass() {
 this.confirmed = "I am allowed to set the private property";

}

}

outside of the class this would be okay

Console.WriteLIne(Myclass.confirmed); //because the get is public

outside of the class this would not work

Myclass.confirmed = “Can I change the set on this?”;

36
Q

WHAT DOES SUBCLASSING MEAN

A

A solution involves creating a base class and one or more derived classes to spread the intelligence of a system horizontally.

  • using inheritance and ploymorphism implements as virtual methods in the base class.
  • virtual methods are then overridden in the derived classes. the base class may also implement functionalitty common to all subclasses.
37
Q

SHOULD INHERITANCE USUALLY BE PREFERRED OVER COMPOSITION?

A

NO

inheritance tends to be overused by newbe’s - composition should be used instead.

sometimes a project needs several classes that appear to be similiar but have different behavior.
in OOP sometimes handled with inheritance - base class , subclasses that inherit from base class and have the properties and functions that are uniquie to the sub class.

can lead to very messy code

alternative is composition - single class that can be composed toi handle the different behaviors.

Awesome example:

https://scottlilly.com/c-design-patterns-composition-over-inheritance/

38
Q

You are working on a new project where code libraries will be released to other developers outside your organization periodically. During those releases, new methods will typically be provided. Some of these methods will need to be implemented by the customer and some not. In general, should an interface be used, or should an abstract class be used to define that functionality?

A

If an interface is used to define the functionality, then anytime a new method is added to an interface then each and every user of that interface will have to implement that method - whether they need it or not. That is one of the limitations of interfaces - there are no optional methods. Each method that is added to an interface must be implemented.

If an interface has been released to a customer base and that interface changes, their code stops working until they implement that new method. That is the reason for the recommendation that once an interface is released, that it never be changed. So, how can new functionality be added when using an interface? One way to add new functionality is to define a new interface that inherits from the old interface. That way the old interface methods are supported and new methods can be introduced that won’t break existing customers use of the old interface.

But, even that approach starts to become unsatisfactory if many updates are released and each update requires a new interface. The problem then becomes a proliferation of interfaces that makes maintenance difficult for both the project and the customer base. In cases where unknown future methods will most likely be added at some point, then the paradigm that should be used is an abstract base class. This provides much more flexibility. If an interface needs to be added to the abstract base class, this can be easily done without affecting the customer code base by providing a default virtual method.

Even if a method needs to be added to an existing interface used only by the abstract base class - even this can be accomplished without affecting the customer code base by simply providing a default virtual method. More importantly, optional methods can be defined as virtual or concrete and given an implementation in an abstract class.

This provides quite a bit of flexibility that allows the project to provide functionality to some customers that need it while at the same time making it easy to ignore that functionality for other customers that don’t need it. This does not mean to suggest that an abstract class should always be used over an interface. It simply means that in certain situations that an abstract class is preferable.

39
Q

WHAT IS THE MOST IMPORTANT PRINCIPLE THAT SHOULD BE CONSIDERED WHEN DECIDING WHETHER TO USE COMPOSITION OR INHERITANCE WHEN DESIGNING A NEW CLASS?

A

whether the new class IS-A type of that other class or HAS-A relationships with that other class.

IS-A = INHERITANCE

HAS-A = COMPOSITION
- subclass can be encapsulated within the base class because it HAS-A relationship.
40
Q

A type needs to be subclassed into many different classes with derived virtual methods. What is a single word for this that also means having multiple forms?

A

Polymorphism

  • works by defining a base class with virtual methods and then overriding those virtual methods in derived classes.
41
Q

What is the way a class can use a parent class to acquire all of its functionality?

Give code example…

A

INHERITANCE

public class Animal
{
}
public class Dog : Animal
{
}
42
Q

What are top 3 OOP pillars - and the 4th

A
  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction
43
Q

Should classes be tightly or lossly coupled - explain why and how.

A

LOOSELY COUPLED

  • a class should have little to no dependencies on other classes so that changes in one class wont force changes in another class.
  • easier to test and maintain

HOW LOOSE COUPLING IS ACHIEVED

  • using a design that promotes single-responsibility, seperation of concerns, and the liberal use of interfaces / abstarct base classes vs concrete classes.
44
Q

If a new class has a dependency on an object, then what criteria should be used to decide on whether the object should be created outside the new class or inside the new class?

What is the name of the principle if the object should be created outside the new class and then passed to the class?

A

DEPENDENCY INJECTION

  • creating an object outside of a class where it is used.
  • DI should be used if the dependent object is likely to use different implementations or classes. In this case an interface is usually passed. However a base class could also be passed in some circumstances. Primary reason for DI!
  • DI also used when an object is used by many different classes within an app since it would make little sense for each class that uses it to creat it.
  • DI supports mocking of objects to make unit and Q/A tests easier. SO in those environments DI is more heavily used even though it might not otherwise be required.
45
Q

Should a class be designed to have more than one primary responsibility?

A

NO

  • class should have only 1 primary responsibility and should focus on doing that well.
  • single responsibility rule.
  • String class is an exception - has over 100 methods defined, yet single responsibility is strings.
46
Q

Difference between UPCASTING an object and DOWNCASTING an object?

A

UPCASTING

  • upcasting an object means casting a more derived object into a less derived object.

DOWNCASTING

  • downcasting an object means casting a base class object to a more derived object.

upcasting to a base class can be done implicitly without a cast operator. Downcasting to a derived class from a base class object requires a cast operator

upcasting always succeeds - downcasting is considered to be a design flaw and somewhat dangerous.

47
Q

A method in a lower level class requires an object that is created near the top of a large app. there is only a single instance of this object.

One solution is adding a parameter for this class to each method call - there are 25 different method calls that would need to be modified to pass the object into the lower level class, but none of those methods require the object.

How would you inject the object into the lower level class without modifying any of the intervening method calls or changing the code to create the lower lovel class earlier.

A

Create a static public property within the lower level class.

this will allow any caller to interject the object into the lower level class . SInce the property is static a reference to the lower level calss is not required and the property may be set and accessed from anywhere.

48
Q

When should gang of 4 design patters be used?

A

Old answer - at the very start of a project.

New answer - patterns only used after simplier approaches have been tried and valid reason for pattern is validated. If so refactor code to use pattern.

Using OOP like seperation of concerns, DRY, Single responsibility rule, favor composition over inheritance, SOLID principles etc are better chouces that tend to produce simplier code.