This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ref and obj ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ref and obj ?" Watch "ref and obj ?" New topic

ref and obj ?

aster nar

Joined: Jul 07, 2009
Posts: 2
what is the use of creating the following type of syntax ?

animal b = new horse();

where animal is the super class and horse is the subclass ?
what is the difference between the above syntax and the following syntax :

horse b = new horse ();

thankx anyway !!!
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Imagine you program a small mini framework. The code works. Now one of the clients decides to overwrite some behavior here and there. This is fine since you old code would simply cast it's way up to the concrete class that was assigned to the super type.

This is called polymorphism and is one of the core concepts of Java. It keeps code extensible.

Also, there is a principle that says 'Program to an Interface, not to an Implementation'

JDBCSupport - An easy to use, light-weight JDBC framework -
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

Welcome to JavaRanch. Please check your private messages for an administrative matter from JavaRanch.

About your question: It is a good design principle to program to an interface, not an implementation. Programming to an interface, instead of a specific implementation of the interface, decouples your code from that specific implementation. Look at this example:

Suppose that sometime later you discover that in this particular case, it would be better to use a LinkedList instead of an ArrayList. Now you can just change the code to this:

And you're done. The rest of your program only needs to know that data is a List of String objects - it does not need to know what implementation of interface List is used.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Simple thing you need to get here is polymorphism helps in reassigning something else later on without actually modufying the entire code.

anime A = new horse() and later on you want to use dog as an animal as you found horse is too big and its not that friendly too so you bring a dog and assign it to your animal designation animal A = new dog(). with horse h = new horse(), h will complain if you try to bring in a dog and assign it to the dog as it was not compatible to hold the dog and assume they have some enmity between them

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

for small projects where one person usually writes all the code, it is often hard to make a solid case.

but if one is selling code to dozens or hundreds of customers, you have options:

1) tell them you'll give them an animal.
2) tell them you'll give them a horse.

Let's assume you do option 2. They write thousand and thousands of lines of code, each time explicitly naming the Horse class. Now for some reason, you decide that to improve your code. You may not want to always return a horse, but maybe a mule, or a llama, or some other pack animal that may work better in some cases.

All your customers have to review all their code, looking for any place they have a Horse reference, and re-code.

Now let's assume I tell my customers I'll give them an animal. at first, the only animal I can return is a Horse, but they don't need to know that. So, they write all their code using an Animal reference. When I decide to start returning llamas sometimes, my customers don't need to change a thing.

all your customers get mad at your design, that when they re-factor their code, they do it to use MY package. all your customers are gone, and my customer base has doubled.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
It is sorta covered in the JavaRanch Style Guide.
subject: ref and obj ?
It's not a secret anymore!