• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Private Final Method

 
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why this code overide final method ? How it can be possible to overide final method ?
IF you want say a() method in MainClass use private modifier so hiden from B . If it is hiden so how can it print "ocpjp" by calling method a() of MainClass from method a() of B class.

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right! I just compiled that code and got "ocpjp". NetBeans does warn about it being pointless to mark a private method final....

I'm thinking that the private modifier hides the original method which means that class B can redefine it without overriding the original. Thanks for bringing this up. I know I learned something.

http://www.javaworld.com/javaworld/javaqa/2000-09/02-qa-0915-private.html

============================================================

I couldn't resist making a small change...



Exception in thread "main" java.lang.StackOverflowError
 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gaurav gupta sitm wrote:

Why this code overide final method ? How it can be possible to overide final method ?
IF you want say a() method in MainClass use private modifier so hiden from B . If it is hiden so how can it print "ocpjp" by calling method a() of MainClass from method a() of B class.



Gaurav,

A method marked either private or final can't be overridden as that method is not inherited by the subclass.

I just tried the below code and I can see the overriding hasn't happened.



 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the @Override annotation allows you to verify that a method has truly been overridden.
 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gaurav gupta sitm wrote:

Why this code overide final method ? How it can be possible to overide final method ?
IF you want say a() method in MainClass use private modifier so hiden from B . If it is hiden so how can it print "ocpjp" by calling method a() of MainClass from method a() of B class.



Is there an extra closing curly brace on line six?
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is there an extra closing curly brace on line six?

Nah, the method a() of class B has a method body that's why there is another curly brace there. Hard to see definitely lol

So the method a() in class B is not overriding the method a() in MainClass. As was stated above, you arn't overriding a method if it is private you are rewriting a NEW method with the same name. Private methods arn't inherited by the subclass. The call that you are making is not going to class B's a() method at all. Put a print statement in there and see. If you change the call to be cast to a B type you will get a stack overflow because you are continuously calling the same method over and over again.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
private methods are implicitly final . so making a private method as final is redundant!.
 
reply
    Bookmark Topic Watch Topic
  • New Topic