1

I am trying to write the Undergrad class which extends Students

here is the base class (Student)

public class Student{

    private String name;
    private int id;
    private double gpa;

    public Student(){

    }

    public Student(int id, String name, double gpa){
        this.id = id;
        this.name = name;
        this.gpa = gpa;
    }

    public Student(int id, double gpa){
        this(id, "", gpa);
    }

    public String getName(){
        return name;
    }

    public int getId(){
        return id;
    }

    public double getGPA(){
        return gpa;
    }

    public void setName(String newName){
        this.name = newName;
    }

    @Override
    public String toString(){
        return "Student:\nID: " + id + "\nName: " + name + "\nGPA: " + gpa;
    }
}

and here the derived class (Undergrad)

public class Undergrad extends Student {

    private String year;

    public Undergrad (int id , String name ,double gpa,String year)
    {
        super(id,name , gpa);
        this.year =year;

    }

    @Override
    public String toString(){
        return super() + " the year is :" + year;
    }
}

the problem i'm facing is that eclipse shows that I have a mistake in toString method in class Undergrad exactly at the super() invoke the error says

"Syntax error on token "super", invalid Name"

could I find any help please ?

MrLore
  • 3,759
  • 2
  • 28
  • 36
iShaalan
  • 799
  • 2
  • 10
  • 30
  • 4
    You are looking for `super.toString()` assuming you want to call the super class implementation of the method. – Sotirios Delimanolis Sep 17 '13 at 18:14
  • 1
    When you write `super()`, it is actually like telling the compiler "please invoke the superclass constructor for me". If you just want a "pointer" to the superclass, you should use the idiom above, as suggested by @SotiriosDelimanolis. – Mauren Sep 17 '13 at 18:21
  • @Mauren In Java , Do we have pointers ? – iShaalan Sep 17 '13 at 18:24
  • 1
    @iShaalan not in the C sense, but yes, read this: http://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java – Mauren Sep 17 '13 at 18:30

3 Answers3

4

super() call is allowed only in constructors.
you need to call super.toString() if you want to call super class method.

public String toString()
{
      return super.toString() + " the year is :" + year;
 }  

This is the syntax for super keyword
1.for calling a superclass constructor is

  1. super(); //the superclass no-argument constructor is called
  2. super(parameter list);//the superclass constructor with a matching parameter list is called

2.for calling a superclass member is

  1. super.fieldName;//here member is field of super class
  2. super.methodName();//here member is method of super class
Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
2

When calling a superclass method, use the syntax super.methodName():

{return super.toString() + " the year is :" + year;}

This is different from calling a superclass constructor from within a subclass constructor; that syntax just needs the keyword super with no dot and no method name, as you already have it.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

super.toString() to call the super class method.

super() calls the super class constructor.

JNL
  • 4,683
  • 18
  • 29