File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A Question I do not like Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A Question I do not like" Watch "A Question I do not like" New topic
Author

A Question I do not like

Bob Sherry
Greenhorn

Joined: Jun 27, 2008
Posts: 20
The following question is from the book "A Programmer's Guide to Java Certification" by Khalid A. Mughas and Rolf W. Rasmussen. I have a
couple of issues with the following question:

Which statment is true?
Select the one correct answer.

(a) Private methods cannot be override in subclass.
(b) A subcanll can override any method in a subclass.
(c) An orderriding method can delcare that it throw more excpetions than
the method it is overriding.
(d) The paramter list of an overriding method must be a subset of the
parameter list of the method that it is overriding.
(e) The orerriding method can have a different return type than the
orderridden method.

First, I claim that c and e are not correct. I also claim that for any
set S, S is a subset of S. In addition, when a method is overriden its
parameter list must be the same as the method it is overriding. Therefore,
I claim that c is a correct choice. Now, please consider the following program:



class test {
private int f1()
{
return 5;
}
}

class d1 extends test {
int f1()
{
return 0;
}
};

This program compiles for me and I claim that in this case f1 is being overriden. Therefore, b is a correct answer.

The book says that the correct answer is a. I am hoping that somebody can comment.

Thanks
Bob
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 118
Bob,

> private methods a re never inherited!
> to override a method you should inherit the one that is being overriden.

in your code you are not overriding as the method f1 is not inherited ... try creating a parent class object in child class & access the private method. it won't compile.
in your code you are just declaring a new method in child class'
hope it hlps


BTW ... next time use the UBB code blocks for code & give a better title for the post so that it helps others
[ July 31, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Bob,

Something can be overridden only if it is accessible. In your example method f1 in class test is not available in class d1. Try the following as class d1


Does the above code compile ?

Here are some jls links that will help clarify
http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.8
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.2.1.3

HTH
Ashish Hareet
[ July 31, 2008: Message edited by: Ashish Hareet ]
Karl Prenton
Ranch Hand

Joined: Mar 10, 2008
Posts: 51
That book is a bit out of date. Since java 1.5, option e would also be correct - see covariant return types.
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

Since the method is not inherited, so there is no question of it being Overridden.
and one more thing, return types are not considered while a funtion is being overridden.

Consider a case,

int hello()
and
void hello()

This is not overriding, as it will give you compile time error.


Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

To add to the concept - all private methods are final....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by AnkitJi Garg:
To add to the concept - all private methods are final....


Why private methods are final?


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Mmmmmmm.... I'm not up with that. A subclass can reference a "final" method. It can't reference a "private" method because it has no access... no visibility... it can't see it.


------------------------
Bob
SCJP - 86% - June 11, 2009
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Private methods are not inherited. Therefore, they cannot be overridden, which makes them effectively (implicitly) final.

But the reverse is not true. Final methods are not implicitly private.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
John M Morrison
Greenhorn

Joined: Jul 25, 2005
Posts: 21
When you inherit, you inherit the public interface. You don't want to override private methods of the base class. Presumably, they exist to serve the public interface.

I am not a big believer in protected methods and state variables.
[ August 06, 2008: Message edited by: John M Morrison ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A Question I do not like
 
Similar Threads
true/false
Overriding method doubt
Overload methods in subclass?
How to get 100% in scjp 5.0 ?
JQ+ question