*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance question" Watch "Inheritance question" New topic
Author

Inheritance question

anthony ward
Greenhorn

Joined: Feb 18, 2009
Posts: 5
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

Joined: Feb 17, 2009
Posts: 40
Did you notice what access modifier has the method xx()?


[Blog] [SCJP6 75%] [SCBCD]
anthony ward
Greenhorn

Joined: Feb 18, 2009
Posts: 5
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

Joined: Feb 18, 2009
Posts: 5
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

Joined: Feb 19, 2009
Posts: 101
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

Joined: Jul 23, 2008
Posts: 352
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!'


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
yeah you have to import!

thanks stephen

Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
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


SCJP 6
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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

Joined: Dec 25, 2008
Posts: 66
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!
 
Don't get me started about those stupid light bulbs.
 
subject: Inheritance question
 
Similar Threads
Question on Casting and Exception
question from JQ+
doubt regarding assigning superclass ref var to subclass var
protected
Exception Handling