• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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:
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic