Why will the variable and an accessory method return two different things under Inheritance? [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Overriding member variables in Java
10 answers
Java inheritance fields [duplicate]
5 answers
Here are two classes
class FirstClass {
int x;
public FirstClass(int n){
x=n;
}
int getx(){
return x;
}
}
class SecondClass extends FirstClass {
int x;
public SecondClass(int n){
super(n*2);
x=n;
}
int getx(){
return x;
}
}
Yet if I do something like FirstClass obj = new SecondClass(5)
, obj.x is 10, and obj.getx() returns 5.
I'd either have expected getx() to be 5 and x to be 5, or getx() to be 10 and x to be 10. Why does the code work this way?
java inheritance
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 1:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
Overriding member variables in Java
10 answers
Java inheritance fields [duplicate]
5 answers
Here are two classes
class FirstClass {
int x;
public FirstClass(int n){
x=n;
}
int getx(){
return x;
}
}
class SecondClass extends FirstClass {
int x;
public SecondClass(int n){
super(n*2);
x=n;
}
int getx(){
return x;
}
}
Yet if I do something like FirstClass obj = new SecondClass(5)
, obj.x is 10, and obj.getx() returns 5.
I'd either have expected getx() to be 5 and x to be 5, or getx() to be 10 and x to be 10. Why does the code work this way?
java inheritance
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 1:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Fields are not polymoprhic, only methods are (with exception ofprivate
,static
orfinal
ones)
– Pshemo
Nov 11 at 1:19
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Overriding member variables in Java
10 answers
Java inheritance fields [duplicate]
5 answers
Here are two classes
class FirstClass {
int x;
public FirstClass(int n){
x=n;
}
int getx(){
return x;
}
}
class SecondClass extends FirstClass {
int x;
public SecondClass(int n){
super(n*2);
x=n;
}
int getx(){
return x;
}
}
Yet if I do something like FirstClass obj = new SecondClass(5)
, obj.x is 10, and obj.getx() returns 5.
I'd either have expected getx() to be 5 and x to be 5, or getx() to be 10 and x to be 10. Why does the code work this way?
java inheritance
This question already has an answer here:
Overriding member variables in Java
10 answers
Java inheritance fields [duplicate]
5 answers
Here are two classes
class FirstClass {
int x;
public FirstClass(int n){
x=n;
}
int getx(){
return x;
}
}
class SecondClass extends FirstClass {
int x;
public SecondClass(int n){
super(n*2);
x=n;
}
int getx(){
return x;
}
}
Yet if I do something like FirstClass obj = new SecondClass(5)
, obj.x is 10, and obj.getx() returns 5.
I'd either have expected getx() to be 5 and x to be 5, or getx() to be 10 and x to be 10. Why does the code work this way?
This question already has an answer here:
Overriding member variables in Java
10 answers
Java inheritance fields [duplicate]
5 answers
java inheritance
java inheritance
asked Nov 11 at 1:09
bleh
11539
11539
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 1:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Andreas
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 1:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Fields are not polymoprhic, only methods are (with exception ofprivate
,static
orfinal
ones)
– Pshemo
Nov 11 at 1:19
add a comment |
Fields are not polymoprhic, only methods are (with exception ofprivate
,static
orfinal
ones)
– Pshemo
Nov 11 at 1:19
Fields are not polymoprhic, only methods are (with exception of
private
, static
or final
ones)– Pshemo
Nov 11 at 1:19
Fields are not polymoprhic, only methods are (with exception of
private
, static
or final
ones)– Pshemo
Nov 11 at 1:19
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Fields are not polymoprhic, only methods are (with exception of
private
,static
orfinal
ones)– Pshemo
Nov 11 at 1:19