File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overridding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method Overridding" Watch "Method Overridding" New topic

Method Overridding

Basu Patel
Ranch Hand

Joined: May 28, 2000
Posts: 60
//Method Overridding
In the following 2 class which represent method overridding, as we all know that the overridding
method cannot be more private than the overridden method, how come the following code gives me
no compile-time error, considering void show(int b,double c) is private? In such a situation will
there be a compile-time error or is it a rule that one must follow with java programs? Please help!
class A{
int i,j;
A(int a,int b){
j=b; }
public void show(int a,int c){
System.out.println("U GOT IT! "+i+","+j);
class B extends A{
B(int a,int b){

private void show(int b,double c){,(int)c); //this calls the superclass method and we have used casting double to int


<BR>Contact Me<BR> <A HREF="" rel="nofollow"></A> <P>
michael huang
Ranch Hand

Joined: Jul 30, 2000
Posts: 63
i have not got any error at compile and run time.
so you could look your error or code again, or could you explain it clearly?
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
This is not an example of method overriding coz the signatures in the base and super class are different. Look at the type of i/p param: In class A it is: void show(int a,int c)
and in class B it is: private void show(int b,double c).
So, they are entirely different methods and hence there is no question of overriding and hence no error :-)
Please let me know if I am wrong.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi ...
Think you're right; the example shows overloading not overriding; which is why it compiles without an error.
If the parameters in the show() method in Class B are changed to (int b, int c) a compile error does occur:
attempting to assign weaker access privileges; was public
private void show(int b, int c){

[This message has been edited by Jane Griscti (edited September 06, 2000).]

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Frode, Johansen

Joined: Aug 31, 2000
Posts: 1
In your code you are not overriding, you are OVERLOADING.
To override, your overriding method must :
have the same return type
have the same name
have the same TYPE of parameters
not be more private than the metod you wish to override.
To OVERLOAD you must :
have the same name
have different TYPE of parameters.
That's all there is to it.

I agree. Here's the link:
subject: Method Overridding
It's not a secret anymore!