Basic Question, please help.
Hi gurus,
I am new to Java. Can anybody explain me the difference between line numbers 18 & 19 in the following code and the purpose of each line.
Thanks in Advance,
01 class StdGreeting {
02 String intro = "hello";
03 String target() {
04return "Hello world";
05}
06 }
07
08 class FrenchGreeting extends StdGreeting {
09 String intro = "bonjour";
10 String target() {
11return "le monde";
12}
13 }
14
15 public class MethodVsFieldInheritanceTST {
16
17public static void main(String[] args) {
18StdGreeting g = new StdGreeting();
19StdGreeting f = new FrenchGreeting();
20}
> Hi gurus,
>
> I am new to Java. Can anybody explain me the
> difference between line numbers 18 & 19 in the
> following code and the purpose of each line.
>
> Thanks in Advance,
>
> 01 class StdGreeting {
> 02 String intro = "hello";
> 03 String target() {
> 04return "Hello world";
> 05}
> 06 }
> 07
> 08 class FrenchGreeting extends StdGreeting {
> 09 String intro = "bonjour";
> 10 String target() {
> 11return "le monde";
> 12}
> 13 }
> 14
> 15 public class MethodVsFieldInheritanceTST {
> 16
> 17public static void main(String[] args) {
> 18StdGreeting g = new StdGreeting();
> 19StdGreeting f = new FrenchGreeting();
> 20}
Hi - I am still not a guru, still learning and exploring Java like you but I have
an idea that might help:
Lines 18 and 19 basically means that you're creating two objects (g and f) of
the same type (StdGreeting) but with different defined attributes and
methods. The same because FrenchGreeting is also a StandardGreeting
(termed as a subclass which inherits or can extend the attributes of the
base class). But with different attributes and methods because
FrenchGrreeting overrides(or redefined) the attributes and method it
inherited from the base class - Standard Greeting.
Did that help? Hope it did... :)