CS 535: Object-Oriented Programming & Design |
---|
class MethodCallInConstructor { private int value; public MethodCallInConstructor( int first, int second, int third ) { this( nonstaticAdd( first, second, third ) ); // compiler error } public MethodCallInConstructor( int first, int second ) { this( add( first, second ) ); // OK, can call static method System.out.println( "2 agruments: Value = " + value ); } public MethodCallInConstructor( int first ) { value = first; System.out.println( "1 agrument: Value = " + value ); } public int nonstaticAdd( int a, int b, int c ) { return a + b + c; } public static int add( int a, int b ) { return a + b; } }
class Parent { public Parent( ) { whichOne( ); } public void whichOne( ) { System.out.println( "In Parent" ); } } class Child extends Parent { public void whichOne( ) { System.out.println( "In Child" ); } } class Test { public static void main( String args[] ) { new Child( ); } }