473,790 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Method signature and method overriding.

dmjpro
2,476 Top Contributor
*************** ***********
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 ClassCastExcept ion.
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 3138
r035198x
13,262 MVP
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 Top Contributor
@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 MVP
@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 Top Contributor
@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 MVP
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 Top Contributor
@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 Recognized Expert MVP
@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 Top Contributor
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 Top Contributor
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
1934
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 tL1uWVMKntHThZ550BS32aw= =4Ijl -----END PGP SIGNATURE-----
0
1168
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 on type b from assembly is overriding a method impl. We traced it to a class that implements multiple interfaces, two of which defined the same method, with same name and same signature. That is,
4
1392
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#) My question is, wouldn't the ToString() method be guaranteed to be available to derived classes even if it had not been declared as virtual?
2
1164
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) However, when I run, the override never happens. Moreover, if I explicitly put the virtual keyword on the C++ declaration for RefMeth, and look at the
3
1557
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
5413
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 Value As Object) End Sub
13
16905
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
4973
by: Ratnakar .N | last post by:
HELLO, Please tell me the main difference between method overriding and method overloading Thank you
6
27815
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 was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.