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 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. ...
|
|||||||
|
|||
|
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 |
| Sponsored Links |
|
|||
|
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(); |
|
|||
|
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. |
| 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 |
| 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 |