Java Inheritance and Method Overrides

I just find out about inheritance and am stuck in a simple problem. I want to redefine the print () operator in class one with a new subclass of two in it. I am not sure how I will access it, since it is a void operator 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 () will print

 Name: Jack Age: 34 

The desired result that I want is

 Name: Jack Age: 34 GPA: 3.20 

I think I need to use a super method, but I can’t find a way to enable it correctly. Any advice would be appreciated!

+4
source share
6 answers

The super keyword gives you access to the (visible) members of the superclass. That way, you can 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); } 

For more information on using super see this short section of the Java tutorials. Using the super keyword . With a simple example, he addresses the same problem that you asked for.

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() two method will call itself indefinitely until you get an OutOfMemoryError .

It's also worth noting that Vash mentioned @Override annotations in his answer .

+5
source

You have the right idea. Try the following:

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

In Java, every method is virtual, which means that you can override it with every available method. Using the available method, we can take a method that has the modifier public, protected or default.

From Java 1.6, it is recommended that you use the @Override annotation to mark methods that have been canceled. This helps the compiler and developer to prevent possible crashes.

To override the One#print() method, you just need to add a method to add a child class with the same signature .

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

A super call is optional when overriding.

It is nice to remember that you need to add a description of the method for which you want to override, and the method calls superclassical logic.

+5
source

it's as simple as calling the super () method of a 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()); } 
+2
source

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 to 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 when you see this: (i) Know what constructors are (ii) Access modifiers, access levels

+1
source

How do you override the print () method in the second class, and you can write in like System.out.println("GPA: " + gpa);

0
source

All Articles