Search code examples
javasubclasssuperclassidentifier

Why there is an error as "<identifier> expected" with the Derived constructor arguments?


I have defined a constructor of super-class Base, but during the declaration of arguments of the constructor in the subclass Derived, why is it showing the error "identifier expected"?

  class Base 
        {     
             int x,y;       
             Base(int x1,int y1)
                {
                    x=x1;
                    y=y1;
                }
                void viewxy()
                {
                    System.out.println("x = "+x+" y= "+y);
                }
                void viewsum()
                {
                    System.out.println("x+y: "+(x+y));
                }
        }
        class Derived extends Base
        {
                int z;
                Derived(x1,y1,z1)
                {
                    super(x1,y1);
                    z=z1;
                }
                void viewz()
                {
                    System.out.println("z = "+z);
                }
                void viewderivedsum()
                {
                    System.out.println("x+y+z= "+(x+y+z));
                }
            }

Solution

  • Here:

    Derived(x1,y1,z1)
    

    You put the types in front of your parameters on the base class, what makes you think you can now omit them?

    Surprisingly enough, the syntax rules are always the same, as you need something like:

    Derived(int x1, int y1, int z1)
    

    instead.