Question: 2. a. Define Encapsulation with example. b. Write a java program that will print the nh Fibonacci number, where the value of n will
2. a. Define Encapsulation with example. b. Write a java program that will print the "nh" Fibonacci number, where the value of "n" will be given by the user. Example: 4 7 Hints: Input: 4 Input Process: 1 1 2 3 5 8 13 21.... Output: 3 Output 3 13 Input: 7 Process: 112358 13 21.... Output: 13
Step by Step Solution
There are 3 Steps involved in it
1 Encapsulation is a fundamental concept in objectoriented programming OOP that binds data attributes and the methods that operate on that data together into a single unit like a capsule This concept is often referred to as data hiding This is how it operates Data Hiding By encapsulating an object its internal data members attributes are not directly accessible Usually a class will define these properties as private Controlled Access You give the public methods getters and setters to interact with the data rather than gaining direct access to it These techniques can set or get the value of the data after carrying out extra logic or validation Benefits of Encapsulation Data Protection Improved Maintainability Increased Security Example public class CoffeeMachine private int waterLevel Private attribute ... View full answer
Get step-by-step solutions from verified subject matter experts
