Unclear about dynamic bindingEfficiency 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?
Are pressure-treated posts that have been submerged for a few days ruined?
Would a small hole in a Faraday cage drastically reduce its effectiveness at blocking interference?
Notation: What does the tilde bellow of the Expectation mean?
Is there an age requirement to play in Adventurers League?
As a GM, is it bad form to ask for a moment to think when improvising?
How is Per Object Storage Usage Calculated
How to ask systemd to not start a system service on boot?
How should I tell my manager I'm not paying for an optional after work event I'm not going to?
Is there a word for food that's gone 'bad', but is still edible?
How can I get people to remember my character's gender?
What is the closest airport to the center of the city it serves?
Handling Null values (and equivalents) routinely in Python
Nested loops to process groups of pictures
Is 'contemporary' ambiguous and if so is there a better word?
Definition of conditional probability and a problem.
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
getline() vs. fgets(): Control memory allocation
How long would it take for people to notice a mass disappearance?
Typeset year in old-style numbers with biblatex
A factorization game
Using Im[] and Re[] Correctly
Gladys unchained
Can my 2 children, aged 10 and 12, who are US citizens, travel to the USA on expired American passports?
My first c++ game (snake console game)
Unclear about dynamic binding
Efficiency 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?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
add a comment |
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
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35
add a comment |
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
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
java
edited Mar 30 at 22:01
Peter Mortensen
14k1987114
14k1987114
asked Mar 30 at 17:54
coding_potatocoding_potato
833
833
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35
add a comment |
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
add a comment |
In Java the decision which methodset to call in case of c2.taste(cc)
is performed at compile-time based on the compile-time type of c2
. The compile-time type of c2
is Cake
, which means that any method call on c2
is searching just class Cake
and its superclasses, and isn't searching any subclasses of Cake
(namely ChocolateCake
) even if all the subclasses are visible to the compiler.
Languages performing fully dynamic method resolution at runtime based on the actual runtime types of the receiver&arguments, which would make c2.taste(cc)
be resolved into ChocolateCake.taste(ChocolateCake cc)
, are rare because it negatively affects runtime performance.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 30 at 18:15
SweeperSweeper
74.5k1075146
74.5k1075146
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 30 at 18:12
answered Mar 30 at 18:06
Amardeep BhowmickAmardeep Bhowmick
6,51621231
6,51621231
add a comment |
add a comment |
In Java the decision which methodset to call in case of c2.taste(cc)
is performed at compile-time based on the compile-time type of c2
. The compile-time type of c2
is Cake
, which means that any method call on c2
is searching just class Cake
and its superclasses, and isn't searching any subclasses of Cake
(namely ChocolateCake
) even if all the subclasses are visible to the compiler.
Languages performing fully dynamic method resolution at runtime based on the actual runtime types of the receiver&arguments, which would make c2.taste(cc)
be resolved into ChocolateCake.taste(ChocolateCake cc)
, are rare because it negatively affects runtime performance.
add a comment |
In Java the decision which methodset to call in case of c2.taste(cc)
is performed at compile-time based on the compile-time type of c2
. The compile-time type of c2
is Cake
, which means that any method call on c2
is searching just class Cake
and its superclasses, and isn't searching any subclasses of Cake
(namely ChocolateCake
) even if all the subclasses are visible to the compiler.
Languages performing fully dynamic method resolution at runtime based on the actual runtime types of the receiver&arguments, which would make c2.taste(cc)
be resolved into ChocolateCake.taste(ChocolateCake cc)
, are rare because it negatively affects runtime performance.
add a comment |
In Java the decision which methodset to call in case of c2.taste(cc)
is performed at compile-time based on the compile-time type of c2
. The compile-time type of c2
is Cake
, which means that any method call on c2
is searching just class Cake
and its superclasses, and isn't searching any subclasses of Cake
(namely ChocolateCake
) even if all the subclasses are visible to the compiler.
Languages performing fully dynamic method resolution at runtime based on the actual runtime types of the receiver&arguments, which would make c2.taste(cc)
be resolved into ChocolateCake.taste(ChocolateCake cc)
, are rare because it negatively affects runtime performance.
In Java the decision which methodset to call in case of c2.taste(cc)
is performed at compile-time based on the compile-time type of c2
. The compile-time type of c2
is Cake
, which means that any method call on c2
is searching just class Cake
and its superclasses, and isn't searching any subclasses of Cake
(namely ChocolateCake
) even if all the subclasses are visible to the compiler.
Languages performing fully dynamic method resolution at runtime based on the actual runtime types of the receiver&arguments, which would make c2.taste(cc)
be resolved into ChocolateCake.taste(ChocolateCake cc)
, are rare because it negatively affects runtime performance.
answered Mar 31 at 15:05
atomsymbolatomsymbol
205510
205510
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Please consider accepting an answer by clicking on that checkmark.
– Sweeper
Apr 1 at 8:35