Hi Guy's, many time while programming we encounter this kind of declaration's. As it's part of daily development, there are chances of facing this concept in interview. Below simple difference will clear the catch.
Child c = new Child() | Parent p = new Child() |
---|---|
If we know exact run time type of object then we should use this approach. | If we don't know exact run time type of object then we should use this approach (Polymorphism) |
By using child reference, we can call both parent and child class methods. | By using parent class reference, we can call only methods available in parent class and child specific methods we can't call. |
We can use child reference to hold only that particular child class objects. | We can use parent reference to hold any child class object. |
HasMap hmap = new HashMap(); ArrayList alist = new ArrayList(); | Map map = new HashMap(); List list = new ArrayList(); |
No comments:
Post a Comment