• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected static method visibility

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello from Java Junior

Scenario:two classes (super class and subclass) in different packages.
Super class has two protected methods,static and nonstatic method.
Subclass using reference on superclass can "see" protected static method but cannot see non static protected method...

package scjp.staticPlusProtected.package1;

public class Country {

protected static void protectedAndStaticMethod(){}
protected void protectedButNotStatic(){}

}


package scjp.staticPlusProtected.package2;

import scjp.staticPlusProtected.package1.Country;

public class Croatia extends Country {

public static void main(){

Country cr=new Country();
cr.protectedAndStaticMethod();// compiler says OK WHY??
cr.protectedButNotStatic();//---->error..I expected that....The method from the type Country is not visible



Croatia croatia=new Croatia();
Croatia.protectedAndStaticMethod();//OK...static protected member is inherited
croatia.protectedButNotStatic();//OK protected member is inherited


}



So why is it possible to see static protected method of class that is in different package??Thanks
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this has nothing to do with the static inheritance
static members are imported with the



your line of code



also keep in mind that
importing packages means allowing ourselves to write a short name for the class and nothing else

following works fine


and also without importing the static member i.e. by using the long name of the class and the class member


hope this helps
 
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I played around with your program.

If Croatia does not extend Country the compiler throws the desired error about protected access.
In this case an outside class is creating a class.

If Country does extend Country it does not throw an error with the protected static method.
In this case a subclass is creating its superclass.

Since Croatia is a Country it has access to the class method.
 
Eric Kizaki
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Croatia is a Country:


is the same as:



If Croatia was not a country you will get protected access error when trying to do either.
 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello prasad...

Static members are not inherited and
does it mean that using protected with static members is similar to using public??
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hareendra Reddy wrote:Hello prasad...

Static members are not inherited and
does it mean that using protected with static members is similar to using public??


It is indeed true that static methods are not polymorphic, and you can't override them and get polymorphic behavior. But static methods are inherited in the sense that they're available amongst the child class's methods; you can call a static method using the child class's name, or using an instance of the child class, or without qualification in the body of a child class.
 
Eric Kizaki
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed, a subclass has access to all the static methods of the parents. Static methods are methods of the class and since a subclass is that class it has access. Outsider classes can use static methods as utility methods if they are public.



An outsider class needs public access to the static methods.
 
Sarica Ivic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone...
So conclusion would be:ChildInDifferentPackage can acess parent's public and protected static methods.
And child also gets those methods through inheritance...
And child does not see parent's private and default static methods nor gets them through inheritance.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic