OOP Interview Questions Flashcards
What is the difference between aggregation and composition?
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.
What is OOP?
It is a programming paradigm where rather than performing actions, we model data and behaviors within objects.
Rather than ‘actions’ it’s objects, rather than logic, it’s data.
Objects themselves encapsulate the data and the associated behavior (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.
What is the difference between inheritance and composition?
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.
The 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.
Inheritance typically models an “is a” relationship - i.e. a banana is a more specific type of fruit.
Whereas composition typically models a “has a” relationship - i.e. a car has an engine.
Is a car and engine better modelled through composition or aggregation or inheritance.
Not inhertiance, since a car is not a type of engine and vice/versa. However, the question of aggregation vs composition is dependent on the domain. In reality it’s an aggregation - as an engine can exist perfectly well without the car. However - in the application, it may not be so simple. For instance - in a game, a car engine may not have any reason to exist outside of the car - so it might be a composition. However, in an car manufacturers database, it may be an aggregation - as an engine may be swapped in and out of cars, but will still exist, regardless of whether or not it’s in the car.
What is SOLID?
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 do you implement the O in SOLID for javascript?
You need to make sure that a user of your code can extend the behaviour of an object without having to open and edit the JS module.
For instance, imagine you have an object that has a fixed array of ‘types’ - say ‘icecreams’. You can improve on that by adding a method that allows you to add types as part of the interface.
What is an example of the L principle?
So 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. Think of C++, in a game, you might have an abstract base class of Weapon. The weapons you derive from this should implement the same functions - such as attack, damage etc. This allows you to create say an array of type Weapon - and through the magic of polymorphism, you can store any derived type of weapon in that array.
Now - as long as you have implemented the weapons such that they all operate in the same way - we are not breaking the Liskov principle.
Another example might be the square / rectangle issue. In maths these are basically identical. But in code - you might derive square from rectangle. However, 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.
Also - avoid inheritance wherever possible. Use composition. ;) THIS IS STILL RELEVANT TO JAVASCRIPT - because you can change a base type at run time, which can affect objects down the tree. So composition should still be favoured.
What are the basic concepts in OOP?
Inheritance,
Encapsulation
Abstraction
Polymorphism
What is the difference between OOP and Prototypal inheritance?
In classical OOP - we typically have two types of abstractions, objects and classes. The classes are like templates - and we do our inheritance with classes, and later create concrete versions (or objects) based on these templates, and inheritance hierarchies.
In prototypal inheritance, there is is only objects. Inheritance is implemented by creating a prototype chain, in which a child objects prototype points to another object, and so on down the chain until you get to the base object.
One key difference is that because there are no templates, and we are dealing with real objects - a change in a prototype down the chain during runtime will affect all objects that inherit from that object.
Is the I (interface segregation principle) relevant to JavaScript?
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.
What is the purpose of Dependency Injection?
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.
What is the difference between Dependency Injection and the DIP?
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.
What is polymorphism in JavaScript?
Polymorphism is the ability of different objects to share the same interface, while the implementation below the surface may differ.
Basically you can call the same method on different objects, by the objects will respond in their own way.
Polymorphism exists in JS in that child objects can override methods in the parent class - and provide their own implementation. But it’s not as strictly enforced (say at compile time like in C# or C++).
What are the two most important OOP principles in JavaScript?
Inheritance / Encapsulation
What is duck typing?
Basically it means code will accept any object, as long as it has a specific method.