java - Safety of subclass data when returned as superclass -


i'm having trouble finding specifics on happens when return subclass in method of superclass type in java. example:

    public class superclass     {         int a;     }     public class subclass extends superclass     {         int b;     }       superclass superobj;     subclass subobj;      private superclass getobject ()     {         return subobj;     }       public static void main (...)     {         superobj = getobject();     } 

what happens subobj when it's returned superclass? realise while typing example test myself, i'm still curious process when happens, , whether it's considered (if works, is) or bad practice.

i'm asking because i'm working on project in have 2 abstract base classes, , several subclasses each of them. i'm trying find out good/bad ways handle having change 1 subclass while still using convenience polymorphism adds when using abstract base classes.

edit: fixed main , class declarations, sorry that.

there oddities in code (defining method inside main?), not withstanding... method getobject not change subobj, return reference looks type superclass. "looks like" mean expose methods or members superclass. however, if take returned value , attempt downcast subclass, cast succeed , find field/methods subclass work expect without loss of information having been returned superclass.


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -