• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Static Method - Redefine/ not Overriding

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

There is rule that static method cannot be override but it can be redefine. I am confused with this.Please find below the code and let me know how it works and why.



The output for this is :
a:if not
a:a:if instance of dog
a:if not


please explain how the second output a:a:if instance of dog generates. Also let me know if i need the output as a :a:

Please help me

Thnaks in advance
Anvi Dixit
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this is the code that produces the output:

((Dog)a[x]).doStuff(); //Output : "a:"
a[x].doStuff(); //Output : "a:"
System.out.println("if instance of dog");

This is because array a has been defined as an array of animals.
for the 2nd statement

a[x].doStuff();

doStuff is a static method so at compile time this statement will be translated to:

Animal.doStuff();

so the statement 2 generates the out put "a:" , I am not sure how exactly complier behaves for first line . but i think it has something to do with the same concept.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing to remember is that when a static method is invoked using a reference, the type of the reference used determines which static method is invoked.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a typo in your source code!!

You have a lower case s in the dog's doStuff() method, so it never gets invoked!

If it was coded correctly the second line would have printed 'd:a:if instance of dog', because when you casted the animal that is a dog, to a dog, you would have invoked the dog's doStuff() method (not the animal's one!).
 
ShivKumar Rajawat
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Andy
 
Anvi Dixit
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andy,

yes you are right. there was a problem with the 's'. Lets analyse the code.

for the x=1 , a[1] = new Dog() and hence we can write
Animal a = new Dog();
a.doStuff();//which will invoke the static method of the Dog class.

output will be d:

why then its showing the output d:a:.
Please explain.


Anvi
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Animal a = new Dog();
a.doStuff();//which will invoke the static method of the Dog class.



No, since doStuff is a static method, the doStuff method of Animal will be invoked.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think Anvi you are confused about how static methods are invoked
just remember what Keith Lynn has written

"when a static method is invoked using a reference, the type of the reference used determines which static method is invoked."

your question is
for the x=1 , a[1] = new Dog() and hence we can write
Animal a = new Dog();
a.doStuff();
output will be d:

no you are wrong. output will be a:
as you see the reference is of Animal type so doStuff() method from Animal class is invoked. so a: will be printed. (for a[x].doStuff()
and when reference is converted from Animal class to Dog class using explicit cast, doStuff() method from Dog class is invoked printing d:
(for ((Dog)a[x]).doStuff()

Pankaj Shinde
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic