J
Johannes Schaub (litb)
I heard today that in Java you cannot override private methods. I'm
wondering about this snippet:
class Printer {
public void print() {
preparePrint();
doPrint();
finalizePrint();
}
private abstract void doPrint();
};
class LaserPrinter extends Printer {
private void doPrint() {
// print stuff
}
};
I wonder why i can't write it like this. Users of "Printer" and derived
classes of Printer are not supposed to call "doPrint" directly, so i would
like to make it private.
I heard from people that do C++ that it is a good idea to make virtual
methods private and public functions non-virtual (non-virtual-interface).
What is the reason Java doesn't follow this path? Thanks in advance for all
answers!
wondering about this snippet:
class Printer {
public void print() {
preparePrint();
doPrint();
finalizePrint();
}
private abstract void doPrint();
};
class LaserPrinter extends Printer {
private void doPrint() {
// print stuff
}
};
I wonder why i can't write it like this. Users of "Printer" and derived
classes of Printer are not supposed to call "doPrint" directly, so i would
like to make it private.
I heard from people that do C++ that it is a good idea to make virtual
methods private and public functions non-virtual (non-virtual-interface).
What is the reason Java doesn't follow this path? Thanks in advance for all
answers!