• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Overriding...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Beauty is in the eye of the tiny ad.
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic