• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

overloading

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we overload the method declared as final?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");

}

}
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic