Start learning 50% faster. Sign in now
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.
How many students have obtained 70 or more marks in at least one of the five subjects?
In which year was the profit as percentage of income is the highest?
If the number of jackets produced by company E is twice the number of jackets produced by company C. The ratio of the number jackets and shirts produced...
The savings of D is approximately how much percent of the Income of B?
Total number of Children and Women visiting Audi 3 is 750. The difference between the total number of Men and Women visiting Audi 5 is 100. What is the ...
Ratio of price of Black pen and Red pen is 9:5 for shopkeeper A. Total amount received after selling both pens is Rs.800. Price of Red pens sold by D is...
Find the average of the number of all the pens sold by D.
In a biased coin the probability of getting a head is 0.6, if we toss a coin 10times, what is the probability of getting exactly three heads?
If each color print costs Rs.10 and each Black & White print cost Rs.5 for Printer A, then find the total revenue generated by Printer A.
What is the average of total expenditure in all the years together?