Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static or dynamic ..why

 
Raja Sagar Panamgipalli
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • 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?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
 
Mika Leino
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you friends...its was informative. I understand its got more to think than to jus read n type.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic