Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualNeophyte

Posted 28 March 2012 - 04:44 AM

Use an anonymous inner class, like this

public class Tester
{
    public static void main(String [] args)
    {
        C cObj = new C() {
            @Override public String toString() { return "I am C"; }
        };
        B [] bList = { cObj };
        for (int i = 0; i < bList.length; i++)
        {
            System.out.println( bList[i].toString() );
        }
    }
}



Edit: Fixed whitespace-issues

#1Neophyte

Posted 28 March 2012 - 04:43 AM

Use an anonymous inner class, like this

public class Tester
{
	    public static void main(String [] args)
	    {
				 C cObj = new C() {
				    @Override public String toString() { return "I am C"; }
				 };
				 B [] bList = { cObj };
				 for (int i = 0; i < bList.length; i++)
				 {
						 System.out.println( bList[i].toString() );
			    }
   }
}

PARTNERS