OOPS Flashcards

1
Q

What is OOPS?

A

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.

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

What are 4 principles of OOPS?

A

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.

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

What is prototypal inheritance?

A

In prototypal inheritance, all objects are linked to a prototype and they share the method which is defined in the prototype.

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

What is a constructor function?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you create a method using constructor function?

A

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());

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

is bharath.__proto___ == consrtctor.Prototype?

A

yes, it is

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

Can we create a property in the prototye?

A

Yes, we can create.

Person.prototype.species = “yeno”;

But this share the property to all instance..

We can use
isPropertyof

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

What is Prototype Inheritance and chain?

A

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.

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

Which is the base of all prototype?

A

object.prototype

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

Can we add a method tlike array.prototype.unique = function

A

Yes, but not recommended

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

What is ES6 classes?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is getter and setter?

A

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);

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

What are static properties?

A

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.

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

What is object .create?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you achieve inheritance in JS?

A

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();

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

is derivedcl.Protoype = BaseCl.prototype

A

No.

17
Q

How to achieve inheritance and polymorphism using ES6 classes?

A

using extends and super keyword.

class baseCL{
constructor(firstName, year){
this.firstName = firstName;
this.year = year;
}

calAge(){
return 2021 - this.year;
}
}

class derivedCl extends baseCl{
constructor(firstName, year, course){
super(firstName, year);
this.course = course;
}
calAge(){
CL('I am from the derived class');
return 2022 - this.year;
}
}
18
Q

Can we have multiple-level inheritance?

A

Yes. It creates the prototype chain.

19
Q

How do we achieve encapsulation in JS?

A

We can have

public fields
private fields
public methods
private methods

to define a field as private, we have to use #, #name. But, we can also have it _. _ is not truly private but it is a convention. Hence, we call also call it as protected.

We can also have static version of above fields.

20
Q

How can we achieve chaining of methods?

A

return this from the method