Showing posts with label Constructor. Show all posts
Showing posts with label Constructor. Show all posts

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

Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

modifier static not allowed here


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

1. Constructor is declared as static(!)

A constructor cannot be static. In the wrong code below:

public class Person
{
private String firstName;
private String lastName;
public static Person()
{
//...
}
}

Here the constructor Person is declared as static. As the constructor must be non-static, the correct code is:

public class Person
{
private String firstName;
private String lastName;
public Person()
{
//...
}
}



2. Interface contains static method(!)

All methods of any interface is abstract and hence they must be non static. See the code below:

public interface MyInterface
{
public static void myMetod();
}

The above code is wrong as the interface method is declared as static. Corrected code is:

public interface MyInterface
{
public void myMetod();
}
Read More
    email this       edit

Thursday, May 5, 2011

Published 5/05/2011 by with 2 comments

invalid method declaration; return type required

The Java error "invalid method declaration; return type required" can occur for any of the following reasons:

1. You are trying to define a constructor (of course without any return type; not even void)  but the constructor name is not given same as the class name.

See the wrong code below:

public class Employee
{
 private int id;
 private String name;
 public employee(int id,String name) // constructor name is not same as the class name.
 {
  this.id = id;
  this.name = name;
 }
}


So check the spelling of the constructor name and also note that Java is a case sensitive language.

2. If you are really defining a method (not a constructor) you have forgotten to declare the return type as the wrong code given below:


public class Test
{
public aMethod() // no return type given
{
System.out.println("This is a method");
}
}

The correct code is:


public class Test
{
public void aMethod() // return type void is given now
{
System.out.println("This is a method");
}
}


Read More
    email this       edit

Tuesday, May 3, 2011

Published 5/03/2011 by with 3 comments

recursive constructor invocation

If a constructor calls itself, then the error message "recursive constructor invocation" is shown. It may happen when we overload constructors and call the wrong constructor (itself) accidentally.

See the wrong code below:
public class Employee
{
  private int id;
  private String name;
  public Employee(int id,String name)
  {
    this.id = id;
    this.name = name;
  }
  public Employee(int id)
  {
    this(id); 
    /* this(id) calls the constructor having one parameter of int type. Thus this is calling itself.*/
  }
  public Employee()
  {
  }
}


In the above code, there are three constructors:
i) Employee(int id,String name)
ii) Employee(int id)
iii) Employee()

Within the second constructor, we have written this(id) which means it calls a constructor with one int parameter and see that it is the constructor itself. It means we have not invoked the right constructor. If we want to invoke the first constructor Employee(int id,String name) within the second constructor we should write this(id,null). We are providing null for the String name as no value is available for the name variable in the second constructor but now it calls the first constructor Employee(int id,String name) as we have provided  two parameters now.

See the corrected code below:

public class Employee
{
  private int id;
  private String name;
  public Employee(int id,String name)
  {
    this.id = id;
    this.name = name;
  }
  public Employee(int id)
  {
    this(id,null); // this(id,null) calls another constructor having two parameters
  }
  public Employee()
  {
  }
}

Summary:

If you see the error message "recursive constructor invocation", check that you have provided correct no and type of parameters while calling one constructor in another one.
Read More
    email this       edit

Sunday, April 10, 2011

Published 4/10/2011 by with 1 comment

call to super must be first statement in constructor

The error message is very clear actually:

i) We can call super in the constructor of a subclass
ii) If we call super in the constructor of a subclass, it must be the first statement in that constructor; i.e, before writing any other statement we call to super must be made.

It is mentionable that, super(parameters) calls the constructor of the super class.

See the code below:

The super class...


public class Employee
{
private int id;
private String name;
public Employee()
{
}
public Employee(int id,String name)
{
this.id = id;
this.name = name;
}
}

The subclass...


public class SalariedEmployee extends Employee
{
private double salary;
public SalariedEmployee()
{
}
public SalariedEmployee(int id,String name,double salary)
{
this.salary = salary; //this is the first statement now
super(id,name); //error : call to super must be first statement in constructor
}
}

The correct subclass will be...


public class SalariedEmployee extends Employee
{
private double salary;
public SalariedEmployee()
{
}
public SalariedEmployee(int id,String name,double salary)
{
super(id,name); // now call to super is the first statement in the constructor
this.salary = salary; // it is now the second statement
}
}



Read More
    email this       edit