OOP Interview Questions Flashcards

1
Q

What is the difference between aggregation and composition?

A

— Aggregation (hollow diamond in UML) implies that the objects can exist without the object they are aggregated within

— Composition on the other hand (solid diamond in UML) implies that the child cannot exist without the parent

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

What is OOP?

A
  • A programming paradigm where rather than performing actions, we model data and behaviors within objects
  • Objects themselves encapsulate the data and the associated behavior (also known as methods)
  • We typically try to design objects such that they have what is called a single responsibilty
  • More complex objects can be created by the use of inheritence, or composition
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between inheritance and composition?

A

Inheritance

— Describes a parent child relationship, where typically the parent is a more generic type of object

— The further down the inheritance chain we go the more specific the object becomes

— Child objects inherit the behaviors and data of the parents

— This allows child object that belong to a single ‘class of object’ to share similar traits (and therefore code) - but implement aspects that are specific to themselves.

Composition

— on the other hand, builds up a model of an object by organising other objects within itself that it delegates responsibility of certain tasks to

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

What is SOLID?

A

— A set of programming principles that describe good programming practices when using OOP design

S > Single reponsibility

O > Open for extension, closed for modificiation

L > Liskov principles

— objects in a program should be replaceable with their subtypes with no problems for the program

I > interface segregation principle

— Many client specific interfaces are better than generic interface

D > Dependency Inversion principle

— Depend on abstractions, do not depend on concretions

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

What is an example of the L principle?

A

— The point of the liskov principle is that you should be able to use the child objects of a parent object in exactly the same way

— Example:

  • Square/ rectangle > in maths these are basically identical
  • But in code, you might derive square from rectangle
  • Your square will probably have/ implement setWidth/ setHeight methods
  • This doesn’t make sense for a square.
  • So the question becomes, is that an appropriate use of inheritance
  • Basically - regardless of whether the base classes should or should not behave in a certain way, any derived classes should NOT break the behaviour of the base class
  • TDD should be able to help maintain solid code when working like this
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the basic concepts in OOP?

A

Inheritance

— Inheritance (OOP) is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior)

— When creating classes that have functionality that can be shared by other classes, its best practice to refactor the shared methods into a base class

— I can further enhance my base class by adding methods that are set as virtual so inheriting classes can override their functionality and keep the same method name

— Base class constructors can also be called in conjunction with child class constructors

Encapsulation

— A language mechanism for restricting direct access to some of the objects components

— Let’s say you have a class that has logic that is used only by its class, Refactor that logic into a private method to “encapsulate” the code so it’s not exposed to other references where it’s not necessary

Abstraction

— The essence of abstractions is preserving information that is relevant in a given context, and forgetting information that is irrelevant in that context

— Can be thought of as the natural extenesion of encapsulation

— Applying abstraction means that each object should only expose a high-level mechanism for using it

— If I am writing a class library, the depending application does not need to understand the inner workings, it only needs access to the method endpoints or APIs. I can use an Interface so any object that inherits the interface knows exactly what the options are for invoking it

Polymorphism

— In object-oriented programming, polymorphism refers to a programming language’s ability to process objects differently depending on their data type or class

— It is the ability to redefine methods for derived classes

— The ToString() method is a good example to use when asked about polymorphism during an interview. Let’s say we have a car class and a vehicle base class. In the vehicle class is a ToString() method marked as override that will return the name of the vehicle using (make + “ “ + model). In the Car class that is the inheriting class of Vehicle we could have a ToString() method that is marked as override which return the make, model and transmission. This is known in OOP as Polymorphism and explained in the coded example below.

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

Is the I (interface segregation principle) relevant to JavaScript?

A

Yes - but not in the same way it was in say C++. JavaScript does not have interfaces. So the point in C++ / PHP etc was to not write monolithic interface classes. You’re better off writing smaller, more specific interfaces, and implementing only those that you need. In JavaScript - this could apply to the interfaces you expose via modules. Don’t require consumers of your API to implement methods that are potentially optional. Say we have a module that lets you create a ROUTE. This SHOULD be all you need to create a route, but the person that wrote this module requires you to also call FixIE8() and pass it a callback before the route works. This is not something you need to handle because you aren’t supporting IE8. This is a poor interface.

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

What is the purpose of Dependency Injection?

A

— To make sure your code is loosely coupled

— So, for instance, you may take an object with an agreed upon interface - and delegate to that object

— However, rather than using ‘new object’, you’d take a reference to the object

Another way of thinking of it

— you could just accept methods as parameters; that way, (because we don’t have interfaces in JavaScript - although we can fake it by testing for the methods), you don’t care about what the object creator has called their internal methods. You just pass the methods in directly to the functions that call them, and you can call them how you want

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

What is the difference between Dependency Injection and the DIP?

A

— Dependency injection is a specific form of inversion of control

— It describes how an object obtains it’s dependencies

So typically, dependencies are supplied, rather than created within the object itself. Dependency Inversion principle is not necessarily concerned with how objects obtain dependencies, it’s more about how high level objects are decoupled from low level objects

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

What are the two most important OOP principles in JavaScript?

A

— Inheritance/ Encapsulation

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

What is duck typing?

A

— Basically it means code will accept any object, as long as it has a specific method

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

Object creation patterns describe…

A

— Encapsulation

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

Code reuse patterns describe…

A

— Inheritance

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

What is abstraction?

A

— The concept of modelling real world entities in code

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

What is meant by generalisation?

A

— In OOP we create generalisations, so a bike is a more generalised type of mud bike

— So mud bikes inherit from bikes

— In classical OOP > classes are generalisations (as they cannot be concretions). In prototypal inheritance, the prototype is the generalisation

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