P
Peter Meier
I have a simple problem. I have a class A and a class B which extends
class A. Both classes have a protected method (e.g. 'print') with the
same signature (so B's method override A's method). If I have an
instance of class B which calls some public method of class A in which
the 'print' method is called then the JVM automatically calls B's
'print' method. How can I achieve that A's 'print' method is called? I
already tried it with the following:
'this.print();' or '((A)this).print();' but neither works. I never see
the print out 'Base class'. Could anyone help me (see code snippet
below). Thanks!
Peter
public class A {
public A() {
}
protected void print() {
System.out.println("Base class");
}
public void doSomething() {
print();
}
public static void main(String[] args) {
B b = new B();
b.doSomething();
}
}
class B extends A {
public B() {
}
protected void print() {
System.out.println("Sub class");
}
}
class A. Both classes have a protected method (e.g. 'print') with the
same signature (so B's method override A's method). If I have an
instance of class B which calls some public method of class A in which
the 'print' method is called then the JVM automatically calls B's
'print' method. How can I achieve that A's 'print' method is called? I
already tried it with the following:
'this.print();' or '((A)this).print();' but neither works. I never see
the print out 'Base class'. Could anyone help me (see code snippet
below). Thanks!
Peter
public class A {
public A() {
}
protected void print() {
System.out.println("Base class");
}
public void doSomething() {
print();
}
public static void main(String[] args) {
B b = new B();
b.doSomething();
}
}
class B extends A {
public B() {
}
protected void print() {
System.out.println("Sub class");
}
}