• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading

 
swati cha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we overload the method declared as final?
 
srinivas.b
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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)
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic