wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overridding Concept. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overridding Concept." Watch "Overridding Concept." New topic

Overridding Concept.

Anna s Iyer

Joined: Aug 21, 2000
Posts: 24
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.
Anna S Iyer.
Travis Gibson
Ranch Hand

Joined: Oct 17, 2000
Posts: 100
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)

Regards,<BR>Travis M. Gibson, SCJP<BR>Java Developer<BR>www.travismgibson.com<BR>travis@travismgibson.com
Ranch Hand

Joined: Oct 31, 2000
Posts: 41
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.....
Anna s Iyer

Joined: Aug 21, 2000
Posts: 24
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.
orclass2 or2 = new orclass2();
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();
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.
Anna S Iyer.
I agree. Here's the link: http://aspose.com/file-tools
subject: Overridding Concept.