3
class Person {
  late String name;
  late int age;
  Person();
  Person.ap(this.name, this.age);
}

class Employee extends Person {
  Employee(String name, int age) : super.ap(name, age); // This works

  // This is giving error. The method 'ap' isn't defined in a superclass of 'Employee'.
  Employee(String name, int age) {
    super.ap(name, age);
  }
}

What's the difference of calling unnamed constructor using super or inside the parenthesis?

Manivannan
  • 1,630
  • 1
  • 11
  • 14
  • `super` can be used only in a constructor's initializer list. This has nothing to do with named vs. unnamed constructors. Also see https://stackoverflow.com/a/63319094/. – jamesdlin Feb 14 '22 at 08:42

2 Answers2

3

try this, just create a setter function named ap in the Person class


     class Person {
          late String name;
          late int age;
          Person();
          Person.ap(this.name, this.age);
          
          ap(String name, int age){ //add this           
              this.name=name;
              this.age=age;
            }
        }
        
        class Employee extends Person {

          //here ap is name constructor 
          Employee(String name, int age) : super.ap(name, age);
          
          //here ap is setter function in the person class
          Employee.ap(String name, int age) {
            super.ap(name, age);
          }
        }
narayann
  • 361
  • 3
  • 6
2

You are making two same-named constructors. Do something like this

class Employee extends Person {

Employee(String name, int age) : super.ap(name, age);

Employee.foo(String name, int age): super.ap(name, age);

}
aman
  • 205
  • 2
  • 4