Question
Which of the following best demonstrates encapsulation
in Object-Oriented Programming (OOP)?Solution
Encapsulation is one of the core principles of Object-Oriented Programming. It is the process of bundling the data (variables) and methods that operate on that data into a single unit, often a class, while restricting direct access to some of the object's components. This is achieved by declaring variables as private and providing public getter and setter methods to access and modify them. Why Option 3 is Correct: Encapsulation ensures that the internal representation of an object is hidden from the outside. The object exposes only specific methods to manipulate its state, providing controlled access and maintaining integrity. For example: class Account {   private double balance; // private variable ensures data hiding   public void setBalance(double amount) {     if (amount > 0) {       balance = amount;     } else {       System.out.println("Invalid amount");     } }   public double getBalance() {     return balance;  }} Here, the balance variable is hidden, and access is managed via getBalance and setBalance methods, ensuring that improper data cannot corrupt the object. Encapsulation improves code readability, security, and modularity. It adheres to the principle of "data hiding," reducing the chance of errors caused by unintended modifications. ________________________________________ Why Other Options Are Incorrect: 1. Using inheritance to extend a base class with additional functionality: This describes inheritance, not encapsulation. While inheritance promotes code reuse, it does not hide internal data or enforce controlled access to it. Encapsulation focuses on protecting the internal state of an object. 2. Declaring all member variables as public for easy access: Public member variables violate the concept of encapsulation. It allows direct access and modification of an object's state, defeating the purpose of data hiding and controlled access, potentially leading to integrity issues. 3. Overloading a method to perform multiple functionalities: Method overloading is a feature of polymorphism, where multiple methods in the same class have the same name but different parameter lists. While useful, it does not relate to encapsulation, which is about data protection and controlled access. 4. Using static variables to share data across instances: Static variables are shared among all instances of a class and can break encapsulation if not used carefully. Encapsulation emphasizes protecting the internal state of an individual object, not sharing data globally.
Sink
(i) She moved to the sink and turned on the water.
(ii) I bet you could really sink your teeth into that pork roast.
(iii) He ...
In the following question, a word has been used in sentences in THREE different ways. Choose the option corresponding to the sentences in which the usa...
Select the most appropriate word for the given group of words.
land covered with grass and other low plants suitable for grazing animals, especia...
Select the most appropriate synonym of the given word.
ADVERSARY
Hatch
(i) The chief engineer of the submarine went down through the hatch.
(ii) Only the embryos of Leptodora are known to hatch out in th...
In the question below, three sentences are given, each of which has two words highlighted in bold. One of the words in each sentence is inappropriate i...
Directions: In each of the following questions, a word has been used in sentences in THREE different ways. Choose the option corresponding to the sente...
In the question below, a statement has three words highlighted in bold, which may or may not be correctly used. You are required to choose fro...
In the following questions, a word has been given and there are three ways in which the word has been used, in similar or different forms. You need to ...
Directions: In each question below, word is used in four different ways. Choose the option in which the usage of the word is INCORRECT or INAPPROPRIATE....