I made a small toy program to replicate a problem I am having in a larger project. In the following code, class A
is a parent class from which class B
inherits. Class B
adds a new method called PrintStuff
. In my main method I am declaring class B
by providing its superclass. I need to do this for reasons related to the real use case:
package com.company;
import java.lang.String;
class A {
String x;
A(String value) {
x = value;
}
}
class B extends A {
B(String value) {
super(value);
}
void printStuff() {
System.out.println(x);
}
}
public class test {
public static void main(String[] args) {
A Binstance = new B("B instance");
Binstance.printStuff();
}
}
However the program does not compile due to the following:
C:\Users\me\Desktop\Library\src\com\company\test.java:28:18
java: cannot find symbol
symbol: method printStuff()
location: variable Binstance of type com.company.
Why is Java not able to recognize that in reality I am instantiating an object of class B
? How can I get the compiler to recognize that a method printStuff
in fact exists?