Domanda

I am using netBeans7.3.1 and getting error *illegal start of type * even though I have written super() as the irst line of class2.

package javaapplication9;

class class1 {

    class1() {
        System.out.print("class1 constructor");
    }

    void msg() {
        System.out.println("Hello class1");
    }
}

class class2 extends class1 {

    super();

    void msg() {

        System.out.println("Hello class1");

    }
}

public class JavaApplication9 {

    public static void main(String[] args) {

        class1 obj = new class1();
        obj.msg();
    }
}
È stato utile?

Soluzione

You forgot the constructor.

class class2 extends class1 {

    class2() {
        super();
    }

    void msg() {

        System.out.println("Hello class1");

    }
}

Altri suggerimenti

It should be inside the constructor of your sub class.

class class2 extends class1 {

    class2() {
        super();
    }

So that it invokes your super class constructor when you create subclass object.

Please prefer to read it before proceeding.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top