This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
//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 }
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
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).]
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.