Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java overriding not working

Tags:

java

I'm a beginner in Java, I used PHP, C++ and Lua and never had this problem, I made two classes just for exercising's sake Facto and MyFacto, first one does find a factorial and the second one should find factorial not by adding, but by multiplying. Don't blame me for the stupid and pointless code, I am just testing and trying to get the hang of Java.

Main:

public class HelloWorld {
public static void main(String[] args) {

    Facto fc = new Facto(5);
    fc.calc();
    System.out.println(fc.get());   

    MyFacto mfc = new MyFacto(5);
    mfc.calc();
    System.out.println(mfc.get());

}
}

Facto.java:

public class Facto {
private int i;
private int res;

public Facto(int i) {
    this.i = i;
}

public void set(int i) {
    this.i = i;
}

public int get() {
    return this.res;
}

public void calc() {
    this.res = this.run(this.i);
}

private int run(int x) {
    int temp = 0;
    if(x>0) {
        temp = x + this.run(x-1);
    }
    return temp;
}
}

MyFacto.java:

public class MyFacto extends Facto {
public MyFacto(int i) {
    super(i);
}

private int run(int x) {
    int temp = 0;
    if(x>0) {
        temp = x * this.run(x-1);
    }
    return temp;
}
}

I thought the result should be 15 and 120, but I get 15 and 15. Why is that happening? Does it have something to do with calc() method not being overriden and it uses the run() method from the Facto class? How can I fix this or what is the right way to override something like this?

like image 603
Sergey Telshevsky Avatar asked Mar 28 '26 03:03

Sergey Telshevsky


2 Answers

The reason you're running into issues is due to member access visibility.

In a nutshell:

  • public allows any Java class to see the field/function, so long as it can be reached.
  • <package>, or no apparent modifier, allows any Java object (but not subclasses) to see the field/function, as long as they're in the same directory, or package.
  • protected allows the declared class and all other subclasses to access that field/function, as well as any class in the same directory/package.
  • private allows only the declared class to access that field/function.
like image 157
Makoto Avatar answered Mar 29 '26 18:03

Makoto


To expand on what @Makoto said, you're running into an issue because the calc() method of Facto does not have access to the run() method of MyFacto, so it's using it's own run() method. Changing them both to protected instead of private should do the trick.

Also, something you should probably learn to use is the @Override annotation. It's good practice to put it above any method that you are overriding. That way, if you misspell something, or the parameters don't match, you will get a warning. Also, it makes it clear to you and/or the reader. For example:

MyFacto.java#run:

@Override
protected int run(int x) {
    int temp = 0;
    if(x>0) {
        temp = x * this.run(x-1);
    }
    return temp;
}

Good luck with Java!

like image 20
lachy2901 Avatar answered Mar 29 '26 19:03

lachy2901



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!