File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Overridding

 
Basu Patel
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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){
i=a;
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){
super(a,b);
}

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

}
 
michael huang
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks
-sampats77
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
<pre>
attempting to assign weaker access privileges; was public
private void show(int b, int c){
^
</pre>

[This message has been edited by Jane Griscti (edited September 06, 2000).]
 
Frode, Johansen
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic