OOPS Flashcards
What is OOPS?
OOPS is a functional paradigm used to organise the code. OOPS uses the concept of classes and objects. We consider classes as a blueprint of objects. Usually, when we are designing we will consider a noun as classes. Example, if there is requirement/usecase, design a car manufacturing facility, then we will consider nouns, such as, car, factory to be designed classes.
What are 4 principles of OOPS?
The main 4 principles of OOPS are-
a. Abstraction - Abstraction means hiding. Using OOPS, we will hide a certain data from the outside world. For example, when I design a car, I may have methods like checkSensors, or properties like valves. Data Abstraction is the property by virtue of which only the essential details are displayed to the user. The trivial or the non-essentials units are not displayed to the user. Ex: A car is viewed as a car rather than its individual components. We achieved abstraction using interfaces. It is in the design level.
Data Abstraction may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of similar type and also help in classifying/grouping the objects.
b. Encapsulation, Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Another way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.
Technically in encapsulation, the variables or data of a class is hidden from any other class and can be accessed only through any member function of own class in which they are declared. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding. Encapsulation can be achieved by Declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables.
c. Inhhetence and Polymorphism.
What is prototypal inheritance?
In prototypal inheritance, all objects are linked to a prototype and they share the method which is defined in the prototype.
What is a constructor function?
To acheieve inheritance in JS, we can use Constructor function. THis constructor function is a function that is called with a new keyword. New keyword will do the following: a. First, create an empty object. b. Then it assign a this c. It will link to the prototype. d. returns the object.
We can create a function inside a constructor function. But that will create the copy of the function in all instances.
const constructorFunc = function (firstName, year) { this.firstName = firstName; this.year = year; };
const bharath = new constructorFunc("bharath", 1986); const shruthi = new constructorFunc("shruthi", 1991);
How do you create a method using constructor function?
onst constructorFunc = function (firstName, year) {
this.firstName = firstName;
this.year = year;
};
const bharath = new constructorFunc("bharath", 1986); const shruthi = new constructorFunc("shruthi", 1991);
constructorFunc.prototype.calAge = function () {
return 2024 - this.year;
};
console.log(bharath.calAge());
is bharath.__proto___ == consrtctor.Prototype?
yes, it is
Can we create a property in the prototye?
Yes, we can create.
Person.prototype.species = “yeno”;
But this share the property to all instance..
We can use
isPropertyof
What is Prototype Inheritance and chain?
In JS, objects will have a special hidden property called prototype. This will be null or refer to another object.
When we read a property from an object, if its not available in the object, it will look into the prototypes. This is called Prototypal inheritance.
Which is the base of all prototype?
object.prototype
Can we add a method tlike array.prototype.unique = function
Yes, but not recommended
What is ES6 classes?
They are just a synthetic sugar for constructor functions.
// ES6 classes
class PersonCL { constructor(firstName, year) { this.firstName = firstName; this.year = year; }
calAge() {
return 2024 - this.year;
}
}
const bharathES6 = new PersonCL("bharath", 1986); const shruthiES6 = new PersonCL("shruthi", 1991);
- Methods inside the classes are actually defined in the prototype.
- Classes are not hosited.
- Code inside classes are executed in strict mode.
- Classes are first-class citizens.
What is getter and setter?
Getters and Setters are object accessors that will hep us to read the properties of a class like a property.
// ES6 classes
class PersonCL { constructor(firstName, middleName, lastName, year) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.year = year; }
calAge() {
return 2024 - this.year;
}
get fullName() { return this.firstName .concat(" ") .concat(this.middleName) .concat(" ") .concat(this.lastName); }
set fullName(fullNameArg) { if (true === fullNameArg.includes(" ")) { const splitted = fullNameArg.split(" ");
this.firstName = splitted[0]; this.middleName = splitted[1]; this.lastName = splitted[2]; } else { console.log("Not a fulla name"); } } }
const bharathES6 = new PersonCL("bharath", "Kumar", "amarnath", 1986); const shruthiES6 = new PersonCL(1991);
shruthiES6.fullName = “Shruthi C D”;
console. log(bharathES6.fullName);
console. log(shruthiES6.fullName);
What are static properties?
When we create a class we can define a properties and methods as static.
Neither static methods nor static properties can be called on instances of the class. Instead, they’re called on the class itself.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don’t need to be replicated across instances.
What is object .create?
Object.Create will create a new object using existing object as a prototype..
const car = { type: "fourWheeler", fuel: "diesel", };
const truck = Object.create(car);
truck.fuel = “gas”;
How do you achieve inheritance in JS?
Consider below code:
const baseCl = function (firstName, year) { this.firstName = firstName; this.year = year; };
baseCl.prototype.calcAge1 = function () {
return 2021 - this.year;
};
const derivedCL = function (firstName, year, course) { this.firstName = firstName; this.year = year; this.course = course; };
derivedCL.prototype.introduce = function () { console.log(`Hey I am ${this.firstName}`); }; const stud = new derivedCL("Bhartah", 1992, "CS");
stud.introduce();
The below code
this. firstName = firstName;
this. year = year;
is repreated. This violated DRY principle.
Hence to inherit, we use baseCl.call(this, …)
and derivedCl.prototype = Object.Create(derivedCL.Prototype)
const baseCl = function (firstName, year) { this.firstName = firstName; this.year = year; };
baseCl.prototype.calcAge1 = function () {
return 2021 - this.year;
};
const derivedCL = function (firstName, year, course) { baseCl.call(this, firstName, year); this.course = course; };
derivedCL.prototype = Object.create(baseCl.prototype);
derivedCL.prototype.introduce = function () { console.log(`Hey I am ${this.firstName}`); }; const stud = new derivedCL("Bhartah", 1992, "CS");
stud.introduce();