Review (background knowledge):   how Java determine which method to use

  • Java determines which method to call/use by match the method signature with the the actual parameters

    Example:

       public class Overload
       {
          public static void main(String[] args)
          {
             meth(1);          // Calls meth( int x )
    	 meth(1.0);        // Calls meth( double x )
    	 meth(1, 1);       // Calls meth( int x, int y )
          }
    
          public static void meth( int x )
          {
       	 System.out.println("Running: meth( int x ) ");
          }
        
          public static void meth( double x )
          {
       	 System.out.println("Running: meth( double x ) ");
          }
        
          public static void meth( int x, int y )
          {
       	 System.out.println("Running: meth( int x, int y ) ");
          }
       }

DEMO: 04-inheritance/11-overload/Demo2.java

Overloading vs. overriding

  • Overloading = defining different methods with the same (method) name but with different (method) signatures

    Example:

    public class NewClass extends SomeClass
    {
        NewClass(int a)
        {
            super(a);
        }
    
        // Inherits: method1()  and method2()
    
        public void method1(int a) // Overloads method1()
        {
            System.out.println("NewClass.m1(int)");
        }
    
        public void method3()
        {
            method1();   // Invokes SomeClass method1
    	method1(22); // Invoked NewClass method1(int)  
        }
    } 
    public class SomeClass
    {
        public int x;
    
        public SomeClass(int a)
        {
            x = a;
        }
    
        public void method1( )
        {
            System.out.println("SomeClass.method1()");
        }
        
        public void method2( )
        {
            System.out.println("SomeClass.method2()");
        }
    } 

DEMO: demo/04-inheritance/11-overload    (Trace with BlueJ)

Overloading vs. overriding

  • Overriding = replacing an inherited method by defining a method with the same (method) signature

    Example:

    public class NewClass extends SomeClass
    {
        NewClass(int a)
        {
            super(a);
        }
    
        // Inherits: method1()  and method2()
    
        public void method1()      // Overrides method1()
        {
            System.out.println("NewClass.m1()");
        }
    
        public void method3()
        {
            method1();       // Invokes NewClass method1   
    	super.method1(); // Invokes SomeClass method1
        }
    } 
    public class SomeClass
    {
        public int x;
    
        public SomeClass(int a)
        {
            x = a;
        }
    
        public void method1( )
        {
            System.out.println("SomeClass.method1()");
        }
        
        public void method2( )
        {
            System.out.println("SomeClass.method2()");
        }
    }  
    

DEMO: demo/04-inheritance/12-override    (Trace with BlueJ)

Quiz 1: on overloading vs. overriding

  • What is printed by the following program that invokes a.method3():

    public class NewClass extends SomeClass
    {
        // Inherits: method1(double) 
    
        public void method1(int x)    
        {
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0);
    	method1(4);
        }
    } 
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: ???
        }
    } 

  • Note:   be careful... did we overload or did we override method method1(double) ?

 

Quiz 1: on overloading vs. overriding

  • Answer:

    public class NewClass extends SomeClass
    {
        // Inherits: method1(double) 
    
        public void method1(int x)    
        {   // Overloading
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0); // method1(double)
    	method1(4);   // method1(int)
        }
    } 
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // SomeClass.m1()
        }                   **********
    } 

  • Overloaded method:   method1(int) is a different method than method1(double)

DEMO: demo/04-inheritance/13-quiz1/

Quiz 2: on overloading vs. overriding

  • What is printed by the following program that invokes a.method3():

    public class NewClass extends SomeClass
    {
        // Inherits: method1(double) 
    
        public void method1(double x)    
        {
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0);
    	method1(4);
        }
    } 
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: ???
        }
    } 

  • Note:   be careful... did we overload or did we override method method1(double) ?

 

Quiz 2: on overloading vs. overriding

  • Answer:

    public class NewClass extends SomeClass
    {
        // Inherits: method1(double) 
    
        public void method1(double x)    
        {   // Override
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0); // method1(double)
    	method1(4);   // method1(double)
        }                 //         ^^^^^ 
    }                     // int --> double
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: *********
        }                           *********
    } 

  • Note:   since there is no method with signature method1(int), Java will cast int --> double to match

DEMO: demo/04-inheritance/14-quiz2/

Java's indication for overriding

  • You can add the special override annotation @Override before an overriding method for clarity:

    public class NewClass extends SomeClass
    {
        // Inherits: method1(double) 
        @Override
        public void method1(double x)    
        {
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0);
    	method1(4);
        }
    } 
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: ???
        }
    }

  • The Java compiler will report an error if the defined method does not override any inherited methods !

DEMO: demo/04-inheritance/14-quiz2/ --- edit NewClass and re-compile

Additional conditions on overriding methods

  • The overriding method must have the same return type as the overridden method

    public class NewClass extends SomeClass
    {
        // Overrides method1(double)  
        // must have same return type 
        public void method1(double x)    
        {
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0);
    	method1(4);
        }
    } 
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: ???
        }
    }

  • You will get a type incompatible error when you use different return types

DEMO: demo/04-inheritance/14-quiz2/ --- change type void to int in NewClass method1 and re-compile

Additional conditions on overriding methods

  • The overriding method should have the same accessibility modifier as the overridden method

    public class NewClass extends SomeClass
    {
        // Overrides method1(double)   
        // should use same access spec 
        public void method1(double x)    
        {
            System.out.print("***********"); 
            System.out.println("x = " + x );
    
        }
    
        public void method3()
        {
            method1(4.0);
    	method1(4);
        }
    } 
    
    public class SomeClass
    {
        public void method1(double x)
        {
            System.out.print("SomeClass.m1(). ");
            System.out.println("x = " + x);
        }
    } 
    
    public class myProg
    {
        public static void main(String[] args)
        {
            NewClass a = new NewClass();
    
            a.method3(); // Output: ???
        }
    }

  • Complicated errors can result when you do not use the same accessibility,

DEMO: demo/04-inheritance/14-quiz2/ --- change public to private in NewClass method1 --> compile error