Hello. We're going into Constructora and super, using extends, subclasses etc. I have this code:

class Student {
 private String name;
 private String matric_no;
 public Student(String n, String m) {
 name = n;
 matric_no = m;
 }
}
class UndergradStudent extends Student {
 private String programme;
 public UndergradStudent(String n, String m, String p) {
 programme = p;
 super(n, m);
 }
}
class PostgradStudent extends Student {
 private String supervisor;
 public PostgradStudent(String s) {
 supervisor = s;
 }
}

and there is a compilation error in the subclasses. i understand that UndergradStudent inherits the private fields of Student but cannot do anything with it because its private, would that cause the error? Could someone explain why there is an error please? Thanks in advance

Read the error messages carefully. They'll tell you what the compiler is complaining about. In particular, the line 13 error:

error: call to super must be first statement in constructor

This was actually a blessing in disguise. It forces you to learn what's happening with the super constructor call. Had you put it as the first call in the function you would have been "fine". "Fine" is in quotes since, even if your code compiled and worked, you need to study up a little more on the super call. Glass half full type of thing.

As an experiment, create a Student() constructor and comment out all super calls. Your program compiles! it doesn't do what you WANT, but it compiles.

Now add a couple of debugging printouts in all of your constructors: "Inside Student(String, String) constructor" or something similar. See code below. Comment and uncomment the super calls. Comment and uncomment the Student() constructor. See what compiles and what does not. This should give you a good idea of how the constructors are called and in what order and how the super call works. Note that I changed its location to be the FIRST line in the constructor.

class Student {
 private String name;
 private String matric_no;
 public Student(String n, String m) {
 System.out.println("In Student(String, String)");
 name = n;
 matric_no = m;
 }

 public Student()
 {
     System.out.println("In Student()");
 }
}
class UndergradStudent extends Student {
 private String programme;
 public UndergradStudent(String n, String m, String p)
 {
     super(n, m);
     System.out.println("In UndergradStudent(String, String, String)");
     programme = p;
 }
}
class PostgradStudent extends Student {
 private String supervisor;
 public PostgradStudent(String s) {
 supervisor = s;
 }

 public static void main(String args[])
 {
     new UndergradStudent("a", "b", "c");
 }
}

Yes. private fields can only be accessed within the class where they are declared.
You need a less restrictive access type such as protected or default.
This tutorial expalins it better than I can:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

ps Lines 11/12... because a superclass's variables have to to be initialised before its subclass's variables are initialised, any call to a super constructor must be the first line of any subclass constructor

First of all read the error message carefully and step by step see program check your syntax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.