• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B book doubt on page 147

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the method is redefined then it will not take part in the run time polymorphism.

please see your sample output.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mamatha Preetham
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic