Your example shows why it is not recommended to hide fields. It makes code that is hard to read and understand. In this case access to the field 'r' is based upon the reference type not the object type.

Also the method call to a.xx(); doesn't have an actual method defined for it so the code will not compile with that line there.

I think it is poor practice to assign initial values to fields outside of a constructor (unless they are static or final fields).


On 13/01/2010, at 8:05 AM, nn roh wrote:

Hi,

why in this examples  the variable r which is in parent used ?



 class Q16
   {
       public static void main(String args[])
       {
          A a = new B();
          a.callme();
          System.out.println(a.r);

          a.xx();
       }
   }


class A
   {
     void callme()
     {
         System.out.println("A");
      }
    int r=10;



    }
    class B extends A
   {
      public void callme()
      {
         System.out.println("B");
      }
         int r=20;


   }


output :
B
10

Thanks,



On Sun, Jan 3, 2010 at 8:55 PM, SAADELDIN SULIMAN <[email protected] > wrote:
superclass classname=new subclass()

there is inheritance

Please read about inheritance.



thanks

Suliman


On Sat, Jan 2, 2010 at 1:01 PM, nn roh <[email protected]> wrote:
> What is the difference if we define the class as
>
> superclass classname=new subclass()
>
> subclass classname=new subclass()
>
>
> Thanks;
> Nada
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en

--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to