Question
Consider the following Java code: public
class Product { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setPrice(double newPrice) { if (newPrice > 0) { this.price = newPrice; } else { System.out.println("Price cannot be negative."); } } } This code demonstrates encapsulation by:Solution
The key to encapsulation here is that name and price are private, meaning they cannot be directly accessed from outside the Product class. Instead, access is provided through public methods like getName() (a getter) and setPrice() (a setter), which includes validation logic (newPrice > 0).
How many four letters meaningful English words can be formed with the second, fourth, sixth and ninth letters of the word CERTIFICATE (when counted from...
- Which of the following is used to transfer data between computers over the Internet securely?
मनीष 3 मार्च 1980 को पैदा हुआ था। संजीव , मनीष से 4 दिन पहले पैदा...
1 – [(5 – {2 + (–5 + 6 – 2)2}] , बराबर है -
Select the letter-cluster from among the given option that can replace the question mark (?) in the following series.
GPSU, KLVR, OHYO, SDBL, ?
- ‘P’, ‘Q’ and ‘R’ divide a certain amount among themselves. The average of their shares is Rs. 4860. Share of ‘P’ is 12(1/2)% more than shar...
A(n) ........................ is a set of programs designed to manage the resources of a computer, including starting the computer, managing programs, m...
In expression F % G $ K # H $ I @ L @ J, then, how is I related to mother of K?
- A 220 metres long train, travelling at 60 km/h crosses a platform in 30 seconds. What is the length of the platform?
Which of the following is NOT a function of BIOS?