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 cabbage, cauliflower and broccoli, hollow stems with darkened interiors are most often caused by:
‘Jack fruit’ belongs to family
Edible part of cauliflower is
Which of the following is popularly known as queen of vegetables?
Nobilization of Sugarcane in India was done by
Vegetative propagation is an asexual method of plant reproduction and different plants are propagated by using different vegetative propagules. Coconut ...
Curd is the economical part of which of the following vegetable?
Cauliflorous bearing habit i.e fruits are borne on trunk and branches, is found in which horticultural crop?
Black leg is a disease of which vegetable?