Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can't understand super keyword behavior  RSS feed

 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's budsy again and i hope that by now administrators recognize me.

I was studying cloning and it lead me to understand protected access better:-

here is the code which i don't understand


Second class that will be importing package one


I know that second class won't compile because of the line o1.method() since it has protected access but then why super.method() is able to call that method when it is protected ?
I hope my question is clear.


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you remember what protected access means? It gives access to classes in the same package, and subclasses by inheritance.

The Java Language Specification puts it as
A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

So super.method1() is fine, as you're calling it from within the same object. But you can only call method1() from outside the object if you're in the same package.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain what do you mean by code that is responsible for the implementation of that object ?
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected variable means which can be accessed only from the subclass not through any other external classes. It works fine even if you remove super keyword. just try and think about the protected variable and super keyword, spend some time playing with these keywords.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit - ignore the first attempt, didn't read it properly

budsy remo wrote:Could you please explain what do you mean by code that is responsible for the implementation of that object ?

I must admit, I don't find that a very clear statement (it's just quoted from the specification). What it means is, as I said, you can access it from within the same object.

So, you've got a class A. You've got another class B that extends A. An instance of B can access protected members of A on itself. But it can't access those members on a separate instance of A.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Malatesh Karabisti - I am aware of that but outside a package if we extend a class and try to access it's protected method by instantiating that class's object
example
Obj o1=new Obj();
o1.method();


but if we use super.method we would succeed .
both of them are trying to do the same thing but why does o1.method() fails and super.method() doesn't ?
 
zameel amjed
Greenhorn
Posts: 14
Notepad PHP Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This super key word is used to call a method in protected access modifier as we know the protected modifiers cannot accesible from any other package or classes without having inheritance

by using the super keyword, compiler identifies the protected method of another class is available in current class as the method has being inherited in current class

also the compiler will not allow to referance an object from another class which has Protected access. Use same class referance and call method() it is possible as the Obj class is inherited.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!