Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

is overloading possible in subclass

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the following lines of code.
class Overridden {
int i = 5;
public void method() {
System.out.println(i);
}
}
class override extends Overridden {
int i=10;
public int method(int i) {
System.out.println("i in override : " + i);
i = super.i;
System.out.println("i in overridden : " + i);
return i;
}
public static void main(String s[]) {
override o = new override();
o.method();
o.method(10);
}
}
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi bajaj ...
The code you've given is an example of 'overloading'. A method overload happens when you have two methods with the same name but different argument lists: int method(int i) in 'override' overloads void method() in 'Overridden'.
To actually 'override' a method you use the same name and argument list but change the code.
In class 'override'

Would actually 'override' method() in class 'Overridden'.
Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic