• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[B] a private final method can be overridden!!!! [/B]

 
souji nutak
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we override a final private method???


class Base
{
final private void amethod(int iBase)
{
System.out.println("Base.amethod");
}
}



class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
Base b = new Over();
int iBase=0;
o.amethod(iBase);
}

public void amethod(int iOver)
{
System.out.println("Over.amethod");
}
}
 
sarika kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since amethod is a private method of Base class, so no subclass can inherit it.Hence, any subclass of Base can have its own amethod.
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private method is not inherited... so it doesn't matter wheter it's final. maybe you should chnage your private method to public ... to get more aproriate error about overriding final method
 
sarika kumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by souji nutak:
Can we override a final private method???


class Base
{
final private void amethod(int iBase)
{
System.out.println("Base.amethod");
}
}



class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
Base b = new Over();
int iBase=0;
o.amethod(iBase);
}

public void amethod(int iOver)
{
System.out.println("Over.amethod");
}
}


I feel I didnt explain properly. So writing again.
Any private method cannot be inherited. So, there is no question of overridding. Second thing is, since a subclass cannot inherit private method i.e amethod, it can always redefine amethod.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

private methods wont be visible out side the class definition where it is decalred. So in case of private method, there is no concept of method overriding.
when you called method on a reference , it will chk the method in the object which is assigned to the reference. so that method will be called.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

private methods wont be visible out side the class definition where it is decalred. So in case of private method, there is no concept of method overriding.
when you called method on a reference , it will chk the method in the object which is assigned to the reference. so that method will be called.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

private methods wont be visible out side the class definition where it is decalred. So in case of private method, there is no concept of method overriding.
when you called method on a reference , it will chk the method in the object which is assigned to the reference. so that method will be called.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , i refreshed the page. So it posted two more times
 
j bande
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends i am new to this thread. i am planning to giv my exam on april 12. but i don't have links for mock exams or any dumps. can anyone help me in this regard...............
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI bande,
You can get some best mock exams here
Free Mock Exams :http://www.javabeat.net/javabeat/scjp2/index.php





regards
SADASIVAKUMAR UTTI
SCJP1.4
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we override a final private method???


class Base
{
final private void amethod(int iBase)
{
System.out.println("Base.amethod");
}
}



class Over extends Base
{
public static void main(String argv[])
{
Over o = new Over();
Base b = new Over();
int iBase=0;
o.amethod(iBase);
}

public void amethod(int iOver)
{
System.out.println("Over.amethod");
}
}


private methods are not inherited by subclasses and, therefore, cannot be overridden (i.e., all private methods are implicitly final).


regards
SADASIVAKUMAR UTTI
SCJP1.4
 
j bande
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sadasiva kumar, thanks for your kind reply.
[ March 19, 2007: Message edited by: j bande ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic