java - Variable Setting to All Subclasses -
is there way insantiate variable encompasses subclasses of class? i've read far must state type variable before setting equal something:
example:
exampleobject1 object = reference object
but if wanted make set variable instance or subclass of object?
yes, can that.
a variable of type t
(as long t
class/interface/enum/annotation) can hold reference instance of class t
, or instance of class extends or implements t
.
for example, works:
class myclass1 { // ... stuff goes here ... } class myclass2 extends myclass1 { // ... stuff goes here ... } class main { public static void main(string[] args) { myclass1 object = new myclass2(); } }
Comments
Post a Comment