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 OverflowVideos
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.
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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Overridden method is called using dynamic binding in Java at runtime based upon type of Object.
- 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.
- 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.
Ok, from what I can see, you have 2 main questions here.
- What does it mean to override a method?
- 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.
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.
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.
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.
Override means that you change the functionality of a method (you overwrite what that method does).
Overload means that you keep the method name and return type , but you have different input paremeters.
The two concepts are very different.
While in override you have a different functionality for the SAME method, with overload you have two DIFFERENT methods with the SAME name .
!EDIT
You can overload a method to have a different return type only if you have different input parameters. You cannot overload a method by only changing it's return type.
When you overload a method, you allow the method to be invoked through different signature..
For example
add(int a, int b);
add(int a, int b, int c);
add(int a, int b, int c, int d);
As you see here, all the three overloads are having different signatures..
However, when you over-ride a method, You must have the same signature as the method you are trying to override..
Hence overloading requires same name whereas over-riding requires same signature, but different functioning..