• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overloading and Overriding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm studying for the SCJP exam and I have a few questions about overloading and overriding.

1) Given this example :

class aClass{
public void aMethod(int j, char x) {}
public void aMethod(int x, char k) {}
}

This isn't considered as overloading right? If so, what is it? And what happens if aMethod is called? Which method will be accessed?

2) Given this example :
class Rectangle{
public void setSize(int x) {} // line a
}

class MyRectangle extends Rectangle{
private void setSize(int y) {} // line b
}

"The accessibility of an over-riding method must not be less than that of the method it overrides". Is this a good example of the statement? If line a and line b were exchanged, that would create an illegal exception right?

I appreciate any help. Right now, I just wanna see if my understanding of the chapters are right. Thanks!
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ivy,

For your 1 st question

This is not overloading since argument types are same.But it is also not overriding since both methods are present in same class.It could have been overriding if one method is in base class and another in derived class.
Two methods having same signature in one class will not compile..

For your 2 nd question:

This will not compile because the overriding method cannot have more restrictive access modifier.Your base class method is public.So derived class method cannot have any other access modifier other than public.

Hope this helps!!

Cheers
Smitha
 
Ivy Kho
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Smitha Ballikar:
Hi Ivy,

For your 2 nd question:

This will not compile because the overriding method cannot have more restrictive access modifier.Your base class method is public.So derived class method cannot have any other access modifier other than public.

Hope this helps!!

Cheers
Smitha



You're answer pertains to when line a and b are exchanged right? What happens if I compile and run the example #2 as is?

Thank you very much!
[ August 24, 2005: Message edited by: Ivy Kho ]
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ivy,

what smitha said is for the second one as it is ...we cannot restict the access modifier when we are trying to override so it wont compile ...
and in the second case if we interchange ..then also it wont compile as we cannot override the method which is declared as private ..

hope it helps..

thanks & regards
srikanth reddy
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per 2 case when the a and b stmts are interchanged
class Rectangle{
private void setSize(int x) {} // line a
}

class MyRectangle extends Rectangle{
public void setSize(int y) {} // line b
}

My understanding to this .
1. being a private method it will be restricted to the class in which it is declared and will not be inherited by the subclass
2. Thus when you say public void setSize (int Y) you will be defining a new method for the subclass. And It will not be an example of Overriding.
3. Hence the code will compile successfully.

For more info
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html
http://www.witscale.com/scjp_studynotes/chapter6.html

Cheers
kanchan
 
Ivy Kho
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I have another question for SCJP again. I hope you can help me.

Given this example :

public SampleClass{
public SampleClass(int a, String s){} // line a
public SampleClass(int a){ // line b
this(a, "Hello World"); // line c
}
}

1) When the 2nd constructor (line b) is called, it points to the first constructor (line a) of the same object, right?

2) Given this statement -- The initialization of the parent class is performed in the overloaded constructor. Is the no-args constructor of the parent class called in the first(line a) or second constructor (line b)? My answer is the first constructor since it's the overloaded constructor but I just want to see if I understood it right.

Sorry, for all these questions.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To make you understand what overloading and overriding are , i made an example :
class MyExample{
........
//overriding
public String toString(){
.........
}
//overloading
//add functionality
public String toString(String adds){
}
}
 
Radu Mantale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you try to compile the first example you will get something like "aMethod already defined in class" . If the second aMethod would've had another number of parameters or another kind of data type then you would not get an error.
But this example works just fine:
aMethod(int x, char k)
aMethod(char k, int x)
 
Ivy Kho
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I posted another question. It's about overloading CONSTRUCTORS. Sorry, I should have clarified that. I already understood the concept of overloading and overriding methods. I just thought it would be better that I keep my questions within this thread so as not to waste space or whatever. Still, thank you for your help
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
overloading is possible when methods in the same class have different signatures- and signatures pertain to the order-spcific argument list

therefore method(int x, int y) has signature method(int, int)
method(int, char) if different from method(char, int) and so on.
Take care that return types and access modifiers are not part of method signature and are not used for overloading resolution.
reply
    Bookmark Topic Watch Topic
  • New Topic