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}

[719 byte] By [Chooti_Babaa] at [2007-11-26 19:22:38]
# 1
do you know what a new operator is suppose to do?it creates an object Stdgreeting of g
lrngjavaa at 2007-7-9 21:43:28 > top of Java-index,Java Essentials,New To Java...
# 2

> 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... :)

lem@phila at 2007-7-9 21:43:28 > top of Java-index,Java Essentials,New To Java...