Question

I'm kind of a beginner at coding and I've been looking all over the internet and nothing really comes out and gives me an example on how to use the extend method and how to properly use the super() method. I'm kind of stuck with this and wondering if someone could show or give me a good example. My project was to Define a class Diploma and its subclass DiplomaWithHonors, so that some statements display their academic achievements. Thanks for your help! Okay now that I took a "stab" at the problem I have a error with the return statement if anyone can help with that...

     public class Diploma
      {
       public String fullName;
       public String course;
       int output = Diploma();

        public Diploma(String name, String info)
         {
         fullName = name;
         course = info;
         }
        public String toString()
         {
          return "This certifies that \n" + fullName + " \n has completed a course in " + course;
           }
        }

         public class DiplomaWithHonors extends Diploma
      {
        public DiplomaWithHonors( String name, String info ) 
      {
        super( name, info );
      }
       public String toString() 
      {
      return "This certifies that \n" + fullName + " \n has completed a course in " +   course+ "\n*** with honors ***";
      }
        }
      public String toString()
       {
        Diploma[] diplomas = new Diploma[2];
        diplomas[ 0 ] = new Diploma("Murray Smith", "Gardening");
        diplomas[ 1 ] = new DiplomaWithHonors("Lisa Smith", "Evolutionary Psychology");

        for( int i = 0; i< diplomas.length; i++) 
        {
         System.out.println( diplomas[ i ] );
        }
       }
        return output;
       }

P.S Sorry about the style malfunction, I kind of messed up on the copy paste.

Was it helpful?

Solution

super isn't a method; it's a keyword. Under certain contexts it may be called or used as a method, but it's really a keyword. The link above will give you tons of helpful info about that, as well as some tidbits on inheritance.

That aside, what it sounds like is that you have a straightforward relationship between two entities:

  • A Diploma should be the base entity that you're using; it holds information such as the recipient's name, their GPA, and what degree subject they're receiving it for.

  • A DiplomaWithHonors sounds like it's a different type of honor for the differing GPA.

A very primitive structure may look something like this. Assume fields are defined.

public class Diploma {
    public Diploma(String name, double GPA, String degreeField, DiplomaType diplomaType) {
        this.name = name;
        this.GPA = GPA;
        this.degreeField = degreeField;
        this.diplomaType = diplomaType;
    }
}

public class DiplomaWithHonors extends Diploma {
    public DiplomaWithHonors(String name, double GPA, String degreeField, DiplomaType diplomaType) {
        super(name, GPA, degreeField, diplomaType);
        calculateHonors();
     }
}

Notice that super in the context of a constructor has to be the first thing referenced inside of that constructor.

OTHER TIPS

see this example's toString() methods

import java.util.List;


public class Diploma {
    private String student;

    public String getStudent() {
        return student;
    }

    public void setStudent(String student) {
        this.student = student;
    }

    @Override
    public String toString() {
        return "Diploma [student=" + student + "]";
    }

    public static void main(String[] args){
        DiplomaWithHonors d1 = new DiplomaWithHonors();
        d1.setStudent("John");
        String[] achievements = {"Golden Medal","Silver Medal"};
        d1.setAchievements(achievements);

        System.out.println(d1);

        DiplomaWithoutAnyHonors d2 = new DiplomaWithoutAnyHonors();
        d2.setStudent("Doe");
        String[] homeworksDoneByStackOverflow = {"How to use super"};
        d2.setHomeworksDoneByStackOverflow(homeworksDoneByStackOverflow );

        System.out.println(d2);
    }
}

and

import java.util.Arrays;



public class DiplomaWithHonors extends Diploma {
    private String[] achievements;

    public String[] getAchievements() {
        return achievements;
    }

    public void setAchievements(String[] achievements) {
        this.achievements = achievements;
    }

    @Override
    public String toString() {
        return "DiplomaWithHonors [achievements=" + Arrays.asList(achievements)
                + ", toString()=" + super.toString() + "]";
    }


}

and

import java.util.Arrays;



public class DiplomaWithoutAnyHonors extends Diploma{
    private String[] homeworksDoneByStackOverflow;

    public String[] getHomeworksDoneByStackOverflow() {
        return homeworksDoneByStackOverflow;
    }

    public void setHomeworksDoneByStackOverflow(
            String[] homeworksDoneByStackOverflow) {
        this.homeworksDoneByStackOverflow = homeworksDoneByStackOverflow;
    }

    @Override
    public String toString() {
        return "DiplomaWithoutAnyHonors [homeworksDoneByStackOverflow="
                + Arrays.asList(homeworksDoneByStackOverflow) + ", toString()="
                + super.toString() + "]";
    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top