• 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

final/private method......

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
final method cannot be overridden -- its true
private method cannot be overridden -- its true, however its possible to have a private method with same name as in superclass be redeclared in subclass.
now if there is somethinf like::
private final somemeth(){
// some stuff
}
what would be the o/p.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not absolutley sure but i think the method is not visible for the subclass because it is private so the subclass does not know that the method in the superclass is final or even exists.
So it should be no problem to declare a method with the same name and the same parameters in the sub class.
Simon
[ November 20, 2003: Message edited by: Simon Klaiber ]
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class base
{
private final void m1() //line 1
//final void m1() //line 2
{
System.out.println("private final in base class");
}
}
class a extends base
{
void m1()
{
System.out.println("m1 in child");
}

public static void main(String args[])
{

}
}
I agree to what Simon says. The above code did not give a compliler error. If we comment line 1 and uncomment line 2, it says "m1() cannot be overriden"
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mona
Your somemeth() doesn't have a return type So, if it's not a constructor it won't compile.
Btw, I'm not clear on what your question is.
Harwinder
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If its a constructor then it can not be final.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its entirely possible because you are not overriding the private method in the base class.
Actually specifying a method to be both private & final is a bit redundant.
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul JG:
Its entirely possible because you are not overriding the private method in the base class.
Actually specifying a method to be both private & final is a bit redundant.


hello rahul,
this was what i was intending to ask - but maybe i missed a return type and my question was a bit misunderstood. Anyway - i got the message that a method cannot b private and final at the same time. Thanks.....
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

got the message that a method cannot b private and final at the same time.


A private method can be final method but as Rahul said it is redundant.
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradeep Bhat:

A private method can be final method but as Rahul said it is redundant.


Hello Pradeep n Rahul
then what r we supposed to do if something of that sort comes in the B'Exam. Mark it true or false. pls tell me both of u....
 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
ok ok i got it u people mean that its legal. Thanks....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic