473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Method signature and method overriding.

dmjpro
2,476 2GB
**************************
Sorry the title would be Overriding. Please experts, correct the title ;)
**************************

Method signature includes ...
1.Modifiers
2.Return type
3.Method name
4.Number of passing parameters and types of parameters
5.Throws clause

No doubt method name and number of passing parameters must be same.

Return type must be same or it may be the super class type in super class and sub class type in subclass.
Have a look ...
Expand|Select|Wrap|Line Numbers
  1. class AClass{
  2.  public Number test(){}
  3. }
  4.  
  5. class BClass extends AClass{
  6.  @Override
  7.  public Integer test(){}
  8. }
  9.  
Now come to the throws clause. It is similar as return type.
Expand|Select|Wrap|Line Numbers
  1. class AClass{
  2.  public void test()throws Exception{}
  3. }
  4.  
  5. class BClass extends AClass{
  6.  @Override
  7.  public void test()throws IoException{}
  8. }
  9.  
Now come to the types of passing parameters.
Have a look at this..
Expand|Select|Wrap|Line Numbers
  1. class AClass{
  2.  public void test(Interger i){}
  3. }
  4.  
  5. class BClass extends AClass{
  6.  @Override
  7.  public void test(Number num){}
  8. }
  9.  
Here it says that method test doesn't get overridden :(
I think here is no chance of ClassCastException.
Expand|Select|Wrap|Line Numbers
  1. AClass a = new BClass();
  2. a.test(new Integer(100)); //here is no chance of ClassCastException
  3.  
Then why it flashes an error?
Jun 2 '09 #1
9 3119
r035198x
13,262 8TB
The reason is what you said above that
"Return type must be same or it may be the super class type in super class and sub class type in subclass." You have it the other way round in that class so it won't compile.
You also left out the order of the passing parameters. The order has to match for an override to take place.

P.S Your typing is still bad. Integer != Interger. Better copy and paste the code straight from your editor.
Jun 2 '09 #2
dmjpro
2,476 2GB
@r035198x
I couldn't get your point here. If Java allowed it then what would happen?

@r035198x
Oh yeah, i left that ;)

@r035198x
Actually i simply wrote it. Sorry again!

By the way why didn't you change the title? I think you got the privilege ;)
Jun 2 '09 #3
r035198x
13,262 8TB
@dmjpro
Then it wouldn't be overriding because your point #(2) fails. The return types must match or be covariant for a true override.
Jun 2 '09 #4
dmjpro
2,476 2GB
@r035198x
You are talking about return type. But i was talking about passing parameters.
Still i am not getting the reason.
Jun 2 '09 #5
r035198x
13,262 8TB
Ok on the third snippet it's because the parameter Number can't be used in place of an Integer.
Integer i = 9;
Number n = i;works
But i =n does not work.
Jun 2 '09 #6
dmjpro
2,476 2GB
@r035198x
How i=n comes here. In the 3rd code snippet it will be n=i, and it works fine.
Jun 2 '09 #7
JosAH
11,448 Expert 8TB
@dmjpro
Overriding is not a conditional thing where the coice is made based upon the type of the parameter(s); that's a job for the overloading mechanism which the compiler can handle; your annotation is wrong there: remove it and the compiler will happiliy create an overloaded method for you.

kind regards,

Jos
Jun 2 '09 #8
dmjpro
2,476 2GB
Well.....
The return type can be co-variant. But in case of parameter(s) type what's wrong with that?
Jun 3 '09 #9
dmjpro
2,476 2GB
Well ...
Now i can figure out ...

Expand|Select|Wrap|Line Numbers
  1. class AClass{
  2.     public Number test(Integer i)throws Exception{
  3.         System.out.println("In Super Class");
  4.         return null;
  5.     }
  6. }
  7.  
  8. class BClass extends AClass{
  9.     //@Override
  10.     public Integer test(Number num)throws IOException{
  11.         System.out.println("In Derived Class");
  12.         return null;
  13.     }
  14.  
  15.     @Override
  16.     public Integer test(Integer i)throws IOException{
  17.         System.out.println("In Derived Class");
  18.         return null;
  19.     }
  20. }
  21.  
Jun 3 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christoph Groth | last post by:
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iD8DBQBAJmKufx2/njzvX5URAipnAKCUco5ZCl85xJ8YboHHJM3RBIqiGwCfX3/P...
0
by: daechulsohn | last post by:
Just thought I would contribute some knowledge for a change. After hours tracking down a problem with obfuscated code, where we were getting the exception : System.TypeLoadException: Method a...
4
by: Phill | last post by:
I read in one of Jesse Liberty's books that: "Because ToString() is a virtual method in the base class Object, it is guaranteed to be available in every derived class." (P.179 Programming C#) ...
2
by: Ed Brey | last post by:
I have a method in a C# assembly like this: public virtual void RefMeth(ref bool hi) {} And I want to override it with a VS.Net 2003 Managed C++ method like this: void RefMeth(bool __gc& hi)...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
6
by: John Cobb | last post by:
MSDN and intellisense shows the Add method of Hashtable being overridable however when I use this code: Public Class RAL Inherits Hashtable Public Overrides Sub Add(ByVal Key As String, ByVal...
13
by: Harold Howe | last post by:
Is it possible to override a specific instance of a virtual, generic method? IE: using System; class Base { public virtual void Test<U>(U u) { Console.WriteLine("Base.Test {0}, u); }
1
by: Ratnakar .N | last post by:
HELLO, Please tell me the main difference between method overriding and method overloading Thank you
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.