Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

static or dynamic ..why

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why declare a refrence variable of superclass type and try to invoke an
object of subclass type?
i.e ..class Animal{
public void eat(){
System.out.println("Eat but eat greedy Animal");
}
}
class Horse extends Animal{
public void eat(){
System.out.println(" Eat but eat Hay");
}
public static void main(String[] args){
/*Animal a =new Animal();
a.eat();*/
Animal b=new Horse();
b.eat();
/*Horse h=new Horse();
h.eat();*/
}
}
my question is when the compiler can automatically trace all the superclass and interfaces
i.e.."upcasting", why would one want to do that.? am i being too practical or is it jus another design issue?
how is static and dynamic binding done in Java?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sagar,
I would like to express my views about one of the questions you posed here,
why would one want to do that.?
Animal b = new Horse();
b.eat();
mechanism forces us to call methods only from Animal class and not from Horse class which might have been "added" to the horse class specifically and were not inherited by Animal anyhow. This is because compiler would complain if we call b.getHorseShoe() on a 'b' variable as b is of type Animal and there is no method getHorseShoe() in there. This is essentially useful to be able to switch between subclasses easily and particularly used with "Collection" classes I guess.
e.g. We should use, HashMap hm = new LinkedHashMap(); rather than declaring hm as LinkedHashMap type. If we have done it this way then we could easily switch to say HashMap hm = new CustomHashMap(); declaration in case we have extended HashMap and override some of the methods. This would ease our task, wouldn't it?
I hope I am able to point what I have observed.
For your questions about static/dynamic binding I would not be a very good 'guide'
Regards
Maulin
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This also let's you handle a bunch of different types of things in the same way. For example, the following code snippet

This means you can call any of the An�mal class's methods, and in the case that Horse, Cow or Dog has overriden that method at runtime the JVM will execute the overridden method.
 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you friends...its was informative. I understand its got more to think than to jus read n type.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sagar,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic