Results 1 to 5 of 5
  1. #1
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Exclamation Doubt in run time polymorphism

    Could You explain the program flow and output?
    Java Code:
    package com;
    
    class Alpha {
    public void foo(String... args) {
    System.out.println("Alpha:foo");
    }
    
    public void bar(String a) {
    System.out.println("Alpha:bar");
    }
    }
    
    public class RegExCheck extends Alpha {
    public void foo(String a) {
    System.out.println("Beta:foo");
    }
    
    public void bar(String a) {
    System.out.println("Beta:bar");
    }
    
    public static void main(String[] args) {
    Alpha a = new RegExCheck();
    RegExCheck b = (RegExCheck) a;
    a.foo("test"); b.foo("test");
    a.bar("test"); b.bar("test");
    }
    }
    One Life!!! Y Serious??? :)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Quote Originally Posted by Thennarasu View Post
    Could You explain the program flow and output?
    How about you tell us:

    a. what you get.
    b. what you expect.
    c. why you expected b.

    otherwise we might simply be doing your homework for you.

  3. #3
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    1) I get O/P as Alpha:foo Beta:foo Alpha:bar Beta:bar
    2) I expected it to be Beta:foo Beta:foo Alpha:bar Beta:bar
    2) I expected (2) because object a is created of the reference type Beta and hence at run time it should call method Beta class.
    One Life!!! Y Serious??? :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default

    Quote Originally Posted by Thennarasu View Post
    1) I get O/P as Alpha:foo Beta:foo Alpha:bar Beta:bar
    You do? That's not either what I expect, or what I get. Did you write the code correctly, or was bar() supposed to be static?

    Quote Originally Posted by Thennarasu View Post
    2) I expected it to be Beta:foo Beta:foo Alpha:bar Beta:bar
    Well, as you suspect, that's not correct either.

    Quote Originally Posted by Thennarasu View Post
    2) I expected (2) because object a is created of the reference type Beta and hence at run time it should call method Beta class.
    The runtime looks for the foo() function that matches a.foo(), which takes a String. It finds one called foo(String...). It then looks to see if it's overridden by the subclass, and doesn't find a child version of foo(String...), so it sticks with the Alpha method it has found.

    At least, that's the gist of its decision making.

  5. #5
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

Similar Threads

  1. is Polymorphism the tool to use?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 05-04-2009, 06:02 PM
  2. Polymorphism Help
    By AWPtic in forum New To Java
    Replies: 5
    Last Post: 04-06-2009, 05:13 PM
  3. inheritance and polymorphism
    By tester in forum Eclipse
    Replies: 1
    Last Post: 12-21-2008, 05:58 AM
  4. what is polymorphism
    By Nari in forum New To Java
    Replies: 5
    Last Post: 04-04-2008, 04:14 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 11:29 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
  •