Hi there,
I'm going over access modifiers in Java from this website and noticed that the following output is displayed if you run the snippet of code.
Cookie Class
import javax.swing.*; public class Cookie { public Cookie() { System.out.println("Cookie constructor"); } protected void foo() { System.out.println("foo"); } }
ChocolateChip class
public class ChocolateChip extends Cookie { public ChocolateChip() { System.out.println("ChocolateChip constructor"); } public static void main(String[] args) { ChocolateChip x = new ChocolateChip(); x.foo(); } }
Output:
Cookie constructor ChocolateChip constructor foo
I've been told that constructors are never inherited in Java, so why is "Cookie constructor" still in the output? I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?
If you can shed any light on this, I would greatly appreciate it.
Many thanks!