aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading" Watch "overloading" New topic
Author

overloading

swati cha
Ranch Hand

Joined: Jul 04, 2006
Posts: 40
can we overload the method declared as final?
srinivas.b
Ranch Hand

Joined: Oct 26, 2006
Posts: 58
Hi swati,

Yes we can overload. No errors. I wrote a program and executed.

OK
class Test1
{
public final void sri(String s)
{
System.out.println("sri");
}
}
public class Test extends Test1{

/**
* @param args
*/

public final void sri()
{
System.out.println("sri");
}

public final void sri(int i)
{
System.out.println("sri1");
}
public static void main(String[] args) {
// TODO Auto-generated method stub

Test1 t=new Test1();
t.sri("sri");

}

}
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

When you overload, essentially, you are creating a whole new method. As humans, we recognize the fact that the two methods have the same name, but the JVM compiler really doesn't care. It's just bytecode to the compiler.

Where you might be confused is with over-riding. You can't override a final method. A final method is a method that cannot be overridden. Conversely, an abstract method must be overriden, or the extending class must itself be marked as being abstract.

Kind regards,

-Cameron McKenzie
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
> You can't override a final method. A final method is a method that cannot be overridden.

but for the exam remember that if it's a private final method you can override it in the subclass! (I see they will test this)


SCJP5 - SCWCD5 - SCBCD5
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Originally posted by al nik:

..if it's a private final method you can override it in the subclass!

In this case the sub class doesnt even know about the method in the super class because its private, so its defining a new one of its own that doesnt have to follow overriding rules. For example this is legal:
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: overloading