• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

overloading & overriden method

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class base {
public void method(int i){}
}
public class sub extends base {
//
}
choose all the method which can be added at // from the list
1. int method(int i)
2. public void method(String a)
3. public void anothermethod(int i)
4.void abstract othermethod(int i)
5.void method(int i)
my ans:2
ans given are:2,3
can anybody explain how option 3 is correct...in that the method name is different..
help
thanx
sherin
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The third method is neither overloaded nor overridden. It is just another method which can be safely defined in that class without causing compile time/runtime error.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sheri,

3 is correct bcoz....???
No Not bcoz of overriding or overloading..... it is just a different method with different name.
U can add any variable declaration, methods, any initializers, any constructor etc at //.
1. int method(int i) // Same method signature as the Base
2. public void method(String a) // Correct
3. public void anothermethod(int i) // Correct
4. void abstract othermethod(int i) // Wrong
5. void method(int i) // Same signature

But from the answers given the best choices are 2 & 3.
Hope this helps,
Aruna


 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since sub (class) is inheriting from base (class), base can have its own additional features (variables/methods) than the base (class). here anothermethod() is a new feature added to sub (class) and hence 2,3 are correct answers.
hope its clear.
 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx to aru & ramesh
 
sheri
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi help me with this:
Given a class One in which a public void method( ){ } is defined, what are
the valid method definitions that can be included in a sub class of A without
changing the class definitions.
Class One{
Public void method( ){ }
}
Class Two extends One{
Public void meth1( ){ }
//X
}
1:void othermethod(){}
2:int method(){}
3:int method(String s){}
4:void method(){ }
5 ublic int othermethod (String s ){}
my ans are:1,3,5
pls correct me if i am wrong
thanx
sherin
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sherin,
Yes u r right, 1,3,5 are the correct answers.
Aruna
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic