Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 46
  1. #21
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Quote Originally Posted by myst View Post
    But line 9 is
    Java Code:
    f3 = s1;
    so shouldn't that treat f3 as a Second, since an instance of First can be assigned an instance of Second?
    No since f3 is declared as a First, it will always be treated as an instance of First even if it now holds an instance of a subclass.

  2. #22
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    No since f3 is declared as a First, it will always be treated as an instance of First even if it now holds an instance of a subclass.
    Oh, so what does

    Java Code:
    f3 = s1
    actually do to f3? Does it get different methods or attributes??

    ...
    15. s1 = f3;
    16. s1 = f4;
    17. System.out.println ("sum = " + s1.sum()); // and finally, why is this an error?
    Last edited by myst; 05-24-2010 at 10:26 PM.

  3. #23
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    It will treat s1 as a instance of First. What different methods or attributes would you expect it to get?

    The object will retain all the methods/attributes it currently has however you can only use the ones that are available to all First objects.

  4. #24
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by myst View Post
    But line 9 is
    Java Code:
    f3 = s1;
    so shouldn't that treat f3 as a Second, since an instance of First can be assigned an instance of Second?

    no, the type of f3 is still First. if you want assign object to another type object you have to cast it. here is a link Java Object Typecasting | Java Beginner which show some basic castings.

    you code could run with this casting

    Java Code:
    public class FooExample {
    	public static void main(String[] args) {
    		First f1 = new First(10);
    		System.out.println("sum1 = " + f1.sum());
    		System.out.println("count = " + First.getCount());
    		First f3 = new First(10, 20);
    		System.out.println("count = " + f3.getCount());
    		Second s1 = new Second(1);
    		First f4 = new Second(2);
    		System.out.println("count = " + f4.getCount());
    		f3 = s1;
    		System.out.println("sum = " + f3.sum());
    		((Second) f3).setNum3(2); // according to my main method, this is an
    									// error! if f3 is now in Second, why
    									// doesn't this work??
    		((Second) f4).setNum3(2); // this should be an error since f4 is in
    									// First
    		s1.setNum3(2); // this should be fine
    		System.out.println("sum = " + s1.sum()); // this should print sum of
    													// Second
    		s1 = (Second) f3; // now, s1 is still referring to Second since
    							// previously we wrote that f3 = s1
    		s1 = (Second) f4; // now s1 refers to First
    		System.out.println("sum = " + s1.sum()); // why is this an error? I
    													// would think it would
    													// print the sum of First.
    	}
    }

    and the output is:

    First constructor2
    First constructor1
    sum1 = 20
    count = 2
    First constructor2
    count = 3
    First constructor2
    First constructor1
    Second constructor1
    First constructor2
    First constructor1
    Second constructor1
    count = 7
    sum = 3
    sum = 4
    sum = 6

  5. #25
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    Thanks! You guys have been so helpful!

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Quote Originally Posted by StormyWaters View Post
    Another thing I'm noticing you are doing... You cannot do this
    Java Code:
    First one = new First();
    Second two = new Second();
    one = two;
    An object of class Second cannot be assigned an instance of class First.
    Yes it can.
    It's rather a fundamental part of OO that a child class can be assigned to a reference of a parent class.
    Second is a subclass of First...that and object of Second is an object of First as well.

  7. #27
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    Quote Originally Posted by Tolls View Post
    Yes it can.
    It's rather a fundamental part of OO that a child class can be assigned to a reference of a parent class.
    Second is a subclass of First...that and object of Second is an object of First as well.
    Oops, I did mix up the code part, however I stated it correctly. An object of class Second cannot be assigned an instance of class First. The code should have looked like this
    Java Code:
    First one = new First();
    Second two = new Second();
    two = one;

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by StormyWaters View Post
    Oops, I did mix up the code part, however I stated it correctly. An object of class Second cannot be assigned an instance of class First. The code should have looked like this
    Java Code:
    First one = new First();
    Second two = new Second();
    two = one;
    That should be:

    Java Code:
    one= two;
    i.e. you can assign a subclass to a reference of the super class but not vice versa; a human is an animal but not all animals are humans.

    kind regards,

    Jos

  9. #29
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Quote Originally Posted by StormyWaters View Post
    Oops, I did mix up the code part, however I stated it correctly. An object of class Second cannot be assigned an instance of class First. The code should have looked like this
    Java Code:
    First one = new First();
    Second two = new Second();
    two = one;
    Oops here as well.
    I read the code, and hadn't noticed your wording...:)

    Yes "two = one" doesn't work without casting.

  10. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Oops here as well.
    I read the code, and hadn't noticed your wording...:)

    Yes "two = one" doesn't work without casting.
    It doesn't work with casting either: a First will never become a Second.

    kind regards,

    Jos

  11. #31
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    It will if the First object was holding an instance of Second.

    Example:
    Java Code:
    First first = new Second();
    Second second = new Second();
    second = (Second)first;

  12. #32
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by StormyWaters View Post
    It will if the First object was holding an instance of Second.

    Example:
    Java Code:
    First first = new Second();
    Second second = new Second();
    second = (Second)first;
    Occam's razor please; you're showing a totally different example here (which indeed will work).

    kind regards,

    Jos

  13. #33
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Sorry, yes Jos.
    Brain was thinking of one thing which the example didn't reflect...

    ...and I had a shave this morning and all.

  14. #34
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Sorry, yes Jos.
    Brain was thinking of one thing which the example didn't reflect...

    ...and I had a shave this morning and all.
    Oh dear, shaving hurts and beards have their right to exist too!

    kind regards,

    Jos (<--- bearded, unshaven, dirty old hippy ;-)

  15. #35
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    My kids complain otherwise...and the less complaining from them the better.

  16. #36
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    My kids complain otherwise...and the less complaining from them the better.
    That's why my wife and I don't have kids (by choice), I mean I can hardly educate myself ;-)

    kind regards,

    Jos

  17. #37
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    We just let them run wild...

    I would send them down a pit, or up a chimney, but apparently there's child labour laws or something that prevent it. Oh, and not many pits or chimneys either.

  18. #38
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    We just let them run wild...

    I would send them down a pit, or up a chimney, but apparently there's child labour laws or something that prevent it. Oh, and not many pits or chimneys either.
    When I was a kid I had to work 29 hours per day; get out of bed 5 hours before I got in, walk uphill to the mill, both ways, work all day, no food, except a bit of gravel, but we were lucky then. Tell your spoiled brats that, but they won't believe you.

    kind regards,

    Jos

  19. #39
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    And I got stuck with a whole collection of encyclopedias, even though I specifically asked the man if he was packing any ecyclopedias. When he assured me he has just a burglar, I let him in, and the rest is history.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  20. #40
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by m00nchile View Post
    And I got stuck with a whole collection of encyclopedias, even though I specifically asked the man if he was packing any ecyclopedias. When he assured me he has just a burglar, I let him in, and the rest is history.
    Luxury! Pure luxury! We would've loved to have an encyclopaedia seller at the door, if we had a door. And we didn't have any food either; what does an encyclopaedia seller taste like?

    kind regards,

    Jos (lucky ;-)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2009, 09:33 PM
  2. Replies: 5
    Last Post: 02-25-2009, 08:14 PM
  3. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 11:59 PM
  4. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  5. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 07:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •