-1

Is there a way on "Rewriting" a function.
Pseudo:

function a() {print "B"}
function a() {print "C"}

Output: C

Device
  • 574
  • 4
  • 19

2 Answers2

1

Overriding

class MyClass {
    public void myMethod () {
        System.out.println("MyClass");
    }
}

class MySubClass extends MyClass {
    @Override
    public void myMethod () {
        System.out.println("MySubClass");
    }

    public static void main (String[] args) {
        MyClass a = new MyClass();
        a.myMethod(); // "MyClass"
        MySubClass b = new MySubClass();
        b.myMethod(); // "MySubClass"
    }
}

In this example, MySubClass overrides the inherited method myMethod.

Overloading

class MyClass {
    public void myMethod () {
        System.out.println("myMethod");
    }

    public void myMethod (int i) {
        System.out.println(i * 2);
    }

    public void myMethod (String s) {
        System.out.println("Hello, " + s);
    }

    public static void main (String[] args) {
        MyClass a = new MyClass();
        a.myMethod(); // "myMethod"
        a.myMethod(33); // "66"
        a.myMethod("Jeremy") // "Hello, Jeremy"
    }
}

In this example, MyClass has multiple definitions of the method myMethod, but they accept different arguments.

Community
  • 1
  • 1
Hatchet
  • 5,320
  • 1
  • 30
  • 42
0

Simply rewrite the method in its subclass.

public class Something {
    public Something() {
    }
    public void printHi() {
        System.out.println("Hi");
    }
}

public class SomethingElse extends Something {
    public SomethingElse() {
    }
    public void printHi() {
        System.out.println("I refuse to say hi!");
    }
}

Something something = new Something();
something.printHi(); // prints Hi

SomethingElse somethingElse = new SomethingElse();
somethingElse.printHi(); // prints I refuse to say hi!
AMACB
  • 1,290
  • 2
  • 18
  • 26
  • Is it mandatory to use the annotation? I remember not having to do that when learning Java a few years ago. Is it a new release that requires it? – RaminS Jan 19 '16 at 22:54
  • Turns out you don't. Edited. – AMACB Jan 19 '16 at 22:59
  • 2
    @Gendarme [When do you use Java's @ Override annotation and why?](http://stackoverflow.com/q/94361). So even though is isn't mandatory you _should_ use it. So the last edit of this answer is a good example for how you _should not_ do it. – Tom Jan 19 '16 at 23:07
  • @Tom The reasons given in the answer in your link are fine I guess. But not important enough to impose a must-do practice of using that annotation. Using it to avoid typos does not make sense. And if a method has a hard parameter-list to keep track of it is probably badly written code. However, this discussion is off-topic so I will stop here. Your link is useful and appreciated, though. – RaminS Jan 19 '16 at 23:19
  • @Gendarme "However, this discussion is off-topic so I will stop here.": Since your arguments are kind of weak (I hope you're just very new to programming), I appreciate that. :). – Tom Jan 19 '16 at 23:23