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!

1
  • example.print(); what is reference/object type of example ? Commented Apr 3, 2013 at 10:21

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

You've got the right idea. Try this:

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

2 Comments

Sorry, I don't understand that comment or the useless one that was added after the code above.
Sorry my mistake, made a typo which caused compiler errors and thought i wasn't explaining it thoroughly. Thanks...
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.

Comments

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());
}

Comments

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

3 Comments

Anuj Balan :- gather information about super. Head first Core java is best book ever to learn core java. Read it ASAP. Don't miss it.
You can do that for the OP.
:-) ignore my comment if you know it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.