aspose file tools*
The moose likes Beginning Java and the fly likes please multi level inheritence in protected? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please multi level inheritence in protected?" Watch "please multi level inheritence in protected?" New topic
Author

please multi level inheritence in protected?

vipul hasija
Greenhorn

Joined: Sep 10, 2011
Posts: 10

this is the code declared in mypack package..

part1 folowing code will work



part2 following code will not work please explain why..?
father ineherit protected member function paint of Pro class



part3 if above code is not working then why below code is working..?



because if the function paint is default or protected or public then it can only be inherited by son class...
if part2 code is not working then it means that function paint inherited in father class is private...
please explain I getting mess up between these...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

Why part 2 does not work: The paint() method in class Pro is protected. That means that you can only access it in the class itself, in subclasses and in classes in the same package. You are trying to access it, in line 16, from the class ProM, which is not a subclass of Pro and which is not in the same package as Pro. So you are not allowed to access it there.

Why Part 3 works: Class Son is a subclass of class Father, and class Father is a subclass of class Pro. Note that Son is also a subclass of class Pro (altough not directly). Because of this, class Son is allowed to access the paint() method of class Pro, so you can call it from the pint() method in line 10.

vipul hasija wrote:because if the function paint is default or protected or public then it can only be inherited by son class...

I don't understand what you mean by this. If a method has default, protected or public access, then it can be accessed by subclasses, and it can be overridden in subclasses.

vipul hasija wrote:if part2 code is not working then it means that function paint inherited in father class is private...

No, it isn't private in the Father class. You can't access protected methods from class ProM because it is not a subclass of Pro and it is not in the same package as Pro.

See: Controlling Access to Members of a Class


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
vipul hasija
Greenhorn

Joined: Sep 10, 2011
Posts: 10

this code is running well..?
though son having a protected access of a method pint
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

That works because class Father and class Mc are in the same package.

Classes that are in the same package can access each other's protected members.

Try putting class Father in another package, you'll see that you'll get an error in line 20 where you try to call the pint() method from class Mc.
vipul hasija
Greenhorn

Joined: Sep 10, 2011
Posts: 10
Jesper de Jong wrote:That works because class Father and class Mc are in the same package.

Classes that are in the same package can access each other's protected members.

Try putting class Father in another package, you'll see that you'll get an error in line 20 where you try to call the pint() method from class Mc.



tried putting father in a mypack package..

please reply that part 3 is running only till father class is in same package if package differs then paint function of pro class inherited by father class will not be available to son class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14104
    
  16

vipul hasija wrote:please reply that part 3 is running only till father class is in same package if package differs then paint function of pro class inherited by father class will not be available to son class.

You can just try it out yourself.

I understand correctly what you mean, then your statement is wrong. Suppose we have class Pro in mypack with a protected paint() method, class Father in mypack, and class Son in the default package. Then from inside class Son you can still call the paint() method, because Son is a subclass of Pro (even though it is not a direct subclass).

The rules for protected are that protected members can be accessed from:

  • the class in which the member is defined itself
  • subclasses (either direct or indirect subclasses); it does not matter if the subclass is in the same package or not
  • classes in the same package; it does not matter if these are subclasses or not

  • Protected members cannot be accessed from any other place which does not fit at least one of those three criteria.

    See: Controlling Access to Members of a Class
    vipul hasija
    Greenhorn

    Joined: Sep 10, 2011
    Posts: 10
    in case of sub class..
    1. used by other function if subclass is in other package.
    2. used directly if subclass is in same package..
    am i right.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: please multi level inheritence in protected?