Page 1 of 3 123 LastLast
Results 1 to 20 of 46
  1. #1
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default Question involving Polymorphism, inherticance, casting...

    *edit

    edit edit edit
    Last edited by myst; 06-01-2010 at 03:54 AM.

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

    Default

    Quote Originally Posted by myst View Post
    You have to say if each line runs correctly
    Do you want us to answer your homework questions? Do we get the grades?

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by myst View Post
    maybe someone can check it over or just give me some hints. Thanks.
    I think they're all correct (I didn't read the questions too closely though).

    kind regards,

    Jos

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

    Default

    Thanks. I edited the thread in a much better way...

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by myst View Post
    *edit
    1. First f1 = new First (10); // why does this print both constructors?? Shouldn't it only use the first constructor since there's only one formal parameter?
    2. System.out.println ("sum1 = " + f1.sum());
    3. System.out.println ("count = " + First.getCount());
    4. First f3 = new First (10, 20);
    5. System.out.println ("count = " + f3.getCount()); //what's the dif between First.getCount (2 lines above) and this...
    6. Second s1 = new Second (1);
    7. First f4 = new Second (2); // how can you do this??
    Thanks. (I think this is a better way of doing things...)
    1. Look at the first constructor? Follow its logic, does it go anywhere?

    5. Reread the information on static variables(Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)) but basically static variables go across instances, meaning calls to it from any instance will update it for all instances. The final modifier is used for constants.

    7. You just did :p, but basically because since the Second class extends the First class, it can be treated as a instance of the class First. However, the methods that are specific to the Second class cannot be called on an instance of the First class, unless you typecast it first.

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

    Default

    1. Look at the first constructor? Follow its logic, does it go anywhere?

    5. Reread the information on static variables(Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)) but basically static variables go across instances, meaning calls to it from any instance will update it for all instances. The final modifier is used for constants.

    7. You just did , but basically because since the Second class extends the First class, it can be treated as a instance of the class First. However, the methods that are specific to the Second class cannot be called on an instance of the First class, unless you typecast it first.
    1. I don't understand it's logic. Where does it go?

    5. Ok, I think I understand. getCount is a static method. It is better to be referenced with the class name, but it may be referenced with an object.

    7. Well...since the Second extends First, Second inherits all of First methods, plus it has it's own. So, Second has all of First but First doesn't have all of Second. So is this what it means:
    s1 is of type Second but it only gets the methods of First??

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Java Code:
    (snip)
              this (num, num); [COLOR="Red"] // what does that do/mean?[/COLOR]
    (snip)
         public First(int num1, int num2){
              _num1 = num1;
              _num2 = num2;
              _count++;
              System.out.println ("First constructor2");
         }
    that call (this(num, num)), is calling the other constructor.

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

    Default

    that call (this(num, num)), is calling the other constructor.
    That explains so much! thank you.

    Also, what does
    super.sum()
    do?

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by myst View Post
    1. I don't understand it's logic. Where does it go?
    So is this what it means:
    s1 is of type Second but it only gets the methods of First??
    Basically yes, because you are stating you are handling a First object by declaring it as one.

  10. #10
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    super.method() calls the method inside the super class, so in this case super.sum() would call the sum method inside the First class.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Quote Originally Posted by myst View Post
    That explains so much! thank you.

    Also, what does do?
    "this" is the object you're currently working with.
    "super" is the parent class of the object you're working with.

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

    Default

    ok thanks! here's the rest...


    9. f3 = s1; // but they are of different types! Don't you need to do casting here - f3 = (First)s1

    // i'll wait to get the answer before I continue below...

    10. System.out.println ("sum = " + f3.sum());
    11. f3.setNum3 (2);
    12. f4.setNum3 (2);
    13. s1.setNum3 (2);
    14. System.out.println ("sum = " + s1.sum());
    15. s1 = f3;
    16. s1 = f4;
    17. System.out.println ("sum = " + s1.sum());

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    "public class Second extends First".

    That is, an object of class Second is also an object of class First.

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

    Default

    Quote Originally Posted by Tolls View Post
    That is, an object of class Second is also an object of class First.
    Great! So f3 = s1 causes aliasing.

    10. System.out.println ("sum = " + f3.sum()); // so this should get the sum of Second, right?


    Also, Is there a way to do something like s1 = f3?

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Not so sure what you mean by "aliasing".
    The variables are simply references to an object in memory, so f3 = s1 is setting the reference in f3 to be the same as that of s1.

    You're correct on 10, vecause f3 is now referencing an object of type Second.

    You can do s1 = f3, but only by casting, to tell the compiler you actually mean it.

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

    Default

    ok, thanks.

    11. f3.setNum3 (2); // according to my main method, this is an error! if f3 is now in Second, why doesn't this work??
    12. f4.setNum3 (2); // this should be an error since f4 is in First
    13. s1.setNum3 (2); // this should be fine
    14. System.out.println ("sum = " + s1.sum()); // this should print sum of Second
    15. s1 = f3; // now, s1 is still referring to Second since previously we wrote that f3 = s1
    16. s1 = f4; // now s1 refers to First
    17. System.out.println ("sum = " + s1.sum()); //why is this an error? I would think it would print the sum of First.

  17. #17
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Can you please post all the steps again as this is getting harder and harder to follow

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

    Default

    public class First{

    private int _num1 = 0;
    private int _num2 = 0;
    private static int _count = 0;

    public First(int num){
    this (num, num);
    _count++;
    System.out.println ("First constructor1");
    }

    public First(int num1, int num2){
    _num1 = num1;
    _num2 = num2;
    _count++;
    System.out.println ("First constructor2");
    }

    public int sum (){
    return _num1 + _num2;
    }

    public static int getCount(){
    return _count;
    }
    }

    and the second class:

    public class Second extends First{

    private int _num3 = 0;

    public Second(int num){
    super (num);
    _num3 = num;
    System.out.println ("Second constructor1");
    }

    public Second (int num1, int num2, int num3){
    super (num1, num2);
    _num3 = num3;
    System.out.println ("Second constructor2");
    }

    public int sum (){
    return super.sum() + _num3;
    }

    public void setNum3 (int num){
    _num3 = num;
    }
    }



    1. First f1 = new First (10);
    2. System.out.println ("sum1 = " + f1.sum());
    3. System.out.println ("count = " + First.getCount());
    4. First f3 = new First (10, 20);
    5. System.out.println ("count = " + f3.getCount());
    6. Second s1 = new Second (1);
    7. First f4 = new Second (2);
    8. System.out.println ("count = " + f4.getCount());
    9. f3 = s1;
    10. System.out.println ("sum = " + f3.sum());
    11. f3.setNum3 (2); // according to my main method, this is an error! if f3 is now in Second, why doesn't this work??
    12. f4.setNum3 (2); // this should be an error since f4 is in First
    13. s1.setNum3 (2); // this should be fine
    14. System.out.println ("sum = " + s1.sum()); // this should print sum of Second
    15. s1 = f3; // now, s1 is still referring to Second since previously we wrote that f3 = s1
    16. s1 = f4; // now s1 refers to First
    17. System.out.println ("sum = " + s1.sum()); //why is this an error? I would think it would print the sum of First.
    Last edited by myst; 05-24-2010 at 07:49 PM.

  19. #19
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by myst View Post
    public class First{
    11. f3.setNum3 (2); // according to my main method, this is an error! if f3 is now in Second, why doesn't this work??
    You declared f3 as a First object so it will still handle it as if it was a First object. There is no setNum3() method in the First class so this will error, unless you typecast f3 into a Second object like so
    Java Code:
    ((Second)f3).setNum3(2);
    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.

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

    Default

    You declared f3 as a First object so it will still handle it as if it was a First object. There is no setNum3() method in the First class so this will error
    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?

    Another thing I'm noticing you are doing...
    I'm not doing it. It's a question which purposely has these mistakes. I'm trying to understand them.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2009, 08:33 PM
  2. Replies: 5
    Last Post: 02-25-2009, 07:14 PM
  3. question about polymorphism
    By becky in forum New To Java
    Replies: 4
    Last Post: 02-11-2009, 10:59 PM
  4. Replies: 3
    Last Post: 12-10-2008, 01:38 AM
  5. Simple program involving military time
    By busdude in forum New To Java
    Replies: 4
    Last Post: 10-08-2008, 06: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
  •