Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question

 
anthony ward
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I just started revising for the exam... I finished the book and now doing some code example before trying the mock exams
anyhow here is what i don't understand!

let say we are in the same package
three classes a and b and c

class a{
protected void xx(){
System.out.println("print something");
}
}

class b extends a{}
class c extends b{
....main(...){
b var= new c();
var.xx(); ////////// WORKS fine ok good
}
}

the little code above works fine but
I don't understand why if class a is in a different package it does not work anymore
i have to do
c var = new c();
var.xx();
or even if I do
b var= new b()
var.xx(); does not work (while a is in a DIFFERENT package)
any help would be appreciated!

thank you


 
Angelika Angley
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice what access modifier has the method xx()?
 
anthony ward
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
yes protected, I'm playing with protected and default to understand it very well! which I don't at the moment
I know protected is about inheritance,
but
why if i Use polymorphism b var = new c(); does not work while c var = new c(); works or even b var = new b(); does not work while it extends class a.. that is what i don't understand!!
 
anthony ward
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i think i found the answer in another thread
the rules states
A subclass in another package can only access protected members in the superclass via references of its own type or it supertypes

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
default=package restriction
protected=no package restriction

but note that you cannot make a reference of the superclass in the subclass if the modifier is protected.
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankitt, there is a package restriction on protected members. Protected members are only visible in different packages, through importing, and and subclassing.



I always say to myself about protected members; 'protected is package wide, but dont forget the kids!'
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah you have to import!

thanks stephen

 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also there is one crucial point exist here:

This restriction on protected members (that you can access the protected member only through inheritance) is not applied to static members! It basically means that you can access the static protected member just like how you access a default access member in the same package. (through a reference of the super class object)

If you are interested, you can read the whole discussion here:
http://www.coderanch.com/t/432333/Programmer-Certification-SCJP/certification/Confusing-protected-behavior
 
Stephen Davies
Ranch Hand
Posts: 352
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Morteza - Manavi - Parast,

The static rule applies for all access modifiers, not just protected, however you must still import the class which contains the static member, if its not in the same package!
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen - Davies,

Ok, you don't get what I mean. First of all, of course, obviously you need to import your desired package before use its classes. BUT after you import the target package, if there is a protected static member exist in your super class in the target package, you can access that static member through a reference of the superclass as well as the subcass.

So, make sense? If not, then definitely you need to go and read the post I mentioned carefully!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic