Unclear about dynamic binding The Next CEO of Stack OverflowEfficiency of Java “Double Brace Initialization”?Getting value to display for Java CurrentAccount classStatic Vs. Dynamic Binding in JavaClone a Singleton objectIs it possible to write a program in Java without main() using JDK 1.7 or higher?Overriding private methods in (non-)static classesExecutorService workStealingPool and cancel methodJava - Method executed prior to Default Constructorconfusion about upcasting vs dynamic bindingjava exception - why does it catch?

Dominated convergence theorem - what sequence?

0 rank tensor vs 1D vector

Can MTA send mail via a relay without being told so?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

Find non-case sensitive string in a mixed list of elements?

Running a General Election and the European Elections together

How to count occurrences of text in a file?

Is it convenient to ask the journal's editor for two additional days to complete a review?

Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?

How to invert MapIndexed on a ragged structure? How to construct a tree from rules?

Is there always a complete, orthogonal set of unitary matrices?

How do I align (1) and (2)?

Would a completely good Muggle be able to use a wand?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Why don't programming languages automatically manage the synchronous/asynchronous problem?

Is it possible to use a NPN BJT as switch, from single power source?

Flying from Cape Town to England and return to another province

Method for adding error messages to a dictionary given a key

Math-accent symbol over parentheses enclosing accented symbol (amsmath)

How did people program for Consoles with multiple CPUs?

RigExpert AA-35 - Interpreting The Information

Proper way to express "He disappeared them"

Does increasing your ability score affect your main stat?

Why isn't acceleration always zero whenever velocity is zero, such as the moment a ball bounces off a wall?



Unclear about dynamic binding



The Next CEO of Stack OverflowEfficiency of Java “Double Brace Initialization”?Getting value to display for Java CurrentAccount classStatic Vs. Dynamic Binding in JavaClone a Singleton objectIs it possible to write a program in Java without main() using JDK 1.7 or higher?Overriding private methods in (non-)static classesExecutorService workStealingPool and cancel methodJava - Method executed prior to Default Constructorconfusion about upcasting vs dynamic bindingjava exception - why does it catch?










10















I am not understanding the concept of dynamic binding and overriding properly:



Here is some code:



class Cake 
public void taste (Cake c)
System.out.println("In taste of Cake class");



class ChocolateCake extends Cake
public void taste(Cake c)
System.out.println("In taste (Cake version) of ChocolateCake class");

public void taste(ChocolateCake cc)
System.out.println("In taste (ChocolateCake version) of ChocolateCake class");




public static void main(String[] args)

ChocolateCake cc = new ChocolateCake();
Cake c = new ChocolateCake();
Cake c1 = new Cake();
Cake c2 = new ChocolateCake();

c1.taste(cc);
c1.taste(c);

c2.taste(cc);
c2.taste(c);



I expected:



In taste of Cake class
In taste of Cake class
In taste (ChocolateCake version) of ChocolateCake class" <----
In taste (Cake version) of ChocolateCake class


Actual:



In taste of Cake class
In taste of Cake class
In taste (Cake version) of ChocolateCake class <----
In taste (Cake version) of ChocolateCake class


If the object is of type ChocolateCake and I call cc which is also ChocolateCake, how come the compiler shows it's getting Cake as a parameter?










share|improve this question









New contributor




coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    10















    I am not understanding the concept of dynamic binding and overriding properly:



    Here is some code:



    class Cake 
    public void taste (Cake c)
    System.out.println("In taste of Cake class");



    class ChocolateCake extends Cake
    public void taste(Cake c)
    System.out.println("In taste (Cake version) of ChocolateCake class");

    public void taste(ChocolateCake cc)
    System.out.println("In taste (ChocolateCake version) of ChocolateCake class");




    public static void main(String[] args)

    ChocolateCake cc = new ChocolateCake();
    Cake c = new ChocolateCake();
    Cake c1 = new Cake();
    Cake c2 = new ChocolateCake();

    c1.taste(cc);
    c1.taste(c);

    c2.taste(cc);
    c2.taste(c);



    I expected:



    In taste of Cake class
    In taste of Cake class
    In taste (ChocolateCake version) of ChocolateCake class" <----
    In taste (Cake version) of ChocolateCake class


    Actual:



    In taste of Cake class
    In taste of Cake class
    In taste (Cake version) of ChocolateCake class <----
    In taste (Cake version) of ChocolateCake class


    If the object is of type ChocolateCake and I call cc which is also ChocolateCake, how come the compiler shows it's getting Cake as a parameter?










    share|improve this question









    New contributor




    coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      10












      10








      10








      I am not understanding the concept of dynamic binding and overriding properly:



      Here is some code:



      class Cake 
      public void taste (Cake c)
      System.out.println("In taste of Cake class");



      class ChocolateCake extends Cake
      public void taste(Cake c)
      System.out.println("In taste (Cake version) of ChocolateCake class");

      public void taste(ChocolateCake cc)
      System.out.println("In taste (ChocolateCake version) of ChocolateCake class");




      public static void main(String[] args)

      ChocolateCake cc = new ChocolateCake();
      Cake c = new ChocolateCake();
      Cake c1 = new Cake();
      Cake c2 = new ChocolateCake();

      c1.taste(cc);
      c1.taste(c);

      c2.taste(cc);
      c2.taste(c);



      I expected:



      In taste of Cake class
      In taste of Cake class
      In taste (ChocolateCake version) of ChocolateCake class" <----
      In taste (Cake version) of ChocolateCake class


      Actual:



      In taste of Cake class
      In taste of Cake class
      In taste (Cake version) of ChocolateCake class <----
      In taste (Cake version) of ChocolateCake class


      If the object is of type ChocolateCake and I call cc which is also ChocolateCake, how come the compiler shows it's getting Cake as a parameter?










      share|improve this question









      New contributor




      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am not understanding the concept of dynamic binding and overriding properly:



      Here is some code:



      class Cake 
      public void taste (Cake c)
      System.out.println("In taste of Cake class");



      class ChocolateCake extends Cake
      public void taste(Cake c)
      System.out.println("In taste (Cake version) of ChocolateCake class");

      public void taste(ChocolateCake cc)
      System.out.println("In taste (ChocolateCake version) of ChocolateCake class");




      public static void main(String[] args)

      ChocolateCake cc = new ChocolateCake();
      Cake c = new ChocolateCake();
      Cake c1 = new Cake();
      Cake c2 = new ChocolateCake();

      c1.taste(cc);
      c1.taste(c);

      c2.taste(cc);
      c2.taste(c);



      I expected:



      In taste of Cake class
      In taste of Cake class
      In taste (ChocolateCake version) of ChocolateCake class" <----
      In taste (Cake version) of ChocolateCake class


      Actual:



      In taste of Cake class
      In taste of Cake class
      In taste (Cake version) of ChocolateCake class <----
      In taste (Cake version) of ChocolateCake class


      If the object is of type ChocolateCake and I call cc which is also ChocolateCake, how come the compiler shows it's getting Cake as a parameter?







      java






      share|improve this question









      New contributor




      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 4 hours ago









      Peter Mortensen

      13.8k1987113




      13.8k1987113






      New contributor




      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 8 hours ago









      coding_potatocoding_potato

      512




      512




      New contributor




      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      coding_potato is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          6














          This is because Java uses both static and dynamic binding to choose a method to call in this case.



          The line in question is this, right?



          c2.taste(cc);


          The compiler first chooses which method to call (static binding). Since c2 is of compile time type Cake, the compiler only sees the taste(Cake) method. So it says "call taste(Cake)".



          Now at runtime, the runtime needs to choose which implementation of taste(Cake) to call, depending on the runtime type of c2. This is dynamic binding. Does it choose the one in Cake? Or the one in ChocolateCake? Since c2 is of runtime type ChocolateCake, it calls the implementation of taste(Cake) in ChocolateCake.



          As you can see, the method that you thought would be called - taste(ChocolateCake) - is not even mentioned! This is because that is a different overload of the taste method, and because it is in the ChocolateCake class, which the compiler can't see. Why can't the compiler see? Because c2 is of compile time type Cake.



          In short, the compiler decides which overload, the runtime decides which implementation.



          Responding to your statement:




          if the object is of type ChocolateCake ...




          Only you know the object is of type ChocolateCake. The compiler does not. It only knows c2 is of type Cake because that's what its declaration says.






          share|improve this answer






























            4














            Since the reference type of the c2 variable is Cake the taste method having the Cake type parameter will be called.



            This is because the Cake type does not have the taste method which takes a ChocolateCake instance, so you can't invoke that method from a Cake type reference variable.



            Now secondly, in Java due to the mechanism of runtime polymorphism the overridden taste method of the ChocolateCake is being called instead of the version declared in the parent Cake class. This is due to fact at runtime the object which the Cake reference is pointing to, will be examined and the taste version of that particular instance will be invoked.



            So due to the combination of these two effects you see that output.



            If you change the reference type of c2 to ChocolateCake you would see that the output is:



            In taste (ChocolateCake version) of ChocolateCake class 


            when you invoke c2.taste(cc);, since now both the compiler and runtime agrees to call that taste(ChocolateCake cc) method in particular.






            share|improve this answer
































              0














              Please modify your code to this for better understanding:



              public class Main 

              public static class Cake
              public void taste()
              System.out.println("In taste of Cake class");



              public static class ChocolateCake extends Cake
              public void taste()
              System.out.println("In taste (Cake version) of ChocolateCake class");



              public static void main(String[] args)
              Cake cake = new ChocolateCake();
              cake.taste();
              cake = new Cake();
              cake.taste();




              Output:



              In taste (Cake version) of ChocolateCake class
              In taste of Cake class


              Dynamic Binding



              **Dynamic binding occurs during runtime (which is known as runtime polymorphism or method overiding).
              In any object-oriented programming language, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.



              When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.**



              Note: Rules of overriding:



              Base Class Reference = Base Class/Child Class Object





              share|improve this answer

























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );






                coding_potato is a new contributor. Be nice, and check out our Code of Conduct.









                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55434188%2funclear-about-dynamic-binding%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                6














                This is because Java uses both static and dynamic binding to choose a method to call in this case.



                The line in question is this, right?



                c2.taste(cc);


                The compiler first chooses which method to call (static binding). Since c2 is of compile time type Cake, the compiler only sees the taste(Cake) method. So it says "call taste(Cake)".



                Now at runtime, the runtime needs to choose which implementation of taste(Cake) to call, depending on the runtime type of c2. This is dynamic binding. Does it choose the one in Cake? Or the one in ChocolateCake? Since c2 is of runtime type ChocolateCake, it calls the implementation of taste(Cake) in ChocolateCake.



                As you can see, the method that you thought would be called - taste(ChocolateCake) - is not even mentioned! This is because that is a different overload of the taste method, and because it is in the ChocolateCake class, which the compiler can't see. Why can't the compiler see? Because c2 is of compile time type Cake.



                In short, the compiler decides which overload, the runtime decides which implementation.



                Responding to your statement:




                if the object is of type ChocolateCake ...




                Only you know the object is of type ChocolateCake. The compiler does not. It only knows c2 is of type Cake because that's what its declaration says.






                share|improve this answer



























                  6














                  This is because Java uses both static and dynamic binding to choose a method to call in this case.



                  The line in question is this, right?



                  c2.taste(cc);


                  The compiler first chooses which method to call (static binding). Since c2 is of compile time type Cake, the compiler only sees the taste(Cake) method. So it says "call taste(Cake)".



                  Now at runtime, the runtime needs to choose which implementation of taste(Cake) to call, depending on the runtime type of c2. This is dynamic binding. Does it choose the one in Cake? Or the one in ChocolateCake? Since c2 is of runtime type ChocolateCake, it calls the implementation of taste(Cake) in ChocolateCake.



                  As you can see, the method that you thought would be called - taste(ChocolateCake) - is not even mentioned! This is because that is a different overload of the taste method, and because it is in the ChocolateCake class, which the compiler can't see. Why can't the compiler see? Because c2 is of compile time type Cake.



                  In short, the compiler decides which overload, the runtime decides which implementation.



                  Responding to your statement:




                  if the object is of type ChocolateCake ...




                  Only you know the object is of type ChocolateCake. The compiler does not. It only knows c2 is of type Cake because that's what its declaration says.






                  share|improve this answer

























                    6












                    6








                    6







                    This is because Java uses both static and dynamic binding to choose a method to call in this case.



                    The line in question is this, right?



                    c2.taste(cc);


                    The compiler first chooses which method to call (static binding). Since c2 is of compile time type Cake, the compiler only sees the taste(Cake) method. So it says "call taste(Cake)".



                    Now at runtime, the runtime needs to choose which implementation of taste(Cake) to call, depending on the runtime type of c2. This is dynamic binding. Does it choose the one in Cake? Or the one in ChocolateCake? Since c2 is of runtime type ChocolateCake, it calls the implementation of taste(Cake) in ChocolateCake.



                    As you can see, the method that you thought would be called - taste(ChocolateCake) - is not even mentioned! This is because that is a different overload of the taste method, and because it is in the ChocolateCake class, which the compiler can't see. Why can't the compiler see? Because c2 is of compile time type Cake.



                    In short, the compiler decides which overload, the runtime decides which implementation.



                    Responding to your statement:




                    if the object is of type ChocolateCake ...




                    Only you know the object is of type ChocolateCake. The compiler does not. It only knows c2 is of type Cake because that's what its declaration says.






                    share|improve this answer













                    This is because Java uses both static and dynamic binding to choose a method to call in this case.



                    The line in question is this, right?



                    c2.taste(cc);


                    The compiler first chooses which method to call (static binding). Since c2 is of compile time type Cake, the compiler only sees the taste(Cake) method. So it says "call taste(Cake)".



                    Now at runtime, the runtime needs to choose which implementation of taste(Cake) to call, depending on the runtime type of c2. This is dynamic binding. Does it choose the one in Cake? Or the one in ChocolateCake? Since c2 is of runtime type ChocolateCake, it calls the implementation of taste(Cake) in ChocolateCake.



                    As you can see, the method that you thought would be called - taste(ChocolateCake) - is not even mentioned! This is because that is a different overload of the taste method, and because it is in the ChocolateCake class, which the compiler can't see. Why can't the compiler see? Because c2 is of compile time type Cake.



                    In short, the compiler decides which overload, the runtime decides which implementation.



                    Responding to your statement:




                    if the object is of type ChocolateCake ...




                    Only you know the object is of type ChocolateCake. The compiler does not. It only knows c2 is of type Cake because that's what its declaration says.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 8 hours ago









                    SweeperSweeper

                    71.4k1075144




                    71.4k1075144























                        4














                        Since the reference type of the c2 variable is Cake the taste method having the Cake type parameter will be called.



                        This is because the Cake type does not have the taste method which takes a ChocolateCake instance, so you can't invoke that method from a Cake type reference variable.



                        Now secondly, in Java due to the mechanism of runtime polymorphism the overridden taste method of the ChocolateCake is being called instead of the version declared in the parent Cake class. This is due to fact at runtime the object which the Cake reference is pointing to, will be examined and the taste version of that particular instance will be invoked.



                        So due to the combination of these two effects you see that output.



                        If you change the reference type of c2 to ChocolateCake you would see that the output is:



                        In taste (ChocolateCake version) of ChocolateCake class 


                        when you invoke c2.taste(cc);, since now both the compiler and runtime agrees to call that taste(ChocolateCake cc) method in particular.






                        share|improve this answer





























                          4














                          Since the reference type of the c2 variable is Cake the taste method having the Cake type parameter will be called.



                          This is because the Cake type does not have the taste method which takes a ChocolateCake instance, so you can't invoke that method from a Cake type reference variable.



                          Now secondly, in Java due to the mechanism of runtime polymorphism the overridden taste method of the ChocolateCake is being called instead of the version declared in the parent Cake class. This is due to fact at runtime the object which the Cake reference is pointing to, will be examined and the taste version of that particular instance will be invoked.



                          So due to the combination of these two effects you see that output.



                          If you change the reference type of c2 to ChocolateCake you would see that the output is:



                          In taste (ChocolateCake version) of ChocolateCake class 


                          when you invoke c2.taste(cc);, since now both the compiler and runtime agrees to call that taste(ChocolateCake cc) method in particular.






                          share|improve this answer



























                            4












                            4








                            4







                            Since the reference type of the c2 variable is Cake the taste method having the Cake type parameter will be called.



                            This is because the Cake type does not have the taste method which takes a ChocolateCake instance, so you can't invoke that method from a Cake type reference variable.



                            Now secondly, in Java due to the mechanism of runtime polymorphism the overridden taste method of the ChocolateCake is being called instead of the version declared in the parent Cake class. This is due to fact at runtime the object which the Cake reference is pointing to, will be examined and the taste version of that particular instance will be invoked.



                            So due to the combination of these two effects you see that output.



                            If you change the reference type of c2 to ChocolateCake you would see that the output is:



                            In taste (ChocolateCake version) of ChocolateCake class 


                            when you invoke c2.taste(cc);, since now both the compiler and runtime agrees to call that taste(ChocolateCake cc) method in particular.






                            share|improve this answer















                            Since the reference type of the c2 variable is Cake the taste method having the Cake type parameter will be called.



                            This is because the Cake type does not have the taste method which takes a ChocolateCake instance, so you can't invoke that method from a Cake type reference variable.



                            Now secondly, in Java due to the mechanism of runtime polymorphism the overridden taste method of the ChocolateCake is being called instead of the version declared in the parent Cake class. This is due to fact at runtime the object which the Cake reference is pointing to, will be examined and the taste version of that particular instance will be invoked.



                            So due to the combination of these two effects you see that output.



                            If you change the reference type of c2 to ChocolateCake you would see that the output is:



                            In taste (ChocolateCake version) of ChocolateCake class 


                            when you invoke c2.taste(cc);, since now both the compiler and runtime agrees to call that taste(ChocolateCake cc) method in particular.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 8 hours ago

























                            answered 8 hours ago









                            Amardeep BhowmickAmardeep Bhowmick

                            5,13321128




                            5,13321128





















                                0














                                Please modify your code to this for better understanding:



                                public class Main 

                                public static class Cake
                                public void taste()
                                System.out.println("In taste of Cake class");



                                public static class ChocolateCake extends Cake
                                public void taste()
                                System.out.println("In taste (Cake version) of ChocolateCake class");



                                public static void main(String[] args)
                                Cake cake = new ChocolateCake();
                                cake.taste();
                                cake = new Cake();
                                cake.taste();




                                Output:



                                In taste (Cake version) of ChocolateCake class
                                In taste of Cake class


                                Dynamic Binding



                                **Dynamic binding occurs during runtime (which is known as runtime polymorphism or method overiding).
                                In any object-oriented programming language, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.



                                When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.**



                                Note: Rules of overriding:



                                Base Class Reference = Base Class/Child Class Object





                                share|improve this answer





























                                  0














                                  Please modify your code to this for better understanding:



                                  public class Main 

                                  public static class Cake
                                  public void taste()
                                  System.out.println("In taste of Cake class");



                                  public static class ChocolateCake extends Cake
                                  public void taste()
                                  System.out.println("In taste (Cake version) of ChocolateCake class");



                                  public static void main(String[] args)
                                  Cake cake = new ChocolateCake();
                                  cake.taste();
                                  cake = new Cake();
                                  cake.taste();




                                  Output:



                                  In taste (Cake version) of ChocolateCake class
                                  In taste of Cake class


                                  Dynamic Binding



                                  **Dynamic binding occurs during runtime (which is known as runtime polymorphism or method overiding).
                                  In any object-oriented programming language, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.



                                  When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.**



                                  Note: Rules of overriding:



                                  Base Class Reference = Base Class/Child Class Object





                                  share|improve this answer



























                                    0












                                    0








                                    0







                                    Please modify your code to this for better understanding:



                                    public class Main 

                                    public static class Cake
                                    public void taste()
                                    System.out.println("In taste of Cake class");



                                    public static class ChocolateCake extends Cake
                                    public void taste()
                                    System.out.println("In taste (Cake version) of ChocolateCake class");



                                    public static void main(String[] args)
                                    Cake cake = new ChocolateCake();
                                    cake.taste();
                                    cake = new Cake();
                                    cake.taste();




                                    Output:



                                    In taste (Cake version) of ChocolateCake class
                                    In taste of Cake class


                                    Dynamic Binding



                                    **Dynamic binding occurs during runtime (which is known as runtime polymorphism or method overiding).
                                    In any object-oriented programming language, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.



                                    When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.**



                                    Note: Rules of overriding:



                                    Base Class Reference = Base Class/Child Class Object





                                    share|improve this answer















                                    Please modify your code to this for better understanding:



                                    public class Main 

                                    public static class Cake
                                    public void taste()
                                    System.out.println("In taste of Cake class");



                                    public static class ChocolateCake extends Cake
                                    public void taste()
                                    System.out.println("In taste (Cake version) of ChocolateCake class");



                                    public static void main(String[] args)
                                    Cake cake = new ChocolateCake();
                                    cake.taste();
                                    cake = new Cake();
                                    cake.taste();




                                    Output:



                                    In taste (Cake version) of ChocolateCake class
                                    In taste of Cake class


                                    Dynamic Binding



                                    **Dynamic binding occurs during runtime (which is known as runtime polymorphism or method overiding).
                                    In any object-oriented programming language, overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.



                                    When a method in a subclass has the same name, same parameters or signature and same return type (or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.**



                                    Note: Rules of overriding:



                                    Base Class Reference = Base Class/Child Class Object






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited 4 hours ago









                                    Peter Mortensen

                                    13.8k1987113




                                    13.8k1987113










                                    answered 8 hours ago









                                    Anish B.Anish B.

                                    392113




                                    392113




















                                        coding_potato is a new contributor. Be nice, and check out our Code of Conduct.









                                        draft saved

                                        draft discarded


















                                        coding_potato is a new contributor. Be nice, and check out our Code of Conduct.












                                        coding_potato is a new contributor. Be nice, and check out our Code of Conduct.











                                        coding_potato is a new contributor. Be nice, and check out our Code of Conduct.














                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55434188%2funclear-about-dynamic-binding%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Styletc

                                        Tórshavn Kliima | Partnerstääden | Luke uk diar | Nawigatsjuun62° 1′ N, 6° 46′ W62° 1′ 0″ N, 6° 46′ 0″ WWMOTórshavn