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.

What is the reason for error...?

This is a discussion on What is the reason for error...? within the Java forums, part of the Software Development category; Hi all, First of all I want to thanks for Rijus & Sirje for their replies for my previors doubt. I m now cleared with that connept at some extent. ...

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-22-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
Unhappy What is the reason for error...?

Hi all,

First of all I want to thanks for Rijus & Sirje for their replies for my previors doubt. I m now cleared with that connept at some extent. But in the following program why the compiler gives error. The super class animal reference f cant invoke subclass drink method....? What is the reason...?


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 void drink()
{

}
}

public class Flo
{
public static void main(String args[])
{
animal d=new animal();
animal f=new horse();
d.eat();
f.eat();
f.drink();

}
}


Thanks & Regards,
Abhijit
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-22-2009
Junior Member
 
Join Date: Sep 2009
Location: Chennai
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
VThamizhselvi is on a distinguished road
Smile Re: What is the reason for error...?

Hi Abhijit !

It is illegal access.
Look at this line....
animal f=new horse();

here u r crate an reference object 'f' for animal class, it holds horse class 's properties and behaviors.It called technically Polymorphic objects.

Polymorphic object access only overridden methods .
Suppose u want to access the non-overridden methods please
you can do explicit type casting.

i.e
horse h=(horse)f;then u call the method
h.drink();
Reply With Quote
  #3 (permalink)  
Old 09-22-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: What is the reason for error...?

Hi Abhi,

COMPILE TIME CHECK:

When we call a method using any class reference then first compiler checks whether the method present in the class? If not then it is a compile time error. As in this case

f.drink(); //Compile time Error not found in animal class

So

animal f = new horse();

Here f can be used to call only overridden methods.reason see here-

RESOLVING CALLS TO OVERRIDDEN METHODS:

Call to overridden method is resolved at run time.
Which method to call is determined on the basis of the object not on the basis of the reference.

d.eat(); //since eat() is present in super class (animal) so it compiles but calls super class method because object is of super class

f.eat(); //since eat() is present in super class (animal) so it compiles but calls the sub class method

Above we saw that in both the cases the presence of eat() method is being checked in super class at compile time because both references(f and d) are of super class.

Now which method to call is resolved at run time on the basis of the object the reference refer to


So if you want to call the drink() method using same mechanism as you are calling then define it in super class also to avoid compile time check.

Hope it would help you.
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
what is the reason for the following? joy_0007 SQL 3 12-17-2008 01:50 AM
Reason for the following programm's output! mkannanmca86 C and C++ 12 08-07-2008 08:07 AM
I have XP and IE7, For some reason web testing won't work in QTP Geek_Guest QTP 2 06-13-2007 03:45 PM
Forms are appearing BLACK, What is the reason Geek_Guest Oracle Apps 0 04-03-2007 12:17 PM
Kindly provide the Reason joel ASP.NET 2 09-16-2006 04:49 PM


All times are GMT -4. The time now is 05:58 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