Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Hi Rijus: Plese refer the given program within the Java forums, part of the Software Development category; Hi all, In the following program how many reference varibale & how many objects are there....? please justify if possible. class animal { public void eat() { System.out.println("you are in ...
|
|||||||
|
|||
|
Hi Rijus: Plese refer the given program
Hi all,
In the following program how many reference varibale & how many objects are there....? please justify if possible. class animal { public void eat() { System.out.println("you are in animal"); } } class horse extends animal { public void eat() { System.out.println("you are in horse"); } } public class Flo { public static void main(String args[]) { animal d=new animal(); animal f=new horse(); d.eat(); f.eat(); } } Thanks & Regards, Abhijit |
| Sponsored Links |
|
|||
|
Re: Hi Rijus: Plese refer the given program
Quote:
In u r example , 1) animal d=new animal(); animal f=new horse(); U r creating two reference variables, ie. Reference variable d && reference varible f. Which means u created variables(d & f) to refer an object. In General, Reference is like a pointer to reffer an object. Geek d=new Geek(); here d is a reference variable. 2) Object can be created by using new keyword. animal d=new animal(); animal f=new horse(); Here object is created and the address stored in variable(reference) d & f. Object is an occupied memory for a class. Reference points to that memory and it has got a name(variable) Here when u write animal d=new animal(); some memory will be occupied in the system. d is the reference(variable) which points to this memory and is used to access the data present in this memory. So in u r pgm overall u created two reference varible(d &f) , and two objects for the classes. Note : animal d // Reference variable(d) for animal class new animal() // Object of animal class So dont be confused with reference(variable) & object Regards, Rijus. Last edited by rijus; 09-19-2009 at 07:37 AM. |
| The Following User Says Thank You to rijus For This Useful Post: | ||
|
|||
|
Re: Hi Rijus: Plese refer the given program
Sorry but don't be confused Bhaskar,
In this example both d and f are reference variables of animal class as Riju told animal d; animal f; But they refer to the objects of different class d refer to animal class object d = new animal(); and f refere to horse class object f = new horse(); Since a super class reference can refer to the object of its own class and its subclass (This mechanism is called Dynamic method dispatch/Dynamic method Look up / Run time polymorphism ) In this case casting is done implicitly called upper casting. f = new horse(); Here new operator allocates memory for horse object and returns its (horse class) reference,since super class can hold its subclass reference so it is assigned to animal class reference f. Thanks |
| The Following User Says Thank You to Sarje For This Useful Post: | ||
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hi Rijus: Identify mistake in given swing program | abhi981 | Java | 3 | 08-21-2009 11:19 AM |
| Refer the attached DOC file | sreekumar_nair_it | Oracle | 1 | 12-11-2007 06:35 AM |
| Struts class does not refer the properties in JSP | Geek_Guest | JSP | 1 | 08-15-2007 06:16 AM |
| What does the term refer to | Bessie | Unix/Linux | 1 | 08-25-2006 04:35 PM |