-8

So I have question from a past quiz. The following code compiles as long as C and D are in the same package

class C {
   int i;
}

class D extends C {
   void m() {
       this.i = 3;
   }
}

Can someone explain the concepts why this is true or false?

Mat.S
  • 1,814
  • 7
  • 24
  • 36

5 Answers5

5

It has a default access modifier.

Access modifiers:

  • public world level visibility (all classes, everywhere),
  • protected like default, but also visible to extending classes,
  • default (you actually don't write any of the three above) - package level visibility.
  • private class level visibility.

Since your field i does not have explicitly written access modifier it is implied that it has a default access mod. and is visible inside the package.

For more information, read Controlling Access to Members of a Class.


Regarding this keyword - it's just a refenrece to the current object.

Since the class of the current object extends the C, it has its methods and fields. And since i is visible in the entire package, instance of D can access it with this.

darijan
  • 9,725
  • 25
  • 38
1

Because the classes has default, means package visibility. For more about visibilities see here.

Sqeezer
  • 1,267
  • 2
  • 14
  • 23
0

Read this: It clearly explains what you are look for:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

ajay.patel
  • 1,957
  • 12
  • 15
-1

This is because the default visibility is package. So all the other classes in the package can see the variable. And since you are extending C , D inherits the variable i.

sidshu
  • 315
  • 4
  • 13
  • 1
    No it does not inherit it. It sees it because they're in the same package. – m0skit0 Jun 17 '13 at 13:59
  • I understand its in the same package but D is extending C as well so it inherits the variables as well thats why you can do this.. Can you do that without extending the class? PLease explain the downvote – sidshu Jun 18 '13 at 09:46
  • @m0skit0 I understand people are downgrading your posts but that doesnt mean to go and do it for other people as well. I am also here to learn ans share my knowledge. – sidshu Jun 18 '13 at 09:52
  • I don't care about people downvoting answers for fun or rage or whatever. I only downvote wrong answers. `default` fields are NOT inherited by child classes. So no, `D` does not inherit `i` from `C` (`C#i`). – m0skit0 Jun 18 '13 at 09:55
  • I am confused here. When you mean by inherit, it means D can access all the variables of C without creating an instance of C. Thats what its meant by this.. Also you cant even extend the class C outside the package can you?. So as long as they are in the same package B is 'inheriting' C . can you do this.i without extending C? – sidshu Jun 18 '13 at 10:31
  • *"Also you cant even extend the class C outside the package can you?"* Of course you can. You can do `D extends C` no matter what package D is in as long as `C` is `public` and not declared `final`. *"So as long as they are in the same package B is 'inheriting' C"* Absolutely not. Inheriting is only when you extend a class. *"can you do this.i without extending C?"* Nope. – m0skit0 Jun 18 '13 at 11:09
-2

It is true.

Since you didn't explicitly specify C#i visibility, this means it has default visibility, which means only package visibility. It is different from protected in the way that such fields (or methods or classes or whatever) are not inherited, thus default visibility is more restrictive than protected.

So doing this.i in D is valid as long as C and D are in the same package. Otherwise, even if D extends C, C#i is not visible because it has default visibility (which is not inherited).

See the documentation for all visibility modifiers and how they work.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • What is `C#i visibility`? – crush Jun 17 '13 at 13:59
  • In Java, when you refer to a member of a class, you write it like that. Thus `C#i` means field `i` in class `C`. – m0skit0 Jun 17 '13 at 14:08
  • Explanation for the downvote is welcome. Or are you just raging? ;) – m0skit0 Jun 17 '13 at 14:09
  • I thought it was something like that, but it's been awhile since I've seen it referred to in that fashion. Might be a good idea to add that to your answer. Seeing how the OP is obviously very new to Java, I would expect him to be equally lost. Sometimes more is less. – crush Jun 17 '13 at 14:11
  • Nonsense. It is standard Java notation. If the OP doesn't understand it, he can ask himself. – m0skit0 Jun 17 '13 at 14:12
  • That elitest "if he doesn't know then he better educate himself" attitude is probably why you are getting down voted by others. Just a thought. – crush Jun 17 '13 at 14:13
  • Don't put words in my mouth I didn't say. I said he can ask himself, I have no problem answering. And I don't care about your votes, just asking for an explanation for the downvote, maybe I said something wrong. But since it is not here, someone is just obviously raging because his answer is wrong. I never heard any of my students complaining about my supposed "attitude". I'm here to help people in what I know. You can keep hunting reputation if that what matters to you. Take care! – m0skit0 Jun 17 '13 at 14:18
  • You've now earned a downvote from me. You might try being more humble at SO. – crush Jun 17 '13 at 14:18
  • As I said, I don't care about downvotes out of pure raging. I'm here to help and learn. I don't care about anything that doesn't fit in these 2 categories. So you can do as you wish ;) – m0skit0 Jun 17 '13 at 14:19