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).
Choose the word with correct spelling.
- Select the INCORRECTLY spelt word.
- Select the correctly spelled sentence.
Den’s sandal label is a miniatures masterclass in the enduring politics of power .Â
...Directions: In each question below, four words printed in bold type are given. These are numbered (A), (B), (C) and (D). One these words printed in bol...
- Select the sentence that does NOT have a spelling error.
Select the sentence with no spelling errors:
Find the correct spelling of the word given below.
If only the routine criminal process is pursued with a universal sense of urgent , the expediency of special courts may not be needed at all.
...- In each sentence below four words have been printed in bold which are numbered (1), (2), (3) and (4) One of these words may be misspelt or inappropriate in...