1

What is the difference between the following declarations? And when are they used?

ParentClass child = new ChildClass();
ChildClass child = new ChildClass();

...if I already have this:

class ChildClass extends ParentClass {}
Wabbage
  • 437
  • 3
  • 6
  • 18
  • 1
    First instance will have access to child *and* parent methods, variables etc. Second instance will be a standalone instance and will only have access to its own methods, variables etc. – Drew Kennedy Mar 31 '16 at 17:32

1 Answers1

1

Lets say that you have a method foo() declared in ParentClass and a method bar() in ChildClass. Since ChildClass extends ParentClass - it also inherits the foo() method.

In the first case you wouldn't be able to call child.bar(); because ParentClass doesn't have a method called bar().

Same thing applies to variables, inner classes, etc. For further information, consult the Oracle documentation.

Dmitri Timofti
  • 2,428
  • 1
  • 22
  • 25