Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Question I do not like

 
Bob Sherry
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to the concept - all private methods are final....
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by AnkitJi Garg:
To add to the concept - all private methods are final....


Why private methods are final?
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John M Morrison
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic