Skip to content

Instantly share code, notes, and snippets.

@rishi93
Created November 17, 2015 12:03
Show Gist options
  • Save rishi93/8e8887cffc0c36adc977 to your computer and use it in GitHub Desktop.
Save rishi93/8e8887cffc0c36adc977 to your computer and use it in GitHub Desktop.
Using Parent reference to store child class objects
class Alpha
{
String getType()
{
return "alpha";
}
}
class Beta extends Alpha
{
String getType()
{
return "beta";
}
}
class Gamma extends Beta
{
String getType()
{
return "gamma";
}
}
public class Poly
{
public static void main(String[] args)
{
Alpha a1 = new Alpha();
Alpha a2 = new Beta();
Alpha a3 = new Gamma();
System.out.println(a1.getType());
System.out.println(a2.getType());
System.out.println(a3.getType());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment