File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B book doubt on page 147 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 "K&B book doubt on page 147" Watch "K&B book doubt on page 147" New topic
Author

K&B book doubt on page 147

Varun Jindal
Greenhorn

Joined: Feb 03, 2007
Posts: 5
Please refer to Page 147 of the book "SCJP" by Kathy Sierra & Bert Bates. I could not find any difference between redefining and overriding a method. Please elaborate with an example, if possible.


Regards,<br /> <br />Varun Jindal<br />Software Engineer
Mamatha Preetham
Ranch Hand

Joined: Jan 23, 2007
Posts: 75
Hi Varun,

As Bert Bates said, most of the people here will not be having the copy of the book, atleast at the time we answer the questions on the ranch. So, please do post the question also...

Thanks & Regards,
mamatha.
SCJP(1.4)
Varun Jindal
Greenhorn

Joined: Feb 03, 2007
Posts: 5
Dear Mamatha,

As per your suggestion, I am including the question also. The code given below is with refernce to the explanation of the fact that "static methods can not be overridden". However, we are always free to redefine the method as done in the following code. Now I again repeat my question : What is the difference between overriding and redefining a method. From the code it seems as if we are doing overriding. Please elaborate method overriding & redefining.

Please note that the code given below works pretty fine and produces an output "aaa" when run.

class Animal
{

static void doStuff()
{
System.out.print("a");
}
}

class Dog extends Animal
{
static void doStuff()
{
System.out.print("d");// it's a redefinition
// not a override
}

public static void main(String [] args)
{
Animal [] a = {new Animal(), new Dog(), new Animal()};

for(int x = 0; x < a.length; x++)
a[x].doStuff();
}
}
sentil kumar
Ranch Hand

Joined: Oct 23, 2006
Posts: 74
if the method is redefined then it will not take part in the run time polymorphism.

please see your sample output.
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
Hi Varun,

The output of this program is aaa

I think you are agree with me with output. If yes, then observed one thing here... when a is a object of new Dog then "d" should print. But we are getting 'a' in the output. This means that polymorphism or we can say overriden is not there. This is simply redifining.

We can not override static methods, we can only redefine them.


Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Mamatha Preetham
Ranch Hand

Joined: Jan 23, 2007
Posts: 75
Hi Varun,

Overriding a method means you are giving a new meaning for the method in your derived class for an already existing method in the base class.
When you are overriding, you must strictlyfollow all rules of overriding.

If you dont follow the rules of overriding, you will no longer be overriding the method, you are defining an all new method!!!

mamatha.
SCJP(1.4)
Varun Jindal
Greenhorn

Joined: Feb 03, 2007
Posts: 5
I could not follow anybody's explanation. Anybody else... Can anyone explain the difference between overriding and redefining a method with an illustrative example(s)? Will moderators please help me out!!!
 
wood burning stoves
 
subject: K&B book doubt on page 147