It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static or dynamic ..why 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 "static or dynamic ..why" Watch "static or dynamic ..why" New topic

static or dynamic ..why

Raja Sagar Panamgipalli
Ranch Hand

Joined: Aug 13, 2003
Posts: 109
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();;*/
Animal b=new Horse();;
/*Horse h=new Horse();;*/
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?

SCJP 1.4<br />SCBCD 1.3
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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();;
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'
Mika Leino
Ranch Hand

Joined: Jan 07, 2002
Posts: 56
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.

Mika Leino<br />SCJP, SCWCD
Raja Sagar Panamgipalli
Ranch Hand

Joined: Aug 13, 2003
Posts: 109
Thank you friends...its was informative. I understand its got more to think than to jus read n type.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.

SCJP Tipline, etc.
I agree. Here's the link:
subject: static or dynamic ..why
It's not a secret anymore!