4

I'm just learning about inheritence and am stuck on a simple problem. I want to override the print() statement in the one class with a new one in it's subclass two. I'm not sure how I would approach it as it is a void statement with no parameters.

  public class One {

    private String name;
    private int age;

    public human(String n, int a)
      name = n;
      age = a;
    }
    public void print() {

     System.out.println("Name: " + name);
     System.out.println("Age: " + age);

    }

Second class:

    public class Two extends One {

    private double gpa;

    public two(String n, int a, double g) {
      super(n,a);
      gpa = g;

    public double getGPA (){
          return gpa;
       }

    public void print() {
    // override code here to include code from class one + displaying GPA..

     }
}

So for example, example.print() would print

Name: Jack
Age: 34

The desired output I want is

Name: Jack
Age: 34
GPA: 3.20

I am thinking I have to use the super method but cannot find a way to incorporate it correctly. Any tips would be appreciated!

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
aiuna
  • 111
  • 1
  • 2
  • 10

6 Answers6

5

You've got the right idea. Try this:

public void print() {
    super.print();
    System.out.println("GPA: " + this.gpa);
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Sorry, I don't understand that comment or the useless one that was added after the code above. – duffymo Apr 03 '13 at 10:01
  • Sorry my mistake, made a typo which caused compiler errors and thought i wasn't explaining it thoroughly. Thanks... – aiuna Apr 03 '13 at 10:05
5

The keyword super gives you access to (visible) members of the superclass. So, you could call the print() method of the superclass, and then just do the specific work of the subclass:

public void print() {
    super.print();
    System.out.println ("GPA: " + gpa);
}

See this brief section of the Java Tutorials for more on the usage of super: Using the Keyword super. It does address with a simple example the very same problem you were asking about.

Please note that if you wrote

public void print() {
    print();
    System.out.println ("GPA: " + gpa);
}

Without specifying that you want to call super.print(), the print() method of two would be calling itself indefinitely until you got an OutOfMemoryError.

It is also worth noting what Vash mentioned about the @Override annotation in his answer.

Community
  • 1
  • 1
Xavi López
  • 27,550
  • 11
  • 97
  • 161
5

In Java every method is virtual, this mean that you can override it each accessible method. By accessible method we can take a method that has modifier public, protected or default.

From Java 1.6, it is recommended to use annotation @Override, to mark the methods that has been override. This help the compiler and developer to prevent potential faults.

So to override method One#print() you just have to in child class add method with same signature.

@Override
public void print() {
    super.print(); //Here you call the functionality from super class.
    System.out.println("GPA: %f", getGPA());
}

Calling super is not mandatory when overriding.

Nice thing to remember is to add description to method why override was required and do the method call the super class logic.

Community
  • 1
  • 1
2

it is as easy as calling the super() of the method

public class one {

private String name;
private int age;

public human(String n, int a);
name = n;
age = a;

public void print() {

System.out.println ("Name: " + name);
System.out.println ("Age: " + age);
Second class:

public class two extends one {

private double gpa;

public two(String n, int a, double g) {
super(n,a);
gpa = g;

public double getGPA (){
      return gpa;
   }

public void print() {
 super.print();
 System.out.println("GPA: " + getGPA());
}
Edo Post
  • 727
  • 7
  • 18
1

Your One.java

public class One {

protected String name;
protected int age;

public One(String n, int a)
{
name = n;
age = a;
}
public void print() {
System.out.println ("Name: " + name);
System.out.println ("Age: " + age);
}
}

Two.java

public class Two extends One{
private double gpa;

public Two(String n, int a, double g) {
super(n,a);
gpa = g;
}
public double getGPA (){
      return gpa;
   }

public void print() {
    System.out.println ("Name: " + name);
    System.out.println ("Age: " + age);
    System.out.println ("gpa: " + gpa);
}
}

Add this in some java file to check the output.

public class ExecuteTwo {
public static void main(String[] args) {
    Two two = new Two("Aiuna",1,100.0);
    two.print();
}
}

This will give you what you want.

What you should do after seeing this: (i) Know what are constructors (ii) Access modifiers, access levels

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
0

As you are overriding print() method in class two and you can write in as System.out.println("GPA: " + gpa);

kkh
  • 41
  • 4