*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final/private method...... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final/private method......" Watch "final/private method......" New topic
Author

final/private method......

Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
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.


Mona(Varijasmom)
Simon Klaiber
Greenhorn

Joined: Nov 13, 2003
Posts: 9
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 ]
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
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"


Thanks,<br />Lakshmi.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
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
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
If its a constructor then it can not be final.
Rahul JG
Ranch Hand

Joined: Jun 05, 2002
Posts: 44
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.


Rahul
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
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.....
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Mona Gadkari
Ranch Hand

Joined: Jul 28, 2003
Posts: 64
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

Joined: Jul 28, 2003
Posts: 64
hello,
ok ok i got it u people mean that its legal. Thanks....
 
Don't get me started about those stupid light bulbs.
 
subject: final/private method......