Tags down


Java protected modifier not working as expected

By : Tdogiii
Date : October 17 2020, 11:12 PM
With these it helps The behavior you observe is because of the fact that printName is static. The protected modifier provides visibility of that method in the sub-class but if the method is non-static, invocation of that method is possible only through the instance of a sub-class (the concern you were raising "if I inherit from the class superClass as follows I do not get any error!"). The JLS section defines this succinctly.
code :

Share : facebook icon twitter icon

Java protected modifier

By : David
Date : March 29 2020, 07:55 AM
I hope this helps . I also had the impression what protected meant "accessible from the same package or from a subclass" but the Java Language Specification is of course more precise, and explains that in a subclass S of C, "If the access is by a qualified name Q.Id, where Q is an ExpressionName, then the access is permitted if and only if the type of the expression Q is S or a subclass of S."
So you can only access a protected method of the superclass via a reference to the subclass you are calling from, like this:
code :
public class C2 extends C1 {
    private C2 c2_other_instance;
    public void doItAgain() {

Protected access modifier in Java

By : Alice
Date : March 29 2020, 07:55 AM
To fix this issue The webpage @MadProgrammer linked gives a decent explanation:
code :
package first; // Current package

First fst = new First(); // from package first and does not extend anything
package second; // Current package

First fst = new First(); // from package first and does not extend anything
package second; // Current package

Second sec = new Second(); // from package second and extends First from package first
package first; // Current package

Second sec = new Second(); // from package second and extends First from package first
package first; // Current package

Third third = new Third(); // from package third and extends Second from package second,
                           // which extends First from package first

Java - protected access modifier

By : elijk88
Date : March 29 2020, 07:55 AM
I hope this helps . I think a code illustration would help here to understand better.
Add a protected member in Class E
code :
public class E extends A {
    protected int protectedIntE;
e.protectedInt = 0; // ok, but why?
e.protectedIntE = 0; // error, exactly as you expected

Understanding java's protected modifier

By : John
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a class called A in package1 and another class called C in package2. Class C extends class A. ,
What's going on here?
code :
C c = new C();

Java - Protected Modifier with Interface

By : Renatto Mysael Mysae
Date : March 29 2020, 07:55 AM
hop of those help? You cannot add more restriction to the method you override in the sub class.In the same way, you cannot add more restriction to the method you implement from an interface.
Now, since methods defined in an interface is by default - public (Yes, you don't need to write it explicitly), so you cannot make your method protected in implementing class.
Related Posts Related Posts :
  • GWT DatePicker Locale
  • How would you design a twitter like message system, how to design the messaging system?
  • Is there a robust java.util.logging handler implementation of syslog?
  • Aligning messageformat on printing a JTable
  • SXXP0003: Error reported by XML parser: Content is not allowed in prolog
  • ehcache LRU evicting recently used entries
  • java cosine similarity problem
  • Efficient way to implement a client multiple file upload service
  • Regarding Object Comparison
  • How to embed IE in Swing application?
  • Using javax.tools.ToolProvider from a custom classloader?
  • What is the difference between i++ & ++i in a for loop?
  • Match a java main class with regexp
  • Confluence Persistence
  • Is there a way for a SecurityManager in java to selectively grant ReflectPermission("suppressAccessChecks")?
  • Using the camera activity in Android
  • Is communication in java rmi secure?
  • Java - how to make "runnable" JTable?
  • How can I write a Java program to draw a box, an oval, and an arrow?
  • What is the difference between ServletRequest's getRemoteHost() and getServerName()
  • java sparse matrix problem
  • ClassCastException when using HQL
  • how to generate a csv file through java code
  • Netbeans + tomcat on Ubuntu
  • How to connect to a remote MySQL database with Java?
  • System Event Handling using Java
  • How to change Ant script with EMMA code-coverage so it can find runtime coverage data?
  • Java bytecode iconst_0 iadd sequence
  • get the size of a list in a property
  • I want to convert a resultset to a string. I have tried everything but it always gives no data found. Please provide som
  • How can i sort java JTable with an empty Row and force the Empty row always be last?
  • How can I mount network drive in Mac OS X on Java?
  • XStream: How do I map xml mixed attributes and elements to POJOs?
  • begin with java voip
  • java: Preferences API vs. Apache Commons Configuration
  • Howto distinguish Application Quit from System Shutdown
  • How to go about with real GUI development with Java Swing and MVC
  • VB support in Java 6?
  • How to use jar files without package information?
  • how to inherit Constructor from super class to sub class
  • Multidimensional arrays in Java extends which class?
  • Connect to ibm mq with jms . Specify the channel and queue manager
  • Accessing the implicit objects from a JSP EL function
  • Find current heap size with jmap
  • Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||
  • What is the best way to manage configuration data
  • commons fileUpload: specifying an upload directory within Webapplication context
  • ModelAttributes not accessible on jsp
  • Session management between thick client and server?
  • Oracle JDBC intermittent Connection Issue
  • No mapping for LONGVARCHAR in Hibernate 3.2
  • A Packet Blocker Software
  • Anyone using Django in the "Enterprise"
  • Memory leak during repeated lucene query searches?
  • First steps developing in java applet. Directions
  • java efficient de-duplication
  • Converting sequential Java to concurrent java code
  • Java variable number or arguments for a method
  • when developing locally, does it make a difference if the context is '/' or '/appname/'?
  • Problem using JUnit4TestAdapter from eclipse plugin context
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org