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).
What is the chemical formula of common salt?
The method of protecting iron from rusting, by coating a thin layer of Zinc is called -
Which among the following nutrients is essential for the normal functioning of the thyroid gland?
Tungsten is used for the construction of filament in an electric bulb because of itsÂ
A battery of emf 12 V and internal resistance 4 ohm is connected to a resistor. If the current in the circuit is 0.6 A, what is the resistance of the re...
A cylindrical metal wire is reformed so that its length decreases by 20% with no change of volume. Its
resistance decreases by:
Which of the following methods is suitable for preventing an iron frying pan from rusting?
What is the atomic number of carbon?
The Periodic Law stating that ‘the properties of elements are the periodic function of their atomic masses’ was given by
A mass m is suspended from a spring of spring constant k and oscillates with amplitude A. The maximum velocity during oscillation is: