Simple Inheritance Program In Java

2020. 2. 28. 09:54카테고리 없음

Examples

Simple Example Program For Multilevel Inheritance In Java

Your assumption is correct.Array created via new Y3 can hold only objects of type Y or its subclasses. It can't store instances of superclasses like X because there is risk that X will not have all methods available in Y, so even if vetx 0 = new X;compiles fine (vetx is type of X so compiler doesn't see anything wrong here), later you could try to access this object via vetY (which is type of Y) and try to invoke vety0.getPropertyAddedInY;which instance of X will not have.So to prevent such situation at runtime each array checks what type of data someone is trying to put in it and in case it is unsupported type will throw java.lang.ArrayStoreException.