Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Declare question

Thread view: 
majorone - 23 Feb 2005 08:54 GMT
Hello guys, I am wondering what does following statement mean, please
help me understand, thanks you!

First first = new Second();

The entire source code is here:

public class Main {
   public static void main(String[] args) {
       First first = new Second();        
   }
}

class First{
   void printMe(){
       System.out.println("First Hello!");
   }    
}

class Second extends First{
   void printMe(){
       System.out.println("Second Hello");
   }
}
Roland - 23 Feb 2005 09:29 GMT
> Hello guys, I am wondering what does following statement mean, please
> help me understand, thanks you!
>
> First first = new Second();

It assigns a newly created instance of class Second to a variable named
first, which happens to be of type First.
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

majorone - 23 Feb 2005 16:31 GMT
> > Hello guys, I am wondering what does following statement mean, please
> > help me understand, thanks you!
[quoted text clipped - 3 lines]
> It assigns a newly created instance of class Second to a variable named
> first, which happens to be of type First.

so what is the different of myVar between the 2 statements, thanks for answering:

Second myVar = new Second();
First  myVar = new Second();
Stefan Schulz - 23 Feb 2005 17:35 GMT
> so what is the different of myVar between the 2 statements, thanks for
> answering:
>
> Second myVar = new Second();
> First  myVar = new Second();

The first declaration declares a variable of type First, and assigns it a
reference to a newly created object of type Second. The second statement
declares a variable of type second, and assigns it a reference to a newly
allocated object of type Second.

Signature

In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
          --- Rear Admiral Grace Murray Hopper

Oscar kind - 23 Feb 2005 17:52 GMT
> so what is the different of myVar between the 2 statements, thanks for answering:

[renamed variables]
> Second myVar1 = new Second();
> First  myVar2 = new Second();

myVar1 is an instance of Second, but your code sees it as an instance of
First (it's superclass), as that is the type of the reference. You can
therefore only use it as an instance of First, unless you cast it to a
Second.

myVar2 is also an instance of Second, and your code sees it as such. You
can therefore use it as an instance of Second.

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Big Jim - 23 Feb 2005 22:34 GMT
It might help your understanding if you look up some introductory
information on inheritance and polymorphism, these subjects are the basis of
your question but are a bit too large to go into here without a basic idea
to start from.

>> > Hello guys, I am wondering what does following statement mean, please
>> > help me understand, thanks you!
[quoted text clipped - 9 lines]
> Second myVar = new Second();
> First  myVar = new Second();
dar7yl - 28 Feb 2005 05:38 GMT
>> > Hello guys, I am wondering what does following statement mean, please
>> > help me understand, thanks you!
[quoted text clipped - 9 lines]
> Second myVar = new Second();
> First  myVar = new Second();

For those purists here, you should be aware that
these statements are in error, as you are redeclaring
myVar and would generate a compiler error.

regards,
   Dar7yl


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.