Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

please multi level inheritence in protected?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
vipul hasija
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this code is running well..?
though son having a protected access of a method pint
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic