aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Methods" Watch "Methods" New topic
Author

Methods

Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Which of the following true?
1 Transient methods cannot be overridden
2 A final class may not be subclassed
3A private method can never be overidden to be more public
4An abstract class may contain final methods
5 A private method of non inner class cannot be overridden and made public
6 Final methods cannot be overridden
I feel answer 2,4,6 are correct. But the mock exam says even 5 is correct.
can any one explain that


Arathi<br />Sun Certified Java Programmer
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
When a method is overridden , always remember that the overrideen in a subclass <u>cannot</u> be less accesseble than the method it is overriding .
hope this helps
Originally posted by Arathi Rajashekar:
Which of the following true?
1 Transient methods cannot be overridden
2 A final class may not be subclassed
3A private method can never be overidden to be more public
4An abstract class may contain final methods
5 A private method of non inner class cannot be overridden and made public
6 Final methods cannot be overridden
I feel answer 2,4,6 are correct. But the mock exam says even 5 is correct.
can any one explain that


Raghav.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Raghav
In Arathi's question the method is being overridden to be more accesible so that would be ok. The problem with the questiojn is that if it is a private method it can't be overridden it can only be hidden by the subclass.
hope that helps


Dave
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
My mistake . I think i need to read the questions very carefully .
Originally posted by Dave Vick:
Raghav
In Arathi's question the method is being overridden to be more accesible so that would be ok. The problem with the questiojn is that if it is a private method it can't be overridden it can only be hidden by the subclass.
hope that helps
Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
public class javaranchquestion
{
private void amethod()
{
System.out.println("hello");
}
public static void main(String[]arg)
{
javaranchquestion t = new javaranchquestion();
t.amethod();
}
}
class trytest1 extends trytest
{
public void amethod()
{
System.out.println("hello to raghav");
}
}

this will compile perfectly !... so the 5th option is wrong !
Originally posted by Dave Vick:
Raghav
In Arathi's question the method is being overridden to be more accesible so that would be ok. The problem with the questiojn is that if it is a private method it can't be overridden it can only be hidden by the subclass.
hope that helps

[ January 12, 2002: Message edited by: raghav mathur ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Since private methods of the base class are not visible to the child class, the child class can override that freely no problem....
I hope it helps and Raghav's example has some error, I guess instead of declaring class javaRanchQuestion, he should have declared it class tryTest
Hope this helps!!!
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205

Here is an example that LOOKS like we are overriding aMethod to be more public in foo. In reality, this is NOT the case!!!
Because aMethod is declared private in the superclass, it is NOT inherited in bar!!
I quote now from the sacred book of Gosling, section 8.2 (also known as JLS):
Members of a class that are declared private are not inherited by subclasses of that class

So, this means that the declaration of aMethod in the bar class is a brand new method,not an overriden version of the one in the superclass. Yes, they have the same name and method signature but they are not related at all!
So 2,3,4,5, and 6 are correct statements.

Rob
[ January 12, 2002: Message edited by: Rob Ross ]
[ January 12, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
sylvia weller
Greenhorn

Joined: Jan 07, 2002
Posts: 29
The statement "a private method cannot be overridden" is true. As Dave said in his post "if it is a private method it can't be overridden it can only be hidden by the subclass."
In a subclass, nothing will stop you from defining method m() with the same signature as private method m() in its superclass. But at runtime the two methods m() will not behave like overridden methods. For example:


The output of is "superclass m". If subclass.m() was overriding superclass.m() you'd get "subclass m". Change the access to public on line //1, recompile and rerun to produce overriding behavior.
Sylvia
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
That's a great example Sylvia!
In fact, I remember getting a question like this on a mock exam once and I got it wrong because I didn't understand about private members not being inherited.
We must share the knowledge! ;p
Rob
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why 3 is incorrect if 5 is correct? does that mean
private methods in inner classes can be overriden?
and how?
your further input will be appreciated.
victor
[ January 12, 2002: Message edited by: victor gu ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205

So 2,3,4,5, and 6 are correct statements.

I had previously indicated I thought 3 was correct. I can't see why that statement would be false. An inner class is still a class so any private methods of an inner class are not inherited by any subclasses of that inner class.
Rob
[ January 12, 2002: Message edited by: Rob Ross ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Rob:
Thanks for clarification, I think you are right.

victor
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Methods