Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods

 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 641
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
sylvia weller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob:
Thanks for clarification, I think you are right.

victor
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic