0% found this document useful (0 votes)
24 views4 pages

Day 10 Assignment - OOP

Uploaded by

Baibhav Dhital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Day 10 Assignment - OOP

Uploaded by

Baibhav Dhital
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ST.

XAVIER’S COLLEGE
Maitighar, Kathmandu
(Affiliated to National Examinations Board)

Day-10 (OOP)
Submitted by:
Bikrant Lekhak
023NEB737
Grade 12 “G”

Submitted to: Signature

Department of Computer
Science (+2)
1. What are the four main principles of Object-Oriented Programming (OOP), and how do
they help in structuring code?
Ans: The four core principles of Object-Oriented Programming (OOP) include encapsulation, abstraction,
inheritance, and polymorphism.
 Encapsulation: It ensures that an object's data and the methods that operate on that data are bundled
together, shielding the internal state from direct access. By restricting direct interaction and exposing
only necessary information, encapsulation minimizes unexpected interference, enhancing code
stability.
 Abstraction: It involves hiding unnecessary details from the user, allowing them to focus on essential
aspects only. This principle reduces complexity by enabling developers to interact with high-level
interfaces without needing to understand the entire underlying implementation, making code more
intuitive and manageable.
 Inheritance: It enables new classes to take on the properties and behaviors of existing ones,
promoting code reuse. Through inheritance, developers can create more specialized classes based on
generalized ones, reducing redundancy and facilitating easier updates across related components.
 Polymorphism: It allows objects to be treated as instances of their parent class, even if their
behaviors vary. This capability enables a consistent interface for different underlying
implementations, leading to flexible code that can accommodate new functionalities without major
alterations.

2. How does JavaScript implement inheritance, and what is the difference between
prototypal inheritance in JavaScript and classical inheritance in languages like Java?
Ans: JavaScript implements inheritance through a system called prototypal inheritance, which differs
fundamentally from the classical inheritance found in languages like Java.
Here’s a comparison table highlighting the differences between prototypal inheritance in JavaScript and
classical inheritance in languages like Java:

Aspect Prototypal Inheritance (JavaScript) Classical Inheritance (Java)

Structure Objects inherit directly from other Objects are instances of classes (blueprints).
objects (prototypes).

Inheritance Flexible and dynamic; can modify Static and fixed; inheritance hierarchy
Type inheritance chains at runtime. defined at compile-time.

Implementation Uses prototype links and prototype Uses classes and subclasses with clear
chains. hierarchies.

Code Reuse Allows mixins and object composition Relies on subclassing and overriding
easily. methods.
Performance Slightly slower due to dynamic lookup Generally faster due to fixed structure and
in prototype chains. direct access in class hierarchy.

3. What is a "constructor function" in JavaScript, and how does it differ from ES6 classes?
Ans: In JavaScript, a constructor function is a special type of function used to create and initialize objects.
Constructor functions act as templates for objects and are invoked with the new keyword,
which automatically sets up an object prototype and initializes any properties defined in the function.
The difference between constructor function and ES6 classes are listed below:

Aspect Constructor Function ES6 classes

Syntax Uses function keyword; creates objects via Uses class keyword, which is more concise
new. and readable.

Prototypes Methods are added to Function.prototype Methods are automatically added to


manually. Class.prototype.

Inheritance Achieved through Object.create() or Supports extends keyword for subclassing.


prototype.

Static Defined separately on the constructor Defined with the static keyword within the
Methods function. class.

Type Technically a regular function under the True class construct; syntactic sugar for
hood. clarity.

4. Explain the concept of "encapsulation" in OOP. How can encapsulation be achieved in


JavaScript?
Encapsulation is a core concept in Object-Oriented Programming (OOP) that involves bundling an object’s
data (properties) and the methods that operate on that data into a single unit, while restricting direct access to
some of the object’s components.

In JavaScript, encapsulation can be implemented in a few key ways:

 Closures: By defining variables within a function and returning only specific methods that interact
with those variables, JavaScript can "close off" data, making it accessible only to certain functions.
This approach creates private data that cannot be accessed directly from outside the function.
 Private Fields in Classes: With ES6, JavaScript introduced classes, and more recently, private fields
within classes. By prefixing a field with #, it becomes private, allowing only the class’s methods to
interact with it. This provides a built-in way to create private variables without the need for
workarounds.
 Symbols: Symbols offer a unique identifier for object properties, making it harder for outside code to
access or overwrite data. Although not truly private, using Symbols can obscure properties from
accidental access or modification.

5. What are getters and setters in JavaScript classes, and how do they support data
abstraction?
Ans: In JavaScript classes, getters and setters are special methods that allow controlled access to an object's
properties, supporting data abstraction by defining a public interface for reading and modifying internal data

A getter method allows you to access a property’s value indirectly. Instead of exposing the property directly,
the getter retrieves the value in a controlled manner, and it can include additional logic. For example, a getter
might validate data before returning it or format it for easier use by the rest of the code.

A setter method allows you to modify a property’s value indirectly. By defining a setter, you can add checks,
such as validating inputs or transforming values, before updating the property. This prevents invalid data from
corrupting the object’s state.

You might also like