Geeks Talk

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.

Hi Rijus: Plese refer the given program

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 ...

Go Back   Geeks Talk > Software Development > Java
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-19-2009
Contributing Member
 
Join Date: Aug 2009
Location: bangalore
Posts: 30
Thanks: 11
Thanked 3 Times in 3 Posts
abhi981 is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-19-2009
Contributing Member
 
Join Date: Aug 2009
Location: Indore
Posts: 33
Thanks: 9
Thanked 5 Times in 5 Posts
Sarje is on a distinguished road
Re: Hi Rijus: Plese refer the given program

Two references and two objects.

But the subclass object can be used wherever super class object can be used.
Reply With Quote
  #3 (permalink)  
Old 09-19-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
Re: Hi Rijus: Plese refer the given program

Quote:
Originally Posted by abhi981 View Post
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
Hi Abhijith,

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.
Reply With Quote
The Following User Says Thank You to rijus For This Useful Post:
  #4 (permalink)  
Old 09-20-2009
Junior Member
 
Join Date: Sep 2009
Location: MEERUT
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
bhaskar tripathi is on a distinguished road
Re: Hi Rijus: Plese refer the given program

hi
in this program d is the object of animal class and f is reference variable of horse class
ok
Reply With Quote
  #5 (permalink)  
Old 09-20-2009
Contributing Member
 
Join Date: Aug 2009
Location: Indore
Posts: 33
Thanks: 9
Thanked 5 Times in 5 Posts
Sarje is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to Sarje For This Useful Post:
Reply

  Geeks Talk > Software Development > Java

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 05:23 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved