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?










share|improve this question













marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 of private, static or final ones)
    – Pshemo
    Nov 11 at 1:19

















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?










share|improve this question













marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 of private, static or final ones)
    – Pshemo
    Nov 11 at 1:19















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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 1:09









bleh

11539




11539




marked as duplicate by Andreas java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

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 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


















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



















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues