Results 1 to 4 of 4

Thread: Dynamic Binding

  1. #1
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default Dynamic Binding

    I have two classes, Super and Sub

    class Super{
    int a=100
    }

    class Sub extends super{
    int a=500;
    }

    class Test{
    Super sup = new Sub();
    S.o.p(sup.a)
    }

    The Output Is 100. Why In Case Of Variable JVM Does not find The SubClass One?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    How did you get that output. There should be a filed hiding. You use the same filed name on Sub and Super classes and use the same variable name. If you are working on a Java IDE you should notice it.

    So, since you use the sup object reference with the Super class you definitely get the result is 100. It never refer the variable a of the class Sub.

    Hope it's clear to you.

  3. #3
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    So, In Case Of Variable, no dynamic binding right?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Dynamic DecimalFormatter
    By felixtfelix in forum New To Java
    Replies: 0
    Last Post: 03-17-2008, 04:16 PM
  2. Help for dynamic classes
    By Bender in forum Advanced Java
    Replies: 1
    Last Post: 12-19-2007, 08:46 PM
  3. how to generate dynamic pdf
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:01 PM
  4. problems with Dynamic Xml
    By boy22 in forum XML
    Replies: 1
    Last Post: 07-24-2007, 12:10 AM
  5. dynamic update in swt
    By sandor in forum SWT / JFace
    Replies: 0
    Last Post: 05-14-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •