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).
If life existed on Mars, it is most likely to be in (1)/ the form of bacteria buried deep in (2)/ the planet’s permafrost or lichens. (3)/ No erro...
In the following questions, a sentence is divided into four parts. Choose the part of the sentence which may contain a grammatical error in it. If all ...
In each of the questions, a sentence has been divided into four parts, one of which may contain an error. Identify that fragment and mark it as your an...
In each of the questions given below, read each sentence carefully to find out whether there is any grammatical error in it. The error, if any, will be...
The company reports (a) / that the demand for their computers (b) / are growing everyday (c) / No error (d)
(A) When you look at a product / (B) on one of it’s web pages, / (C) Amazon suggests other / (D) products you might like as well.
The recently(1) finding that microplastic particles(2) are found even in ‘safe’ bottled water indicates(3) the magnitude(4) of the crisis.Â
...He explained that the pillars of tribalism (1)/ that humans rely on for security would always (2)/ counter any significant culture or social change. (3)...
On its part, India has said that (A)/a technical malfunction during routine maintenance (B)/lead to the accidental firing of the missile (C).
Each of the girls(A)/ have come(B)/ with her books.(C)