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.
In a scholarship test, 'P' achieved 28% of the total marks and fell short of passing by 45 marks. Meanwhile, 'Q' scored 44% of th...
- During a club election between members 'X' and 'Y', 10% of all votes were invalid. If 'X' got 52% of the valid votes and 'Y' secured 21,168 valid votes, de...
- The sum of two numbers, x and y, is 640. If x is reduced by 8% and y is raised by 12%, the two values become equal. Determine the value of y.
A girl found the answer for the question “subtract the sum 1/2 and 1/8 from unity and express the answer in decimals” as 1.5. The percentage...
- X is 40% less than Y and Z is 30% more than W. If W is 25% more than X, then which of the following is true?
A person spent 25% of his monthly income on food and 55% of the remaining on rent. If amount spent on rent is Rs 825, then find the amount spent on food.
The sum of the monthly incomes of ‘A’, ‘B’ and ‘C’ is Rs. 55000 which is 4 times the monthly income of ‘C’. If ‘A’ spends 60% of his...
In a given years, A sold 1850 caps while B sold 20% more number of caps than A and C sold 5% less number of caps than B. Find the ratio between the numb...
- Each year, the population of a village rises by 8%. If after 2 years the population will reach 34,992, what is the population now?
The population of a city is decreased by 12% in the first year and then increased by 25% in the second year. Find the population of the city at the end...