File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Overriding..." Watch "Overriding..." New topic
Author

Overriding...

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
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
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
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
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
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
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
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.
 
subject: Overriding...
 
Similar Threads
main method
Query on Q49, RHE Bonus 1 - final 'default' method overriding
Java Rule Round-Up
equals and hashCode??
non rechable statment?