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): " +
"x = " + x +
"a = " + a);
}
public void method3()
{
method1(); // Invokes the inherited method
method1(22); // Invoked the overloaded method
}
}
|
public class SomeClass
{
public int x;
public SomeClass(int a)
{
x = a;
}
public void method1( )
{
System.out.println("SomeClass.method1(). x = " + x);
}
public void method2( )
{
System.out.println("SomeClass.method2(). x = " + x);
}
}
|
- (Use a
demo program to
invoke
method3() to
show that
method1()
and
method1(22)
are
different calls)
|
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(): " +
"x = " + x );
}
public void method3()
{
method1(); // Invokes the overriding method
super.method1(); // Invoke method in superclass
}
}
|
public class SomeClass
{
public int x;
public SomeClass(int a)
{
x = a;
}
public void method1( )
{
System.out.println("SomeClass.method1(). x = " + x);
}
public void method2( )
{
System.out.println("SomeClass.method2(). x = " + x);
}
}
|
- The
inherited method
method1()
can now
only be
accessed using:
super.method1() !!
|
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) ?
|
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)
{ // 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(); // Output: ???
}
}
|
-
Overloaded method:
method1(int)
is a different method than
method1(double)
|
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) ?
|
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 !
|
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)
{ // Overriding
System.out.print("***********");
System.out.println("x = " + x );
}
public void method3()
{
method1(4.0); // method1(double)
method1(4); // method1(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: ???
}
}
|
-
Overridden method:
method1(4)
will invoke
method1(double) because
4 can be
converted to
4.0
to match:
method1(double)
|
Additional conditions
on
overriding methods
- The overriding method
must have
the same return type
as the
overridden method
in the
superclass
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(): " +
"x = " + x );
}
public void method3()
{
method1(); // Invokes the overriding method
super.method1(); // Invoke method in superclass
}
}
|
public class SomeClass
{
public int x;
public SomeClass(int a)
{
x = a;
}
public void method1( )
{
System.out.println("SomeClass.method1(). x = " + x);
}
public void method2( )
{
System.out.println("SomeClass.method2(). x = " + x);
}
}
|
- You will get a
type incompatible
error when you use
different return types
|
Additional conditions
on
overriding methods
- The overriding method
should
have
the
same accessibility modifier
as the
overridden method
in the
superclass
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(): " +
"x = " + x );
}
public void method3()
{
method1(); // Invokes the overriding method
super.method1(); // Invoke method in superclass
}
}
|
public class SomeClass
{
public int x;
public SomeClass(int a)
{
x = a;
}
public void method1( )
{
System.out.println("SomeClass.method1(). x = " + x);
}
public void method2( )
{
System.out.println("SomeClass.method2(). x = " + x);
}
}
|
-
Complicated errors can
result when you
do not use
the
same accessibility,
|
❮
❯