/* */ public class MiscSnippets { public static void main(String[] args) { test1(); test2(); } public static void test1() { Base b = new Base( ); Base d = new Derived( ); Derived e = new Derived( ); b.someMethod( 5 ); d.someMethod( 6 ); // d.someMethod( 7.0 ); // ILLEGAL: Attempts to call overrided someMethod(int) e.someMethod( 8.0 ); // Okay: Called Derived someMethod(double) } public static void test2() { Object o = new Object( ); Object p = new Object( ); System.out.println( o.toString( ) + " " + p.toString( ) ); } } /* Overriding/Overloading example */ class Base { protected void someMethod( int x ) { System.out.println( "In Base:someMethod(int)" ); } } class Derived extends Base { public void someMethod( int x ) { System.out.println( "In void Derived:someMethod( int )" ); } // ERROR: duplicate method declaration (changing the return type is not enough) // public int someMethod( int x ) { // System.out.println( "In int Derived:someMethod( int )" ); // } public void someMethod( double d ) { System.out.println( "In Derived:someMethod( double )" ); } } /* Student athlete example */ class Student { private String name; } interface Athlete { public String getSport( ); public boolean isAmateur( ); } class StudentAthlete extends Student implements Athlete { String mySport; boolean amateur; // … other things omitted public String getSport( ) { return mySport; } public boolean isAmateur( ) { return amateur; } }