• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Private Variable of superclass

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

i heard that the subclass can access the private field of the superclass
provided the both classes are members of the same class

what it means i didn't understand

can any one explain me?
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
hi

i heard that the subclass can access the private field of the superclass
provided the both classes are members of the same class

what it means i didn't understand

can any one explain me?




I think you mean to say "provided that the subclass is a member of the superclass".

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be this is what u mean:
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i got ur point
thank u shri

i tried to access the innermethod see whether this is correct way of accessing the method or is there any other way to access the innerMethod()

Please clarify

class InnerJava {
private int i = 0;
class extendsInnerJava extends InnerJava {
public void innerMethod() {
System.out.println("The value of is " + i);
}
}


public static void main(String arfs[])
{
InnerJava in=new InnerJava();
in.my();
}

public void my()
{
extendsInnerJava temp=new extendsInnerJava();
temp.innerMethod();
}

}
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats also right,
u can have main method like this as well

Hope u got it.

P.S:- from next time use CODE tags to format ur code to make it look better.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Animesh,



i dont understand when u write sth like in.new extendsInnerJava(). What is actually happening? is it equvalent to

extendsInnerJava enj = new InnerJava();

ans secondly an Inner class is associated with an instance of the enclosing class and its just like non-static members, so how can we directly use this statement inside the main() as main() is a static content and extendsInnerJava class here is actually instance inside an instance.
As you can see that if the code was



then we get a compilation error saying non-satic cant be accesed from static.

Thanx
amit
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Amit,


dont understand when u write sth like in.new extendsInnerJava(). What is actually happening? is it equvalent to

extendsInnerJava enj = new InnerJava();


This is equivalent to
extendsInnerJava enj = new InnerJava(). new extendsInnerJava ();

which means we are creating an instance of extendsInnerJava using an instance of InnerJava class


Ok now let me explain u in a simple way, suppose this ur class.

Hope the above example is clear.
Now just replace ur method with an non-static inner class, i mean


Hope this is clear, any problem do let me know

Thanks
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again Animesh,

well its clear now but i need to do some theory and practice on this topic ....can you tell me some place from where i can get a good practice for this..

thanx once more,
amit
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Khalid Mughal has explained Inner classes very well. U can go through that.
Hope u r having the book or else his book is also available in the net, u can read online as well.
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i bought all the books i might require recently but haven't started anyone of them.....
i've planned to start with K&B them Mughal....mock tests .....real test(the day)


 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can u give the link where i could get the books of Khalid Mughal
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey i have it in my system but i dont know how to get it on the net. I got it from a friend of mine. Anyways, i will send it to u thru mail.
Check it out
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Send me ur email id, i will send it to u
 
reply
    Bookmark Topic Watch Topic
  • New Topic