Hello friends Can someone Explain me why the below is giving a Warning instead of Error :- package one public class Test { void met() { System.out.println("Method met of Test Class "); } public static void main(String[] args) { System.out.println("Hello Main() of Test Class "); } }
package two public class OtherTest { int met() { System.out.println("Method met of Test Class "); return 1; } public static void main(String[] args) { System.out.println("Hello Main() of OtherTest Class "); } }
In the Above, I have tried to Override Method met with different return type, but instead of giving Error, it is giving me below Warning "OtherTest.java:0: Note: Method int met() in class two.OtherTest does not override the corresponding method in class one.Test. If you are trying to override this method, you cannot do so because it is private to a different package. package two; Thanks.
Shah Chunky - Sun Certified Java2 Programmer.
Jonathan W. Brett
Ranch Hand
Joined: Mar 15, 2001
Posts: 36
posted
0
Your method "void met()" does not have an access modifer, therefore being default which means it is only visible in classes within the same package try making it public . . . .but make sure to make the overriding ethod public too because overriding methods can only be made more public
Jonathan Brett, SCJP
nitin sharma
Ranch Hand
Joined: Feb 24, 2001
Posts: 290
posted
0
sir, I would like to ask all of u, can we override a method where two classes does not extend from each other.If that is true,can anyone explain how can we override a method void method() in the above post.
nitin sharma
Ranch Hand
Joined: Feb 24, 2001
Posts: 290
posted
0
sir, I would like to ask all of u, can we override a method where two classes does not extend from each other.If that is true,can anyone explain how can we override a method void method() in the above post.
Axel Janssen
Ranch Hand
Joined: Jan 08, 2001
Posts: 2164
posted
0
Hello Sir Nitin, I think you just cannot override a method from another class if this class does not extend the other class. In Shahs example the method int met() in class two.OtherTest does NOT override the method void met() one.Test. If this would be the case the compiler won't allow it, because of the different return type. int met() is a completly new method without any overriding hirarchie. Correct me if i am wrong Axel
vkswamy venkatachalam
Greenhorn
Joined: Feb 18, 2001
Posts: 21
posted
0
Hi Chunky, To override a method in the subclass the signature(method name and parameter list) must be identical to the signature of the method in superclass. 1)the return type should be the same as overridden method 2)should not be more private than superclass method 3)any exceptions declared must be the same class as thrown by the superclass or a subclass of the exception Just changing the return type does leads to overridding. To Sharma, by overriding U mean to alter the basic behavior the method by oops.Then why are u trying to override two methods not connected in a way.I think it is obstruct to oobs. regards vkswami.
vkswami
Rick Zhong
Greenhorn
Joined: Apr 13, 2001
Posts: 10
posted
0
these two classes has no relationship whatsoEver, so there no 'overriding' such thing, besides, these two method can not pass compiler check given the two classes inherited each other for they have different return type! am i right, correct me if i am wrong, please Rick
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.