-1
class Parent {
    int m;
}

class child extends Parent {
    public static void main(String args[]) {
        Parent x = new Child(); 
    }
}

What is the meaning when we say x is of type Parent? Why do we make use of such referencing while we can use Child x = new Child()?

Ibrokhim Kholmatov
  • 1,079
  • 2
  • 13
  • 16

2 Answers2

0

A List<Shape> can contain rectangles, circles, if those are subtypes of Shape. That's an example for why it's good.

Törpetestű
  • 192
  • 10
0

Java resolve the class of an object dynamically. It means that the actual type of an object is resolved over the indicated type.

So, in your example, if Child contains an overloaded method from Parent, this method will be called first before the one inheritted from Parent.

Marcadia
  • 538
  • 1
  • 4
  • 9