How does java implements runtime polymorphism.?

Hi all.
we know how does runtime polymorphism take place in java.
But my quesions is , how does java implement it internally.
in C++, with the use of keyword virtual , complier decides to make a call at runtime using virtual table and Vptr.
like in c++,
class base {
public:
virtual void show(){
cout<<"I am in base class"<<endl;
class child : public base {
public:
void show(){
cout<<" I am in child class"<<endl;
int main(){
base*p = new child();
p->show();
return 0;
out put - I am in child class.
but if we remove the virtual keyword then output
I am in base class..
We know how it happens
but in java
class base {
void show(){
System.out.println("I am in base class");
class child extends base {
void show(){
System.out.println(" I am in child class");
class demo {
public static void main(string args[ ]){
base b;
child c = new child();
b = c;
b.show();
output is - I am in child class
but how can i bring the output as
I am in base class ---
complier knows that b is base reference so y doesnt it just use base version.

if all methods are virtual..then we should always have runtime binding but we do have early biding too.
shouldnt we able to call base verison using a base reference variable like in c++.
May be I m mixing big times java n c++. But it seems to me as core java is much like c++ . The things u can do in c++ , u can do same in java in different ways.

Similar Messages

  • Java supports runtime polymorphism?

    Java supports runtime polymorphism but fails if overridden method is a static method?

    As static method associated with a class (with all the instances) it may fail the runtime polymorphism.
    But I can see the overriding is still allowed. When I run the following code it runs perfectly without any error. I am not sure
    public class TestAnimal {
         public static void main(String[] args){
         Animal A = new Dog();
         A.bark();
    class Animal{
         public static void bark(){System.out.println("I don't know how to bark");}
    class Dog extends Animal{
         public static void bark(){System.out.println("barking");}
    I am not sure if bark() function is not overridden.
    Any thoughts?

  • How does java support the concept of destructor?

    How does java support the concept of destructor?

    @KunalSurana: before you flood the forum with basic questions, do us a favour and read.
    [Sun's basic Java tutorial|http://java.sun.com/docs/books/tutorial/]
    [Sun's New To Java Center|http://java.sun.com/learning/new2java/index.html]
    Includes an overview of what Java is, instructions for setting up Java, an intro to programming (that includes links to the above tutorial or to parts of it), quizzes, a list of resources, and info on certification and courses.
    jGuru
    A general Java resource site. Includes FAQs, forums, courses, more.
    JavaRanch
    To quote the tagline on their homepage: "a friendly place for Java greenhorns." FAQs, forums (moderated, I believe), sample code, all kinds of goodies for newbies. From what I've heard, they live up to the "friendly" claim.
    [Yawmarks List|http://forums.devshed.com/java-help-9/resources-for-learning-java-249225.html]
    [The Java Developers Almanac|http://www.amazon.com/exec/obidos/tg/detail/-/0201752808?v=glance]
    [http://javaalmanac.com|http://javaalmanac.com]
    Bruce Eckel's [Thinking in Java(Available online.)|http://mindview.net/Books/DownloadSites]
    Joshua Bloch's [Effective Java|http://www.amazon.co.uk/exec/obidos/Author=Bloch,%20Josh]
    Bert Bates and Kathy Sierra's [Head First Java|http://www.amazon.com/exec/obidos/tg/detail/-/0596004656?v=glance ]
    James Gosling's [The Java Programming Language|http://www.bookpool.com/sm/0321349806]
    Gosling is the creator of Java. It doesn't get much more authoritative than this.
    Joshua Bloch and Neal Gafter [Java Puzzlers.|http://www.javapuzzlers.com/]

  • How does java.exe behave if I only use -Xmsn option?

    Hi,
    if I only set the -Xmsn option and do not set -Xmxn option when I launch my Java application, how does java.exe behave? Does the heap will automatically grow like what Bea's JRockit does when my application requires more memory?
    I use JDK 5.0.
    Thanks for your help!

    If your application needs the space, the JVM will grow the heap up to the
    default value of -Xmx. Note that it will run a garbage collection cycle before
    expanding the heap, and it may take a few or several cycles to expand to
    the -Xmx value. How fast or slow it expands depends somewhat on the
    behavior of your application (how much it allocates and how long the
    objects live).
    The default value of -Xmx depends on the platform; see
    http://java.sun.com/docs/hotspot/gc5.0/ergo5.html.

  • Java Gurus: How does Java compare to 4GLs?

    I've been using Java for a year now. I love Java. As a 3GL, how does Java compare to a 4GL?
    (I can't say I know a whole lot about fourth generation languages. The only 4GL that I'm a little familiar with is Clarion.)
    What are your thoughts?

    What is 3GLs and 4GLs? You mean Programming Language and Scripting Language?

  • How does Java store floating point numbers?

    Hello
    I'm writing a paper about floating point numbers in which I compare an IEEE-754 compatible language [c] with Java. I read that Java can do a conversion decimal->binary->decimal and retain the same value whereas c can't. I found several documents discussing the pros and cons of that but I can't find any information about how it is implemented.
    I hope someone can explain it to me, or post a link to a site explaining it.
    Cheers
    Huttu

    So it is a myth.
    I still ask because I observed a oddity: When I store 1.4 in c and printf( %2.20f\n",a); it I get 1.39999999999999991118. If I do the same in Java with System.out.printf( %2.20f\n",a); I get 1.4. If I multiply the variable with itself I get 1.95999999999999970000:
    double a=1.4;
    a=a*a;
    System.out.printf( %2.20f\n",a);
    {code}
    Does this happen because of the rounding in Java?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How does Java file I/O move files?

    I'm working support for an integration platform, and there's a problem at a customer that seems to crop up once in a blue moon, yet still all to often. Its a bit difficult to track down, because they are using an old piece of software that is not officially supported anymore, but the problem still needs to be solved and I could use some help to either confirm or dismiss my suspicion of what might be the cause of it.
    What happens is this....
    Their system places a file in a folder, and the file is /supposedly/ fully created, and then just moved to the folder. Then an adapter program takes the file, puts it on a messaging queue, and after that is successful, it moves the file into a backup directory.
    Now, what has happened is that sometimes, the files that end up being sent are incomplete. Either the writing application messed up, or the adapter somehow went crazy. But more puzzling is that I've observed that the files have been correctly sent, but the file that ended up in the backup-directory was empty. It is not possible to send an empty file through the platform, since there's transformation logic that handles it. Those parts I completely trust, but I need to verify if it might indeed be the adapter, or the customer's own systems that messes up the message.
    So the question really boils down to, how does the java file-I/O method for moving a file work? Does it copy it bitwise, or does it call the system's move-command? Because if it copies it bitwise, it might mean it reads the data at the exact moment the file is created but not yet filled, and then doesn't check again as it writes it to the storage, and then just deletes the file. If it calls the system's move command, the system wouldn't be able to move a file that is being written, and something else is more likely the problem.
    The machine is a windows 2000 server, sp4, and the java being used is jdk1.3.1_03.
    Rewriting the adapter is not an option, and it will hopefully be replaced in the near future (as in, within a year...) whenever the customer deems it can budget for the work. The adapter is not officially supported by us, but the platform is, and I need to pin-point the problem.

    So the question really boils down to, how does the
    java file-I/O method for moving a file work? Does it
    copy it bitwise, or does it call the system's
    move-command? It could be either one. Both are available, so it depends what the programmer for that tool decided to use.
    Without more details, it's impossible to be sure what the probem is, but a couple of general possibilities that fit what you've described pop to mind:
    * The copy/move/send step is happening before the file being written is flushed or closed. This might be because an exception in the writing code is causing the flush/close to be skipped entirely.
    * The copy/move/send is being called too soon.
    * The step that's reading the file after its been copied/moved/sent is ocurring before the file is completely there.

  • What is the output of Maya and how does Java use it in animation.

    What type of software is used and what input and output files are created in the animation process?
    From what I read:
    Maya is used to create polygon structures, perform rigging, and it attaches surface textures which are made in Photoshop. Then Maya creates key images or positions of the animation. Next Maya exports an image and a data file to do the neccessary transformations. Finally, Java uses the image and the data file to paint the images and their position based on the game logic's use of the data file.
    What have I missed or described wrong?
    I also need more clarity on the input files to Java and how Java stores and uses these files. The use of Maya images and files seems different. Is Maya or Java doing the transformations or are they both doing them? How does this work?
    Thanks for your help
    Jon

    Thanks for the explanations. What this means in my situation is that tags are an unnecessary way of spending a lot of time making my file system more complicated, without adding any value whatsoever. Without tags, I can find any of my 3,000 files with a few clicks. (On my computer, Spotlight has cobwebs. I very rarely need it, but it's wonderful when I do.) Now that I know how tags work, I can ignore it.
    A human being can only perceive a quantity of seven without counting or grouping; hence we arrange things in structures to keep the number of entitites low on each level. You don't need to count three objects to know that they are all there, but you do need to count twelve objects. Which is why Home Alone was believable. We sort screws and nails in jars and toolboxes have compartments. Even the grocery store has a hierarchical arrangement. You don't find steaks in the bakery section, or breakfast cereal in the produce department.
    Some people aren't organized, and that's not a bad thing, but no folder, no tag, and no group is going to change a fundamental personality characteristic.
    Thanks everyone for showing me that in my case, there's no point to tags. That saves me a lot of time.

  • How does java basically work?

    I know that java makes use of many classes which have inherent methods that do our work. But I want to know how those methods work. where are the files for its running? Like in println, how does it output to the console?
    please explain.
    And also tell me how java was built...
    Thanks in advance.
    Megamatrix.

    If you want to see the source code of the standard API classes it's very easy - during the installation of the jdk you can choose to install it. In that case there will be a large file called src.jar in the JDK installation directory. You can extract the sources from it with eg. winzip or the "jar" command line tool.
    And if you are interested in the source of the JDK itself, it can be downloaded too... http://www.sun.com/software/java2/index.html

  • How does Java Compiler deal with circular dependence?

    A simple example is in the most basic class Object, you have a toString() method which uses String class, but, a String is an Object class first. So when the compiler encounters this circular dependence situation, how does it deal with it?
    Anyone has some ideas?

    I studied this a while ago... I think I know the answer, but you can still confirm it.
    Suppose you said javac Object.java, the first thing it would do is load the file into memory and layout its functions. Then it will search for dependencies. Finding String on in the classpath, it loads that up into memory too. It then searches for String's dependencies and so on...

  • How does Photoshop implement L*a*b*. Does it use CIE LAB with 2 percent view angle and Bradford CAT @ D50?

    Does anyone know how Adobe Photoshop implements
    L*a*b* to calculate image data
    White Point D50 or D65
    What version of Lab = CIE, ICC etc..
    ant CAT's Bradford of Van Kreis
    View Angle 2percent, 10 percent?
    Thank you.

    As explained in our documentation: LAB in Photoshop is D50, 2 degree standard observer, matching the ICC version 2 PCS.

  • How does Java structure memory for an object's members?

    I'm trying to figure out how Java structures/allocates memory for objects. (Yes this is implementation specific. I'm using the Oracle 1.7 runtime for this.) I did some work on this here and here and the results are confusing.
    First off, in both referenced links, when I allocated an array of objects, the equivalent of new Object[10000], it used 4 bytes per object. On a 32-bit system this makes perfect sense. But I'm on a 64-bit system so what's going on here?
    Is Java limited to a 32-bit address space even on 64-bit systems?
    Is Java limited to a 32-bit address space per array and each array object then has a pointer to where the elements are?
    Something else?
    Second, I compared the memory footprint of 8 booleans vs. a byte as the variables in a class. The 8 booleans requires 24 bytes/object or 3 bytes/boolean. The single byte approach requires 10 bytes/object.
    What is going on with 3 bytes/boolean? I would understand 4 (making each an int) and definitely 1 (making each a byte. But 3?
    And what's with expanding a byte to 10 bytes? I would understand 8 where it's expanding a byte to a native int (I'm on a 64-bit system). But what's with the other 2 bytes?
    And in the case of different ways to create a RGB class it gets really weird.
    For a class composed of 3 byte variables it uses 24 bytes/instance. Expensive but understandable as each uses an int.
    So I tried where each class is a single int with the RGB stored in parts of the int (using bit shifting). And it's still 24 bytes/instance. Why on earth is Java taking 3 ints for storage? This makes no sense.
    But the weirdest case is where the class has a single variable of "byte[] color = new byte3;" Note that the byte is allocated so it's not just a null pointer. This approach takes less memory than the other two approaches. How???
    Any guidance as to what is going on here is appreciated. I have a couple of classes that get allocated a lot and the flywheel pattern won't work (these objects have their values changed all over the place).
    And an associated question, does the order of declaring variables matter? Back in the old days when I did C++ programming, declaring "int, byte, int, byte" used 4 int's work of space while "int, int, byte, byte" used 3.
    thanks - dave

    This is a quadruple crosspost.

  • How does java support the doc files

    Hai all,
    I am doing a project where the back up word files are to be displayed on the browser . If it is a text file we can give it as a input stream and if it is a image , it will take it as a .jpg . Then how the .doc can be inputed(I want the MS-word files to be inputed) .
    If u have a solution please mail it to
    E-mail ID : [email protected]

    I am bit confused about ur requirement.
    U cannot open a file thr' ur applet which runs in a browser. Do do so, u may have to modify the java policy file or use signed applet. These two links may be of some help for u.
    http://developer.java.sun.com/developer/onlineTraining/Programming/BasicJava1/data.html#applet
    http://java.sun.com/docs/books/tutorial/security1.2/tour1/step2.html
    But I have written a mail long back to ur email with regard to Java Securiy. plz read it. If u need more abt it, get back to me.

  • How does Java API handles binary files

    I couldn't find some documentation which describes how to extend the Java API for handling binary files. (I want to upload, check in/out (and delete) such files from within a Java application.) Does anybody know where I can find an example or some related documentation?
    TIA, Olaf

    The methods on ContentObjectDefinition
    allow you to create binary or ascii document content.

  • How does Java achieve multiple inheritance using interfaces

    Java does not allow multiple inheritance through classes as classes might contain methods with same names. what happens if a class implements two interfaces with same method names?
    I am really confused abt this? Can anybody help me out?
    Message was edited by:
    vijkris

    yes to avoid the ambiguous functions which can result due to multiple inheritance of classes like in c++ , java doesn't have this through classes. But if you have same method (both return type and parameter) then java doesn't bother and it won't complain as ultimately only one implementation is possible in deriving class even though method declalaration is there in both the interfaces. If return type changes then it won't compile as it can't overide the both methods as they have same name and different return types. thats why inside interfaces they restricted the implementation of methods so that it can work fine in ambiguous scenarios.

Maybe you are looking for

  • New Hard Disk won't work

    G4 12" powerbook original hard disk died. Replaced with 160GB seagate momentus 5400.3 and it seems to be working ie. disc starts up is recognised by G3 iMac when connected as a firewire drive (start up + T key). Have used disk utility to format it an

  • HT201363 I need to change my Security Questions, as I cannot remember the answers to them, At all.

    How can I change them? I have tried calling them, I have tried things that are already listed on Google for it, And the website, But it just doesn't help. Can someone please help me?

  • The quicklink row on the overall window for this version of firefox continually skips two icons/links, why?

    I didn't have this issue on any previous version of my favorite browser, but now when I remove the 'Name' that would show up next to the icon, to only show the logo of my bookmarks, two icons that are adjacent to each other do not allow me to select

  • Hola, tengo un problema con iphoto.

    Hola, hace mas de 2 dias tengo un problema con mi iphoto cuando lo abro se congela y no hace nada y la unica manera de salir es forzando la salida. Ya lo he intentado actualizando la versión del programa y aún sigue con el mismo problema.

  • Keeping iMovie 06 and iLife 08

    I just installed iLife 08, and while I like the new iWeb 08 and iPhoto 08, iMovie 08 leaves much to be desired (many of the features I used in iMovie 06 are missing in the new version and I cannot use iMovie without them). I just read that there is a