• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding Concept.

 
Anna s Iyer
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code and comments.

So, same method name, arguments and return type are the definition for a method to get qualified for overriding. The actual overriding happens and determined in the way we are creating the objects for the classes.
Am I understanding it correctly?. Please correct / confirm me.
Thanks,
Anna S Iyer.
 
Travis Gibson
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anna,
Overriding methods used to give me fits until I realized there is an easy way to remember which methods are referenced during overriding. It goes like this(using your example).

class orclass1 {
void ormeth1(int a){
System.out.println("orclass1 " + a);
}
}
class orclass2 extends orclass1 {
public void ormeth1(int a) {
System.out.println("orclass2 "+ a);

}
}
public class override1 {
public static void main (String args[]) {
orclass1 or1 = new orclass1();
Any method you call using the Object "or1" which is actually in "orclass1" will be used when the method is called. This is because the class which is used to create the object in this case "orclass1" tells Java which method it should use.
Therefore it calls the instance method in orclass1!
or1.ormeth1(100); //Calls the instance method of orclass1
The same as above applies here except this time orclass2 is the class used for the "or2" object.

orclass2 or2 = new orclass2();
or2.ormeth1(200); //Calls the instance method of orclass2

orclass1 or12 = new orclass2();
or12.ormeth1(300);//actual overriding happens ONLY here because it //calls the method of class orclass2 though the //class type is of class orclass1.

}
}
FYI, in the case of variables it is the reference type that determines which class a variable is pulled from:
(i.e. orclass2 or2 = new orclass2();
The word is bold tells Java which class to use.
Hope this helps,
Travis M. Gibson
(Future SCJP, Nov 19, 2000)

 
SHALINI PATEL
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI ANNA,
Lets see if I can clear it for yu.
Here its probably not overriding, but it is shadowing...... Hope I am correct. So because of dynamic method lookup, we r accessing the ormth1(300) of class2 because the object created at orclass1 or12 = new orclass2(); if of class2.............
Since yu have defined the method as void, yu cant have a return type and in overriding a method yu definetly need a return type. Thats what my sweetheart Moughal says.........
Correct me if I'm wrong.....
shalini
 
Anna s Iyer
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini,
Methods have no return type (void) are also eligible for overriding. Please refer this
Shadowing - One place shadowing comes up is when you have field names and parameter names that are the same, and you want to use the parameters to set the fields:
int a; //instance variable
public void f(int a) {
a = a;
}
This doesn't change the instance variable, because the parameter "a" shadows the field "a", that is, the parameter name blocks access via a simple name to the field name. This has the work around with the usage of "this" variable. I don't think we need to mix up shadowing with overriding and hope this helps of shadowing. Please pass your comments.
Travis,
orclass2 or2 = new orclass2();
or2.ormeth1(200);
I don't think we can say here overriding happens. It just invokes the method ormeth1 defined in the class orclass2 goes by the object or2 construction.
The reason why I am NOT saying this is overriding is that -
For example - Add modifier static to both the methds ormeth1 in both the classes and run the program. It will write 200. But keep in mind that static methods cann't be overridden. So hidden takes place.
Does it make sense?.
-----------------------------------------------------------------
orclass1 or12 = new orclass2();
or12.ormeth1(300);
ONLY here overriding happens because it supposed to display the method in the class orclass1 as the way the object or12 constructed. But it invokes the method in orclass2 and hence overriding takes place.
Appreciate your explanation and comments.
Thanks,
Anna S Iyer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic