File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Only one question in refference and actual object type? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Only one question in refference and actual object type?" Watch "Only one question in refference and actual object type?" New topic

Only one question in refference and actual object type?

Shanmugam nagaraj
Ranch Hand

Joined: May 07, 2007
Posts: 76
why we want the refference type to be different from the actual object type?

For eg:
foo.Person person=new foo.Employee();

My understand is like:
If the "Person" is like abstract class,interface.
"Employee" is class.

We cannot create a actual object type for the (interface, abstract class), So we creating a refference type and the actual type is different.

Is my understanding about this is correct if not "what is the need for using this", if yes "is there any usage apart from this "

please clarify


Shanmugam N

Thanks <br />Shanmugam N
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

i can make a collection (an arraylist, for example) of Person objects - the interface.

I can then iterate over the collection, calling the printBirthday() method for them, since the printBirthday method is required by the Person interface.

Now, it doesn't matter if i stick in an Employee, a Manager, a Bum, or an Infant, since they all implement Person, and my code works.

Two years from now, someone wants to write a CollegeStudent class. As long as they implement my Person interface, i don't need to touch my code for it to work.

That's pretty cool. Somebody is writing a class i never DREAMED of, two years after I wrote my code, and my code can handle it. I get to sit on the beach reading "Harry Potter", while THEY write new code, and I know my code is rock-solid.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
Unable to create an instance of the superclass or interface. Well, sometimes, but not always.

What you do is to specify a set of methods you are going to use. In the examples you posted you tell the compiler you are going to use the methods in the Person class in the foo package.
Obviously Employee is a subclass of Person, so it has all the same methods. You are telling the compiler to use a "Person" and give it an "Employee" object to deal with.

Imagine I had some children around and asked them, "Would you like a biscuit?" [This analogy won't work in America.]
"Yes, Campbell, we would, thank you."
"There you go, Dave, there's a chocolate biscuit."
"Thank you."
"There you go, Alan, there's a wafer biscuit."
"Thank you."
"There you go, Simon, there's a caramel biscuit."
"Thank you."

Now, each of them has the same (a biscuit) and each has something different, but each biscuit has an "eatMe()" method.

This will not work for methods declared in the subclasses; you cannot expect them to invoke the biscuit's "dunkIncoffee()" method because that is only implemented for the GingerBiscuit subclass.
I agree. Here's the link:
subject: Only one question in refference and actual object type?
jQuery in Action, 3rd edition