Question
Examine the following Java-like code:
```java class Parent { String name = "Parent"; public void display() { System.out.println(name); } } class Child extends Parent { String name = "Child"; // No display method override } public class Main { public static void main(String[] args) { Parent p = new Child(); p.display(); } } ``` What will be the output?Solution
This demonstrates variable hiding (not overriding) and method overriding. `Parent p = new Child();` creates a `Child` object but the reference `p` is of type `Parent`. When `p.display()` is called, Java uses dynamic method dispatch (polymorphism) to call the `display()` method of the actual object type, which is `Child`. However, `Child` does not override the `display()` method. Therefore, the `display()` method from the `Parent` class is executed. Inside `Parent.display()`, `name` refers to `Parent.name`, which is "Parent". The `Child`'s `name` variable is a separate, hidden variable.
A jewel was sold passing through 3 hands and overall, its price is raised by 60%. The first seller earns 15% profit, and the second seller earns 20% pro...
- A shopkeeper bought a computer for Rs. 30,000 and a printer for Rs. 5,000. He sold the computer at a profit of 10% and the printer at a loss of 15%. Find t...
Kavita purchased a microwave oven for Rs. 24,200 and managed to sell it for Rs. 27,830. Determine her percentage profit.
Profit percentage earned on selling article 'G' is the same as the percentage loss incurred on selling article 'H'. If the cost price of article 'G' is ...
- A sofa set was sold for Rs. 63,000 at a 10% loss. If it were sold for Rs. 75,600, what would be the profit percentage?
- Arjun purchased a mobile phone and marked its price 75% higher than the cost price. He sold it after giving a 20% discount for Rs. 3600. What would be the ...
A shopkeeper buys a fan for Rs 1,440 and marks it 25% above the cost price. During a sale, he gives a discount of 10% on the marked price. What is his p...
A shopkeeper sold an article at a discount of 13%. If he had given a discount of 7% in place of 13%, then he would have earned Rs. 108 more. If the cost...
A school bag is sold for Rs.630 after giving two successive discounts of 20% and 30%. If school bag is marked up by Rs.600 above its cost price, then fi...
Roshan purchased 100 rolls of paper for Rs. 27 per roll. He spent Rs. 600 on their transportation. He also paid 50 paise per roll for packing of each ro...