Showing posts with label access modifier. Show all posts
Showing posts with label access modifier. Show all posts

Saturday, September 20, 2014

Published 9/20/2014 by with 0 comment

Illegal modifier for parameter ... only final is permitted

A local variable, i.e., method level variable or a parameter cannot be declared as static or public/private/protected.

So local variables must be declared without these modifiers.

For example, the following code is wrong:

    void methodA(static int param) {

        System.out.println(param);
    }

    void methodB(private int param) {

        System.out.println(param);
    }

    void methodC() {

        private static int methodVariable = 10;
        System.out.println(methodVariable);

    }


To resolve the error, remove static and the access modifiers from the local variable declarations. Only the class level variables (declared outside of any method) can be declared with static and access modifiers.
Read More
    email this       edit

Thursday, December 12, 2013

Published 12/12/2013 by with 0 comment

The constructor ... is not visible

When you invoke the private constructor of a class in another class, you’ll get the error message “The constructor ... is not visible”. And when the private constructor of a super class is invoked from the subclass implicitly or explicitly, you’ll get the following:

Implicit super constructor ... is not visible for default constructor. Must define an explicit constructor.
Implicit super constructor ... is not visible. Must explicitly invoke another constructor.

See an example below:

public class SuperClass
{

       private SuperClass()
       {
              System.out.println("Super class private constructor");
       }
      
       public SuperClass(int n)
       {
              System.out.println("Super class protected constructor");
       }
}

In the above code, the class has two constructors. Access modifier for one is private and another is public. So if you create an instance of this class using the private constructor in another class, you’ll encounter the error.

You cannot write SuperClass  superClassObject = new SuperClass();
You should call other constructors that you can access as per access modifier rule or the other way of instantiating the class (if available in that class). Sometimes such class has some getInstance() like methods.

Again, when you inherit a class that has private default constructor, you have to write your own constructor and invoke a constructor (that is accessible) within your constructor. In such case, if you do not write any constructor in the subclass you’ll get “Implicit super constructor ... is not visible for default constructor. Must define an explicit constructor.”

This happens because when you do not define any constructor, a default constructor is automatically created by the Java compiler and the default constructor invokes the default constructor of the super class. This is how the constructor is invoked implicitly.

See the code below:
//Code1
public class Subclass extends SuperClass
{
      
}
Or
//Code2
public class Subclass extends SuperClass
{

       public Subclass()
       {
             
       }
}

I have not defined any constructor in code1. In code2, I have defined a constructor but have not invoked any constructor explicitly. So the super class default constructor will implicitly be called.

In the code below I have defined a constructor. But still I called the super class default constructor explicitly through “super()”. I’ll encounter the error.

public class Subclass extends SuperClass
{

       public Subclass()
       {
              super();
       }
}

Now the solutions are:
1.      
      We cannot call the private constructor of class from the subclass or other class. You should look the other constructors or the other way of instantiating class.

2.       If the default constructor of a super class is private, you have to write your own constructor in the subclass and explicitly call visible constructors.

For the above scenario, correct coding would be:

public class Subclass extends SuperClass
{

       public Subclass()
       {
              super(10);
       }

}
Read More
    email this       edit

Thursday, May 19, 2011

Published 5/19/2011 by with 2 comments

attempting to assign weaker access privileges

Java error "attempting to assign weaker access privileges" can happen when we override a superclass method in the subclass if the access modifier is not correctly chosen.

See the sample code below:

public class SuperClass
{
public void display()
{
System.out.println("this is super class");
}
}


public class Subclass extends SuperClass
{
private void display()
{
System.out.println("this is subclass");
}
}

If the above Subclass is compiled the error message will be:
display() in Subclass cannot override display() in SuperClass; attempting to assign weaker access privileges; was public

Let's understand the error message:
The method display has public access in the SuperClass and private access in the Subclass, that means when overriding this method, a weaker access privilege is going to be set (private is weaker access privilege than public).

So we need to know the allowed access modifier in the subclass for method overriding. The table below shows this:


Access modifier in super class method
Allowed access modifier for overriding
private
private, package, protected, public
package
package, protected, public
protected
protected, public
public
public


Read More
    email this       edit
Published 5/19/2011 by with 4 comments

modifier private not allowed here

Java error "modifier private not allowed here" can occur for any of the following reasons:

1. An outer class declared as private like below:
private class OuterClass
{
    //...
}
An outer class only be public or package private not private or protected. So you can write an outer class as below:
public class OuterClass
{
   //...
}
or
class OuterClass
{
   //...
}
This is true for an interface also. An interface can only be public or package not private or protected.


2. An interface method declared as private.

public interface TestInterface
{
   private void method(); // wrong as the interface method is private
}

An interface can contain only public or package private method.

The correct code of the above is:

public interface TestInterface
{
   public void method();
}
or
public interface TestInterface
{
   void method(); // interface method declared as package private
}
Read More
    email this       edit