Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Polymorphism problem in java..

Polymorphism problem in java..

Scheduled Pinned Locked Moved Java
javaoophelpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gateway23
    wrote on last edited by
    #1

    When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }

    L J A 4 Replies Last reply
    0
    • G gateway23

      When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      See the note about implicit casting in this page[^] of the Java Tutorials. Also please put your code between <pre> tags for proper formatting.

      The best things in life are not things.

      1 Reply Last reply
      0
      • G gateway23

        When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        gateway23 wrote:

        a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass?

        A reference points to an object. Your reference points to an object of type 'b'.

        1 Reply Last reply
        0
        • G gateway23

          When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          gateway23 wrote:

          a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass?

          It is because eventhough you have declared aRef to be of class 'a', it acutally holds an instance of class 'b' at runtime. That is what Polymorphism is all about. That's not a problem, that's the expected behavior.

          J 1 Reply Last reply
          0
          • G gateway23

            When we assign a subclass object to its superclass reference then the superclass reference can only access the methods that are in the superclass, it can't even the aceess its overridden methods in subclass. if the above lines are true then how the following code can works ... class a { int x; void show() { System.out.println(x); } } class b extends a { void show() { System.out.println("in subclass"); } void show2() { System.out.println("in show2"); } } class Main { public static void main(String args[]) { b bRef=new b(); a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass? aRef.show(); bRef.show2(); } }

            A Offline
            A Offline
            Ashish Tyagi 40
            wrote on last edited by
            #5

            Hello friend... Just add this line in main() System.out.println("aRef's type is " + aRef.getClass().toString()); and output for this line must me : aRef's type is b when you call aRef.show() then first this call must be resolved by class of aRef's object if fuction show() not found then jvm try to resolved it from super class.

            1 Reply Last reply
            0
            • L Lost User

              gateway23 wrote:

              a aRef=new b(); // why this line prints "in subclass". How a reference access the subclass?

              It is because eventhough you have declared aRef to be of class 'a', it acutally holds an instance of class 'b' at runtime. That is what Polymorphism is all about. That's not a problem, that's the expected behavior.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              It doesn't hold it. The heap holds it. It points to it.

              L 1 Reply Last reply
              0
              • J jschell

                It doesn't hold it. The heap holds it. It points to it.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Well, that's what I meant. Don't read the word "holds" literally :)

                L 1 Reply Last reply
                0
                • L Lost User

                  Well, that's what I meant. Don't read the word "holds" literally :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Shameel wrote:

                  Don't read the word "holds" literally

                  That is not the point. If you must give a technical answer then make sure it is accurate and clear. Remember, many of these questions are asked by people who do not have a lot of background knowledge or experience and rely on these answers to help them in their learning. The difference between "holds" and "points to" may be crucial to a new developer.

                  The best things in life are not things.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    Shameel wrote:

                    Don't read the word "holds" literally

                    That is not the point. If you must give a technical answer then make sure it is accurate and clear. Remember, many of these questions are asked by people who do not have a lot of background knowledge or experience and rely on these answers to help them in their learning. The difference between "holds" and "points to" may be crucial to a new developer.

                    The best things in life are not things.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    You're right. I'll make sure I choose the right words to explain my answer. Thanks for pointing out.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups