• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading and Overriding

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this code compile ?


public class class1 {
public static void main(String args[]) {
System.out.println("main");
}

public void test(String name) {
System.out.println("test");
}

public void test(StringBuffer name){}
public void test(String name){}
}


[ November 26, 2005: Message edited by: Arnb Sen ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't. You can't have two methods declarations with the same signature. You can however, as the class does, overload 'test' by methods with different signatures.
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf.


These are set of codes which I wrote. Some are overloading, some are overriding and some are simlpe hiding.

I have clasified them. Are they correct ?

FIRST CODE



SECOND CODE



THIRD CODE



FOURTH CODE



FIFTH CODE



SIXTH CODE



Categories : Overloading, Overriding, Hiding

1. FIRST CODE - Creating an instance of Base class in the subclass. Which cateory does this fall in ?
2. SECOND CODE - Is this Overriding ? Code does not compile. Method "methodInBase" has different return type in subclass as compared to that of the Base class.
3. THIRD CODE -
4. FOURTH CODE - This is Overriding
5. FIFTH CODE - This is Overriding as well
6. SIXTH CODE - This will not compile because sublass variable cannot reference an instance of Base class.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Arnb,

1. FIRST CODE - overloading
2. SECOND CODE - This is not a valid overriding or overloading. That is the reason for compilation error.
3. THIRD CODE - same as above
4. FOURTH CODE - This is Overriding.
5. FIFTH CODE - This is Overriding.
6. SIXTH CODE - This is also Overriding. But, compilation error because base class object can't be assigned to a subclass reference(class2 c2 = new class2Base() .
Hope this helps.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arnb Sen:
Why does this code compile ?



[ November 26, 2005: Message edited by: Arnb Sen ]



What made you say it compiles??
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amrutha,

Thanks a lot. Helps

Hi Stuart,

The code was slightly different when I was compiling in textpad and then to make it neater while posting, I modified some of the names.

When Ulf pointed out that it is not compiling, I cross-checked the code that I had and the one I posted and realised that while changing the names, I made an error.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah ok
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic