• Post Reply Bookmark Topic Watch Topic
  • New Topic

Access Modifier Question  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Default {
private int pri = 5 ;
protected int pro = 6 ;
public int pub = 7 ;
int def = 8;
}

public class Public {
private int pri = 1 ;
protected int pro = 2;
public int pub = 3;
int def = 4;
}


class Test1 extends Default {} // both are in same package
perfectly fine .
Can't inherit private
Can inherit protected - 1] they now will be private ?
Can inherit public - 2] will be public only ?
Can inherit default - 3] they now will be private ?


class Test2 extends Default {} // both are in different package
give an error at compile time .


class Test3 extends Public {} // both are in same package
perfectly fine .
Can't inherit private
Can inherit protected - 4] they now will be private ?
Can inherit public - 5] will be public only ?
Can inherit default - 6] they now will be private ?


class Test4 extends Public {} // both are in different package
perfectly fine .
Can't inherit private
Can inherit protected - 7] they now will be private ?
Can inherit public - 8] will be public only ?
Can't inherit default


Is this whole example & this 8 points are correct ???

please help me out .

thanks .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "they now will be private ?" and "will be public only ?"
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean is , if any other class will extends this class i.e. Test1( that is already subclass of any class i.e Public ) then how this members behave for 3rd class .

I hope it is clear now ...

please help ...

thanks .
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I understand your question.

I believe you should try these things yourself. You'll remember them much better.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i understand that it will help myself only but as you know that time is very less for beta ...
any expert can have a look & easily tell the answer .

don't explain answer if you also have less time but atleast mention the right answer .

thank you very much .
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help ...
thanks .
 
saran sadaiyappan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The prtected variable remains private to the subclasses in other packages.what I mean is that if some var in class 1 in package A is protected ans some class 2 in package B extends class A then the protected var remains private here so that anyother methods in class B will not be able to access it.

2)The public as everyone knows remains universally public

3)The default var behaves in the same manner as protected but their scope in not inter package specific.

I hope this shd ans all your questions.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks saran .
yes i got completely .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!