So you just want the overridden version to use a different delimiter pattern for the Scanner? I suggest taking a look at the Scanner API as it documents how to use a different delimiter pattern.

public boolean openRead() throws FileNotFoundException
{
    boolean result = super.openRead();
    sc.useDelimiter(DELIMITERS);
    return result;
}

edit

Or perhaps you just don't know what overriding is in Java, and for that you should read more in the Java tutorials.

But essentially, if you had some class:

public class ScannerUserParent
{
    protected Scanner sc;
    private String filename = null;

    // all that other stuff like constructors...

    public boolean openRead() throws FileNotFoundException
    {
        sc = new Scanner(new File(fileName));

        if (fileName != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Then you subclass that class (or extends):

public class ScannerUserChild extends ScannerUserParent
{
    protected final String DELIMITERS = "[\\s[^'a-zA-Z]]";

    // stuff like constructors...

    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object

        sc.useDelimiter(DELIMITERS);
        return result;
    }
}

However, there are other things that can prevent you from doing exactly this. For example, if the sc member was private scope, then the subclass could not use it directly in the manner I have shown.

In my example, sc uses protected access, so the class and its subclasses can use it.

In case there's a private Scanner, and assuming the parent has a getScanner() method that returns you the Scanner, you could do this:

    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object
        Scanner sc = getScanner();
        sc.useDelimiter(DELIMITERS);
        return result;
    }
Answer from wkl on Stack Overflow
Top answer
1 of 1
1

So you just want the overridden version to use a different delimiter pattern for the Scanner? I suggest taking a look at the Scanner API as it documents how to use a different delimiter pattern.

public boolean openRead() throws FileNotFoundException
{
    boolean result = super.openRead();
    sc.useDelimiter(DELIMITERS);
    return result;
}

edit

Or perhaps you just don't know what overriding is in Java, and for that you should read more in the Java tutorials.

But essentially, if you had some class:

public class ScannerUserParent
{
    protected Scanner sc;
    private String filename = null;

    // all that other stuff like constructors...

    public boolean openRead() throws FileNotFoundException
    {
        sc = new Scanner(new File(fileName));

        if (fileName != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Then you subclass that class (or extends):

public class ScannerUserChild extends ScannerUserParent
{
    protected final String DELIMITERS = "[\\s[^'a-zA-Z]]";

    // stuff like constructors...

    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object

        sc.useDelimiter(DELIMITERS);
        return result;
    }
}

However, there are other things that can prevent you from doing exactly this. For example, if the sc member was private scope, then the subclass could not use it directly in the manner I have shown.

In my example, sc uses protected access, so the class and its subclasses can use it.

In case there's a private Scanner, and assuming the parent has a getScanner() method that returns you the Scanner, you could do this:

    public boolean openRead() throws FileNotFoundException
    {
        boolean result = super.openRead(); // we are calling the parent's openRead() method to set up the Scanner sc object
        Scanner sc = getScanner();
        sc.useDelimiter(DELIMITERS);
        return result;
    }
🌐
GeeksforGeeks
geeksforgeeks.org › java › overriding-in-java
Overriding in Java - GeeksforGeeks
The overriding method cannot throw new or broader checked exceptions than the method in the superclass. It can throw fewer or narrower checked exceptions. It can throw any unchecked exceptions (like RuntimeException) regardless of the superclass ...
Published   October 14, 2025
🌐
Programiz
programiz.com › java-programming › method-overriding
Java Method Overriding
In this tutorial, we will learn about method overriding in Java with the help of examples. If the same method defined in both the superclass class and the subclass class, then the method of the subclass class overrides the method of the superclass. This is known as method overriding.
🌐
Medium
medium.com › @nakulmitra2114 › method-overriding-in-java-cd3fcea6bd82
Method Overriding in Java. Method overriding is a fundamental… | by Nakul Mitra | Medium
1 month ago - To override a method in Java, we simply declare the method in the subclass with the same signature as in the superclass. We can use the @Override annotation to inform the compiler that you intend to override the method, which provides error ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_overriding.htm
Java - Overriding
Method overriding allows us to ... and is used for writing specific definitions of a subclass method that is already defined in the superclass. The method is superclass and overridden method in the subclass should have the same declaration signature ...
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › method-overriding-in-java-with-example
Method overriding in java with example
Overriding is done so that a child ... and the method in child class is called overriding method. In this guide, we will see what is method overriding in Java and why we use it....
🌐
Simplilearn
simplilearn.com › home › resources › software development › method overriding in java: essential rules and examples
Method Overriding in Java: Essential Rules and Examples
June 9, 2025 - Discover the key rules and examples of method overriding in Java, a crucial concept for achieving polymorphism and dynamic process execution.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Tpoint Tech
tpointtech.com › method-overriding-in-java
Method Overriding in Java - Tpoint Tech
February 11, 2026 - This illustrates how polymorphic behaviour dependent on the type of object at runtime is made possible by method overriding, which enables each subclass to give its own implementation of a method inherited from the superclass. Static methods cannot be overridden in Java.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 50828226 › print-with-category-in-java-using-method-overriding-in-java
arrays - Print with category in java using method overriding in java - Stack Overflow
Based upon the output, you might want a method like this: @Override public String toString() { return studentType + " name: " + name + " age: " + age + ", roll: " + rollno; } Now we can take a look at the public static void main(String args[]) function. We have a few things we need to do here. Since it appears we do not know the number of students we are going to be adding, we should use an ArrayList. We should also instantiate our scanner here as well: ArrayList<Student> students = new ArrayList<Student>(); Scanner scan = new Scanner(System.in); ...
Top answer
1 of 6
14

To answer the question, which is specifically how overriding is implemented in the virtual machine, there's a write up available in Programming for the Java Virtual Machine (Google Books link).

The VM will look for an appropriate method definition in the referenced class, and then work its way up through the inheritance stack. Obviously at some stage various optimisations will apply.

See here for a description of the relevant bytecode instruction invokevirtual:

invokevirtual looks at the descriptor given in , and determines how many arguments the method takes (this may be zero). It pops these arguments off the operand stack. Next it pops objectref off the stack. objectref is a reference to the object whose method is being called. invokevirtual retrieves the Java class for objectref, and searches the list of methods defined by that class and then its superclasses, looking for a method called methodname, whose descriptor is descriptor.

As gustafc has highlighted below, various optimisations can apply, and no doubt the JIT will introduce further.

2 of 6
1

Method overriding in Java is a concept based on polymorphism OOPS concept which allows programmer to create two methods with same name and method signature on interface and its various implementation and actual method is called at runtime depending upon type of object at runtime. Method overriding allows you to write flexible and extensible code in Java because you can introduce new functionality with minimal code change.

There are few rules which needs to be followed while overriding any method in Java, failure to follow these rules result in compile time error in Java.

  1. First and most important rule regarding method overriding in Java is that you can only override method in sub class. You can not override method in same class.
  2. Second important rule of method overriding in Java that name and signature of method must be same in Super class and Sub class or in interface and its implementation.
  3. Third rule to override method in Java is that overriding method can not reduce accessibility of overridden method in Java. For example if overridden method is public than overriding method can not be protected, private or package-private; But opposite is true overriding method can increase accessibility of method in Java, i.e. if overridden method is protected than overriding method can be protected or public.
  4. Another worth noting rule of method overriding in Java is that overriding method can not throw checked Exception which is higher in hierarchy than overridden method. Which means if overridden method throws IOException than overriding method can not throw java.lang.Exception in its throws clause because java.lang.Exception comes higher than IOException in Exception hierarchy. This rule doesn't apply to RuntimeException in Java, which is not even need to be declared in throws clause in Java.
  5. You can not override private, static and final method in Java. private and static method are bonded during compile time using static binding in Java and doesn't resolve during runtime. overriding final method in Java is compile time error. Though private and static method can be hidden if you declare another method with same and signature in sub class.
  6. Overridden method is called using dynamic binding in Java at runtime based upon type of Object.
  7. If you are extending abstract class or implementing interface than you need to override all abstract method unless your class is not abstract. abstract method can only be used by using method overriding.
  8. Always use @Override annotation while overriding method in Java. Though this is not rule but its one of the best Java coding practice to follow. From Java 6 you can use @Override annotation on method inherited from interface as well.
🌐
Vlabs
java-iitd.vlabs.ac.in › exp › method-overriding › theory.html
Method Overriding in Java - Virtual Labs
In this example, we have defined the run method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding · //Java Program to illustrate the use of Java Method Overriding //Creating a parent class.
🌐
Unstop
unstop.com › home › blog › method overriding in java | rules, use-cases & more (+codes)
Method Overriding In Java | Rules, Use-Cases & More (+Codes)
November 11, 2024 - In technical terms, you inherit a method from a superclass (like the recipe) but redefine it in your subclass (your own version) to suit specific needs. In this article, we will discuss method overriding in Java in detail, including its rules, implementations, use cases, pitfalls, and more, with proper code examples.
🌐
Educative
educative.io › answers › what-is-method-overriding-in-java
What is method overriding in Java?
To accomplish Java runtime polymorphism, overriding methods are utilized. The object that is used to call the method determines the version of the method that is being run.
🌐
Coding Shuttle
codingshuttle.com › home › handbooks › java programming handbook › method overriding in java
Method Overriding in Java | Coding Shuttle
April 9, 2025 - In this blog, we learned about Method Overriding in Java, its importance in achieving runtime polymorphism, and the rules associated with it. We also explored examples demonstrating overriding and how to use super to call the parent class method.
Top answer
1 of 5
4

Ok, from what I can see, you have 2 main questions here.

  1. What does it mean to override a method?
  2. How to I override a method?

Lets think up a new class, so that I can avoid doing your homework for you.

We have a class called Student, that stores 3 Strings.

  • First Name
  • Last Name
  • GPA

It might look something like this.

public class Student {
    String firstName;
    String lastName;
    String gpa

    public Student(String firstName, String lastName, String gpa){
        this.firstName = firstName;
        this.lastName = lastName;
        this.gpa = gpa;
    }

    // Perhaps some methods here

    public String getIdentity(){
        return this.lastName + ", " + this.firstName;
    }
}

You like the Student class, but you decide that it would be much better if you could keep track of a College studentId as well. One solution would be to extend the Student class.

By extending the class, we get access all of the methods (and constructors) that Student had, and we get to add some of our own. So lets call our new class CollegeStudent, and add a String for the studentId.

public class CollegeStudent extends Student{

    String studentId;

}

Now, with no additional work, I can create a CollegeStudent, and get it's identity.

// Code
CollegeStudent myCollegeStudent = new CollegeStudent("Dennis", "Ritchie", "2.2");
String identity = myCollegeStudent.getIdentity();
// Code

Ok, enough setup. Lets answer your questions.

So lets assume that instead of returning "Ritchie, Dennis", you would prefer getIdentity() to return the "studentId" for that college student. You could then override the getIdentity method.

By overriding the method, you will get to re-write getIdentity() so that it returns the studentId. The CollegeStudent class would look something like this.

public class CollegeStudent extends Student{

    String studentId;

    @Override
    public String getIdentity(){
        return this.studentId;
    }
}

To override a method, you must return the same type of object (String in our example), and you must accept the same parameters (our example did not accept any parameters)

You can also override constructors.

So how does this apply to your assignment? .equals(), .compareTo(), and .toString() are already defined because of the classes that you will be extending (such as Object). However you will want to re-define or override those methods so that they will be useful for your class. Perhaps your implementation of toString() will return the word "four" instead of "IV". Probably not, but the point is that you now have the freedom to decide how the method should be coded.

Hope this helps.

2 of 5
2

When one class extends another, it inherits all of its non-private instance members.

For example, RomanNumeral extends Object, so it inherits the latter's toString() method. This means that you can call toString() on any RomanNumeral instance, and it will call the Object.toString() method.

However, RomanNumeral can choose to override this method by providing its own definition, that will supersede the one it inherited. Then, when you call toString() on a RomanNumeral instance, it will call the method that you defined. (This works even if you're referring to the RomanNumeral instance via an Object reference.)

So, you are being asked you to flesh out this rubric:

public class RomanNumeral {

    // ... your other methods ...

    @Override       // this line is optional
    public String toString() {
        // TODO define this method
    }

    @Override       // this line is optional
    public boolean equals(Object o) { // note: 'Object', *not* 'RomanNumeral'!
        // TODO define this method
    }

    @Override       // this line is optional
    public int hashCode() {
        // TODO define this method
    }

}

Note that, despite what you seem to have been told, there is no such thing as an "override file". The above methods are defined inside RomanNumeral.java, just like any other method.

🌐
Refreshjava
refreshjava.com › java › method-overriding-in-java
Method Overriding in Java with Example - RefreshJava
Many times in real world projects when we create a more specific class from a generic class, we need to use method overriding to change the behavior of generic class method. For example let's suppose you have an Animal class having a method as sound, now if you create classes like Dog and Cat from Animal class, you would be needed to change/override the behavior of sound method in Dog and Cat classes since a dog barks whereas a cat meows. There are many java classes where method overriding is used, some of the frequently used classes like StringBuffer, StringBuilder, ArrayList, HashMap etc has many overridden methods.
🌐
Medium
medium.com › @raghunathchavva › method-overriding-in-java-5b8a0f71231b
Method Overriding In Java. The process of replacing existed method… | by Raghu Chavva | Medium
July 18, 2023 - Method Overriding In Java The process of replacing existed method functionality with some new functionality is called as Method Overriding. To perform Method Overriding, we must have inheritance …
Top answer
1 of 4
2

If you make a new method bark for your Dog class instead of overriding speak, then you also have to add code to detect that youre dealing with a dog, then cast it to a Dog type, then call bark. You get a program that has a bunch of very specific code that has to handle each concrete type individually, and the workflow has to be updated every time you add a new subclass. There is no abstraction or generality.

The idea behind polymorphism is that your program shouldn't have to worry about which objects are dogs and which are cats, it just handles animals, and lets the subclasses take care of the details for themselves. The program stays at a high level and tells the animals to speak, the specific animals' subclasses decide whether to bark or meow. So if you introduce a new animal subclass the code handling animals doesn't change.

2 of 4
0

You override a method when you have a class hierarchy, as you do, where there is a sensible default behaviour which many of your subclasses will want to use, but some subclasses need different behaviour and you want to call the method using a reference of the base class.

If I had code like:

Dog d = new Dog();
d.speak();

Then just implementing bark() would be fine, because d is always a Dog. But when I have code like this:

Animal a;
if (...) {
 a = new Dog();
} else {
 a = new Cat();
}
a.speak();

Then we have to override a method. If the subclasses had a new method (like your bark()) which wasn't present on Animal we wouldn't be able to call it, because a is an Animal -- the actual object it refers to might be a Dog or a Cat.

🌐
Oracle
docs.oracle.com › javase › tutorial › java › IandI › override.html
Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type. When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass.