How do I override an inherited protected attribute from a parent class? [duplicate]












3
















This question already has an answer here:




  • Overriding member variables in Java

    10 answers



  • Java abstract class fields override

    6 answers




I'm coming up with a set of Java classes for templating.



I have an abstract template:



package Test;

public class Abstract {
protected String template = "ABSTRACT TEMPLATE";
public Abstract() {

}

public void Render() {
System.out.println("FROM ABSTRACT RENDER:");
System.out.println(this.template);
}
}


And an actual one:



package Test;

public class Actual extends Abstract {
protected String template = "ACTUAL TEMPLATE";
public Actual() {
super();
System.out.println("FROM ACTUAL CONSTRUCTOR:");
System.out.println(this.template);
}

public void Test() {
System.out.println("FROM ACTUAL TEST:");
System.out.println(this.template);
}
}


I'm having trouble getting the extending class to reset the value of a protected attribute (in this case template) and letting be used by the abstract method.



This is my use case:



Actual actual = new Actual();
actual.Render();
actual.Test();


And this is my output:



FROM ACTUAL CONSTRUCTOR:
ACTUAL TEMPLATE
FROM ABSTRACT RENDER:
ABSTRACT TEMPLATE <--- this is the problem, why not "ACTUAL"?
FROM ACTUAL TEST:
ACTUAL TEMPLATE


How do I override that value from the child class? If I don't set it to anything, calling the abstract method will always say the attribute is null, even though it's set in the child class.










share|improve this question















marked as duplicate by Robin Green, Andrew Tobilko 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 25 '18 at 11:46


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.



















  • Related stackoverflow.com/q/9414990/5168011

    – Guy
    Nov 25 '18 at 11:46








  • 1





    "Variables are not polymorphic in Java; they do not override one another."

    – Andrew Tobilko
    Nov 25 '18 at 11:46











  • just remove the field "Actual.template".

    – Alexei Kaigorodov
    Nov 25 '18 at 12:37
















3
















This question already has an answer here:




  • Overriding member variables in Java

    10 answers



  • Java abstract class fields override

    6 answers




I'm coming up with a set of Java classes for templating.



I have an abstract template:



package Test;

public class Abstract {
protected String template = "ABSTRACT TEMPLATE";
public Abstract() {

}

public void Render() {
System.out.println("FROM ABSTRACT RENDER:");
System.out.println(this.template);
}
}


And an actual one:



package Test;

public class Actual extends Abstract {
protected String template = "ACTUAL TEMPLATE";
public Actual() {
super();
System.out.println("FROM ACTUAL CONSTRUCTOR:");
System.out.println(this.template);
}

public void Test() {
System.out.println("FROM ACTUAL TEST:");
System.out.println(this.template);
}
}


I'm having trouble getting the extending class to reset the value of a protected attribute (in this case template) and letting be used by the abstract method.



This is my use case:



Actual actual = new Actual();
actual.Render();
actual.Test();


And this is my output:



FROM ACTUAL CONSTRUCTOR:
ACTUAL TEMPLATE
FROM ABSTRACT RENDER:
ABSTRACT TEMPLATE <--- this is the problem, why not "ACTUAL"?
FROM ACTUAL TEST:
ACTUAL TEMPLATE


How do I override that value from the child class? If I don't set it to anything, calling the abstract method will always say the attribute is null, even though it's set in the child class.










share|improve this question















marked as duplicate by Robin Green, Andrew Tobilko 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 25 '18 at 11:46


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.



















  • Related stackoverflow.com/q/9414990/5168011

    – Guy
    Nov 25 '18 at 11:46








  • 1





    "Variables are not polymorphic in Java; they do not override one another."

    – Andrew Tobilko
    Nov 25 '18 at 11:46











  • just remove the field "Actual.template".

    – Alexei Kaigorodov
    Nov 25 '18 at 12:37














3












3








3









This question already has an answer here:




  • Overriding member variables in Java

    10 answers



  • Java abstract class fields override

    6 answers




I'm coming up with a set of Java classes for templating.



I have an abstract template:



package Test;

public class Abstract {
protected String template = "ABSTRACT TEMPLATE";
public Abstract() {

}

public void Render() {
System.out.println("FROM ABSTRACT RENDER:");
System.out.println(this.template);
}
}


And an actual one:



package Test;

public class Actual extends Abstract {
protected String template = "ACTUAL TEMPLATE";
public Actual() {
super();
System.out.println("FROM ACTUAL CONSTRUCTOR:");
System.out.println(this.template);
}

public void Test() {
System.out.println("FROM ACTUAL TEST:");
System.out.println(this.template);
}
}


I'm having trouble getting the extending class to reset the value of a protected attribute (in this case template) and letting be used by the abstract method.



This is my use case:



Actual actual = new Actual();
actual.Render();
actual.Test();


And this is my output:



FROM ACTUAL CONSTRUCTOR:
ACTUAL TEMPLATE
FROM ABSTRACT RENDER:
ABSTRACT TEMPLATE <--- this is the problem, why not "ACTUAL"?
FROM ACTUAL TEST:
ACTUAL TEMPLATE


How do I override that value from the child class? If I don't set it to anything, calling the abstract method will always say the attribute is null, even though it's set in the child class.










share|improve this question

















This question already has an answer here:




  • Overriding member variables in Java

    10 answers



  • Java abstract class fields override

    6 answers




I'm coming up with a set of Java classes for templating.



I have an abstract template:



package Test;

public class Abstract {
protected String template = "ABSTRACT TEMPLATE";
public Abstract() {

}

public void Render() {
System.out.println("FROM ABSTRACT RENDER:");
System.out.println(this.template);
}
}


And an actual one:



package Test;

public class Actual extends Abstract {
protected String template = "ACTUAL TEMPLATE";
public Actual() {
super();
System.out.println("FROM ACTUAL CONSTRUCTOR:");
System.out.println(this.template);
}

public void Test() {
System.out.println("FROM ACTUAL TEST:");
System.out.println(this.template);
}
}


I'm having trouble getting the extending class to reset the value of a protected attribute (in this case template) and letting be used by the abstract method.



This is my use case:



Actual actual = new Actual();
actual.Render();
actual.Test();


And this is my output:



FROM ACTUAL CONSTRUCTOR:
ACTUAL TEMPLATE
FROM ABSTRACT RENDER:
ABSTRACT TEMPLATE <--- this is the problem, why not "ACTUAL"?
FROM ACTUAL TEST:
ACTUAL TEMPLATE


How do I override that value from the child class? If I don't set it to anything, calling the abstract method will always say the attribute is null, even though it's set in the child class.





This question already has an answer here:




  • Overriding member variables in Java

    10 answers



  • Java abstract class fields override

    6 answers








java inheritance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 11:45









Robin Green

22.6k876156




22.6k876156










asked Nov 25 '18 at 11:40









Display nameDisplay name

12410




12410




marked as duplicate by Robin Green, Andrew Tobilko 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 25 '18 at 11:46


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 Robin Green, Andrew Tobilko 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 25 '18 at 11:46


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.















  • Related stackoverflow.com/q/9414990/5168011

    – Guy
    Nov 25 '18 at 11:46








  • 1





    "Variables are not polymorphic in Java; they do not override one another."

    – Andrew Tobilko
    Nov 25 '18 at 11:46











  • just remove the field "Actual.template".

    – Alexei Kaigorodov
    Nov 25 '18 at 12:37



















  • Related stackoverflow.com/q/9414990/5168011

    – Guy
    Nov 25 '18 at 11:46








  • 1





    "Variables are not polymorphic in Java; they do not override one another."

    – Andrew Tobilko
    Nov 25 '18 at 11:46











  • just remove the field "Actual.template".

    – Alexei Kaigorodov
    Nov 25 '18 at 12:37

















Related stackoverflow.com/q/9414990/5168011

– Guy
Nov 25 '18 at 11:46







Related stackoverflow.com/q/9414990/5168011

– Guy
Nov 25 '18 at 11:46






1




1





"Variables are not polymorphic in Java; they do not override one another."

– Andrew Tobilko
Nov 25 '18 at 11:46





"Variables are not polymorphic in Java; they do not override one another."

– Andrew Tobilko
Nov 25 '18 at 11:46













just remove the field "Actual.template".

– Alexei Kaigorodov
Nov 25 '18 at 12:37





just remove the field "Actual.template".

– Alexei Kaigorodov
Nov 25 '18 at 12:37












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen