# OOPS Asked Questions

1:What is meant by the term OOPs?

ans:- OOPs refers to Object-Oriented Programming. It is the programming paradigm that is defined using objects. Objects can be considered as real-world instances of entities like class, that have some characteristics and behaviors. OOPs helps users to understand the software easily. With OOPs, the readability, understandability, and maintainability of the code increase mutifold. Even very big software can be easily written and managed easily using OOPs. this is used oop: c++ , java , paython , javascript this is not used oops: C,COBOL (Common Business-Oriented Language), Pascal, Fortran, assembler. OOPs or Object Oriented Programming mainly comprises of the below four features, and make sure you don't miss any of these: Inheritance Encapsulation Polymorphism Data Abstraction OOPs is very helpful in solving very complex level of problems. Highly complex programs can be created, handled, and maintained easily using object-oriented programming. OOPs, promote code reuse, thereby reducing redundancy. OOPs also helps to hide the unnecessary details with the help of Data Abstraction

2.What is a class? A class can be understood as a template or a blueprint, which contains some values, known as member data or member, and some set of rules, known as behaviors or functions. So when an object is created, it automatically takes the data and functions that are defined in the class. Therefore the class is basically a template or blueprint for objects. Also one can create as many objects as they want based on a class. For example, first, a car’s template is created. Then multiple units of car are created based on that template

// Define a class called "Person" class Person { // Constructor to initialize object properties constructor(name, age) { [this.name](http://this.name) = name; this.age = age; }

// Method to introduce the person introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); } }

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Call the introduce method for each person person1.introduce(); person2.introduce();

3.What is an object? An object refers to the instance of the class, which contains the instance of the members and behaviors defined in the class template. In the real world, an object is an actual entity to which a user interacts, whereas class is just the blueprint for that object. So the objects consume space and have some characteristic behavior. For example, a specific car.

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Call the introduce method for each person person1.introduce(); person2.introduce();

4.What is Abstraction? Abstraction is the method of hiding unnecessary details from the necessary ones. It is one of the main features of OOPs. For example, consider a car. You only need to know how to run a car, and not how the wires are connected inside it. This is obtained using Abstraction.

example:- class Person { constructor(name, age) { this.\_name = name; // Private property this.\_age = age; // Private property }

// Getter method to access name (abstraction) getName() { return this.\_name; }

// Setter method to update name (abstraction) setName(name) { this.\_name = name; }

// Getter method to access age (abstraction) getAge() { return this.\_age; }

// Setter method to update age (abstraction) setAge(age) { if (age &gt; 0) { this.\_age = age; } else { console.error("Age must be a positive number."); } }

introduce() { console.log(`Hi, my name is ${this._name} and I'm ${this._age} years old.`); } }

const person1 = new Person("Alice", 30); console.log(person1.getName()); // Accessing name using getter method console.log(person1.getAge()); // Accessing age using getter method

person1.setName("Carol"); // Updating name using setter method person1.setAge(35); // Updating age using setter method

person1.introduce();

5.What is meant by Inheritance? The term “inheritance” means “receiving some quality or behavior from a parent to an offspring.” In object-oriented programming, inheritance is the mechanism by which an object or class (referred to as a child) is created using the definition of another object or class (referred to as a parent). Inheritance not only helps to keep the implementation simpler but also helps to facilitate code reuse.

example: // Define a class called "Person" class Person { // Constructor to initialize object properties constructor(name, age) { [this.name](http://this.name) = name; this.age = age; }

// Method to introduce the person introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); } }

// Define a subclass called "Student" inheriting from "Person" class Student extends Person { // Constructor to initialize object properties constructor(name, age, grade) { super(name, age); // Call the constructor of the parent class this.grade = grade; }

// Method to introduce the student introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`}, I'm ${this.age} years old, and I'm in grade ${this.grade}.`); } }

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Create instances of the Student class const student1 = new Student("Charlie", 15, 10); const student2 = new Student("Diana", 17, 12);

// Call the introduce method for each person and student person1.introduce(); person2.introduce();

student1.introduce(); student2.introduce();

6.What is encapsulation? one can visualize Encapsulation as the method of putting everything that is required to do the job, inside a capsule and presenting that capsule to the user. What it means is that by Encapsulation, all the necessary data and methods are bind together and all the unnecessary details are hidden to the normal user. So Encapsulation is the process of binding data members and methods of a program together to do a specific job, without revealing unnecessary details. Encapsulation can also be defined in two different ways:

1. Data hiding: Encapsulation is the process of hiding unwanted information, such as restricting access to any member of an object.
    
2. Data binding: Encapsulation is the process of binding the data members and the methods together as a whole, as a class. example:- // Define a class called "Person" class Person { // Constructor to initialize object properties constructor(name, age) { // Private properties let *name = name; let* age = age;
    
    // Getter method to access name (encapsulation) this.getName = function() { return \_name; };
    
    // Getter method to access age (encapsulation) this.getAge = function() { return \_age; }; }
    

// Method to introduce the person introduce() { console.log(`Hi, my name is ${this.getName()} and I'm ${this.getAge()} years old.`); } }

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Call the introduce method for each person person1.introduce(); person2.introduce();

7. What is Polymorphism? Polymorphism is one of the most important concepts in OOP. It describes the ability of something to have or to be displayed in more than one form. Polymorphism is composed of two words - “poly” which means “many”, and “morph” which means “shapes”. Therefore Polymorphism refers to something that has many shapes. In OOPs, Polymorphism refers to the process by which some code, data, method, or object behaves differently under different circumstances or contexts. Compile-time polymorphism and Run time polymorphism are the two types of polymorphisms in OOPs languages.
    

example: // Define a class called "Person" class Person { // Constructor to initialize object properties constructor(name, age) { [this.name](http://this.name) = name; this.age = age; }

// Method to introduce the person introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); } }

// Define a subclass called "Student" inheriting from "Person" class Student extends Person { // Constructor to initialize object properties including grade constructor(name, age, grade) { super(name, age); // Call the constructor of the parent class this.grade = grade; }

// Override the introduce method to include grade information introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`}, I'm ${this.age} years old, and I'm in grade ${this.grade}.`); } }

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Create instances of the Student class const student1 = new Student("Charlie", 15, 10); const student2 = new Student("Diana", 17, 12);

// Call the introduce method for each person and student person1.introduce(); // Output: Hi, my name is Alice and I'm 30 years old. person2.introduce(); // Output: Hi, my name is Bob and I'm 25 years old.

student1.introduce(); // Output: Hi, my name is Charlie, I'm 15 years old, and I'm in grade 10. student2.introduce(); // Output: Hi, my name is Diana, I'm 17 years old, and I'm in grade 12.

8.Method overloading method overriding Definition: Method overloading refers to defining multiple methods with the same name but different parameters within the same class, while method overriding involves creating a method in the child class that has the same name, parameters, and return type as a method in the parent class. //overloading class Person { // Constructor to initialize object properties constructor(name, age) { [this.name](http://this.name) = name; this.age = age; }

// Method to introduce the person without any additional information introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); }

// Method to introduce the person with an additional message introduce(message) { console.log(`${message} My name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); } }

// Create an instance of the Person class const person = new Person("Alice", 30);

// Call the introduce method with different parameters person.introduce(); // Output: Hi, my name is Alice and I'm 30 years old. person.introduce("Nice to meet you! "); // Output: Nice to meet you! My name is Alice and I'm 30 years old.

//overriding Method overriding involves creating a method in the child class that has the same name, parameters, and return type as a method in the parent class. // Define a subclass called "Student" inheriting from "Person" class Student extends Person { // Constructor to initialize object properties including grade constructor(name, age, grade) { super(name, age); // Call the constructor of the parent class this.grade = grade; }

// Method to introduce the student, overriding the introduce method in the Person class introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`}, I'm ${this.age} years old, and I'm in grade ${this.grade}.`); } }

// Create an instance of the Student class const student = new Student("Charlie", 15, 10);

// Call the introduce method for the student student.introduce(); // Output: Hi, my name is Charlie, I'm 15 years old, and I'm in grade 10.

9.Pass by value pass by reference Pass by Value: In pass by value, a copy of the actual value of the argument is passed to the function. This means that changes made to the parameter inside the function do not affect the original value of the argument.

Here's an example in JavaScript:

javascript

function increment(num) { num++; // Increment the copy of 'num' }

let x = 5; increment(x); console.log(x); // Output: 5 (unchanged)

Pass by Reference: In pass by reference, a reference (memory address) to the original value of the argument is passed to the function. This allows the function to modify the original value of the argument directly.

Here's an example in JavaScript:

javascript

function changeName(person) { [person.name](http://person.name) = "John"; // Modify the original object }

let personObj = { name: "Alice" }; changeName(personObj); console.log([personObj.name](http://personObj.name)); // Output: John (modified)

10. Interface An interface refers to a special type of class, which contains methods, but not their definition. Only the declaration of methods is allowed inside an interface. To use an interface, you cannot create objects. Instead, you need to implement that interface and define the methods for their implementation.
    

// Define an interface-like object with method signatures const PersonInterface = { introduce() {} };

// Define a class that implements the PersonInterface class Person { // Constructor to initialize object properties constructor(name, age) { [this.name](http://this.name) = name; this.age = age; }

// Method to introduce the person introduce() { console.log(`Hi, my name is ${`[`this.name`](http://this.name)`} and I'm ${this.age} years old.`); } }

// Implement the PersonInterface in the Person class Object.assign(Person.prototype, PersonInterface);

// Create instances of the Person class const person1 = new Person("Alice", 30); const person2 = new Person("Bob", 25);

// Call the introduce method for each person person1.introduce(); person2.introduce();

11.Constructor Constructors are special methods whose name is the same as the class name. The constructors serve the special purpose of initializing the objects. For example, suppose there is a class with the name “MyClass”, then when you instantiate this class, you pass the syntax: MyClass myClassObject = new MyClass();

Now here, the method called after “new” keyword - MyClass(), is the constructor of this class. This will help to instantiate the member data and methods and assign them to the object myClassObject.

Default constructor: The default constructor is the constructor which doesn’t take any argument. It has no parameters.

class ABC { int x;

ABC() { x = 0; } }

Parameterized constructor: The constructors that take some arguments are known as parameterized constructors.

class ABC { int x;

ABC(int y) { x = y; } }

Copy constructor: A copy constructor is a member function that initializes an object using another object of the same class.

class ABC { int x;

ABC(int y) { x = y; } // Copy constructor ABC(ABC abc) { x = abc.x; }

Abstract method and class An abstract class is a special class containing abstract methods. The significance of abstract class is that the abstract methods inside it are not implemented and only declared. So as a result, when a subclass inherits the abstract class and needs to use its abstract methods, they need to define and implement them.

this key word the this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

super keyword The 'super' keyword allows referencing the parent class or superclass of a subclass in Java. It is often employed to access members (fields or methods) of the superclass that have been overridden in the subclass. You can call the superclass's method from within the subclass using super.

access modifier Access modifiers are keywords which define the accessibility of a class and its members. Access modifiers are used in Java to control the visibility (accessibility) of classes, interfaces, variables, methods, constructors, data members, and setter methods. public, private, protected and default public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is a subclass?

The subclass is a part of Inheritance. The subclass is an entity, which inherits from another class. It is also known as the child class.

Define a superclass?

Superclass is also a part of Inheritance. The superclass is an entity, which allows subclasses or child classes to inherit from itself.

What is an exception?

An exception can be considered as a special event, which is raised during the execution of a program at runtime, that brings the execution to a halt. The reason for the exception is mainly due to a posit+ion in the program, where the user wants to do something for which the program is not specified, like undesirable input.

What is meant by exception handling?

No one wants its software to fail or crash. Exceptions are the major reason for software failure. The exceptions can be handled in the program beforehand and prevent the execution from stopping. This is known as exception handling. So exception handling is the mechanism for identifying the undesirable states that the program can reach and specifying the desirable outcomes of such states. Try-catch is the most common method used for handling exceptions in the program.
