In Java:
Parent obj = new Child();
I created an object of type Parent. I assume that I can only invoke the methods defined in Parent class. Thus, I can’t invoke the “additional” methods defined in Child or access variables defined in Child.
So I think it’s a waste of memory since I have newed a instance of Child which takes additional memory space than Parent. To this point I think it does not make sense.
Can anyone please give some reason/example where this make sense?
Yes, it does make sense.
You would do this if you want to use a Child, but constrain your interaction with it to
Parentmethods.This idiom is particularly common with Java collections: