aspose file tools*
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
Author

Only one question in refference and actual object type?

Shanmugam nagaraj
Ranch Hand

Joined: May 07, 2007
Posts: 76
Hi,
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

Thanks

Shanmugam N


Thanks <br />Shanmugam N
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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: http://aspose.com/file-tools
 
subject: Only one question in refference and actual object type?
 
Similar Threads
Graphics
Difference between abstract and interface,not Syntactical diff
Calls trough interface references are slower (on Android)
useBean doubt
Reg : marker interface