This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final/private method......

 
Mona Gadkari
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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.
 
Simon Klaiber
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 ]
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • 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"
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • 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
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its a constructor then it can not be final.
 
Rahul JG
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
  • 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.....
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
hello,
ok ok i got it u people mean that its legal. Thanks....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic