This question already has answers here:
Is there a way to override class variables in Java?
(17 answers)
Closed 6 years ago.
In Java, I have had several projects recently where I used a design pattern like this:
public abstract class A {
public abstract int getProperty();
}
public class B extends A {
private static final int PROPERTY = 5;
#Override
public int getProperty() {
return PROPERTY;
}
}
Then I can call the abstract method getProperty() on any member of class A. However, this seems unwieldy - it seems like there should be some way to simply override the property itself to avoid duplicating the getProperty() method in every single class which extends A.
Something like this:
public abstract class A {
public static abstract int PROPERTY;
}
public class B extends A {
#Override
public static int PROPERTY = 5;
}
Is something like this possible? If so, how? Otherwise, why not?
You cannot "override" fields, because only methods can have overrides (and they are not allowed to be static or private for that).
You can achieve the effect that you want by making the method non-abstract, and providing a protected field for subclasses to set, like this:
public abstract class A {
protected int propValue = 5;
public int getProperty() {
return propValue;
}
}
public class B extends A {
public B() {
propValue = 13;
}
}
This trick lets A's subclasses "push" a new value into the context of their superclass, getting the effect similar to "overriding" without an actual override.
Related
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 1 year ago.
For a project in Java I want to create a private reference to an array in a superclass, but do not implement it. I want to do the implementation in the childclass, so I created a getter method for that array in the superclass. In the childclasses constructor, I implement the array and now the problem is, that the array reference in the superclass stays null even if I added an array to the reference. I want to use the array later in the superclass.
I will add some code for better understanding.
I understand my fault that this is not easily possible in Java like this, but is there any solution to "edit" the array like this by it's reference or do I have to do this with a setter method?
//parent class
public abstract class Superclass {
private Test[] test_Values;
public Measurement[] getTestValues() {
return this.test_Values;
}
}
//child class
public class ChildClass extends SuperClass {
public ChildClass() {
Test[] measures = super.getTestValues();
measures = new Test[4];
}
}
Initialize your array in the parent constructor:
public abstract class Superclass {
private Test[] test_Values;
protected Superclass(int capacity) {
this.test_Values = new Test[capacity];
}
public Test[] getTestValues() {
return this.test_Values;
}
}
Then, you have to use that non-default constructor in your child classes.
public class ChildClass extends SuperClass {
public ChildClass() {
super(4);
}
}
Or, to get additional control over how you initialize the values in the array, you could add an abstract method that is used in the parent's constructor:
public abstract class Superclass {
private Test[] test_Values;
protected Superclass() {
this.test_Values = initializeArray();
}
public Test[] getTestValues() {
return this.test_Values;
}
protected abstract Test[] initializeArray();
}
The child class then needs to implement that abstract method:
public class ChildClass extends SuperClass {
#Override
protected Test[] initializeArray() {
Test[] testValues = new Test[4];
// calculate elements
return testValues;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am new to java and I have few basic questions:
What memory does a Static variable / method use ?
Does the keyword 'Super' have significance only in scenarios of Method overriding ?
Keyword super can be used for:
Declaring a generic type:
public class Foo<E super Bar> { // <=====
...
}
Calling base class constructor from subclass constructor:
public class Bar {
private int id;
public Bar(int id) {
this.id = id;
}
}
public class Foo extends Bar {
public Foo(int id) {
super(id); // <=====
}
}
Access a field in a base class, when subclass has hidden the field:
public class Foo extends Bar {
private int id;
public int getFooId() {
return this.id;
}
public int getBarId() {
return super.id; // <=====
}
}
Calling base class method from overridden subclass method:
public class Bar {
public void doGreatWork() {
...
}
}
public class Foo extends Bar {
#Override
public void doGreatWork() {
...
super.doGreatWork(); // <=====
...
}
}
Referencing base class method when subclass has overridden the method
public class MultiBar extends Bar {
public void doGreatWork() {
list.stream().forEach(super::doGreatWork); // <=====
}
}
To access the data members of parent class when both parent and child class have member with same name, to explicitly call the no-arg and parameterized constructor of parent class, to access the method of parent class when child class has overridden that method.It can be used to access the variables of parent class, to invoke constructor of parent class and can be used in case of method overriding.
What memory does a Static variable / method use ?
if we want to access or call the method, we don't need to create object, simply we can use static keyword while writing method like ->> public static show().
when we use the static keyword the class will be called and executed the method.
the static means we can't change the value , we can use static keyword to variables also, once we use static keyword that means we cant change the values often.
example:
public class static example
{
public static void main(String[] args)
{
xyz.i = 10;
xyz.show();
}
}
class xyz
{
static int i;
public static void show()
{
System.out.println("Stackoverflow example by Me");
}
}
Does the keyword 'Super' have significance only in scenarios of Method overriding ?
when we want to call the method from parent class, the child class method will be executed in method overloading, because the parent and child classes will have same method name. that's why the method overloading happens. to overcome this problem we use super keyword to call the parent class method even having parent class and child class same name. we can easily call and execute parent class method
example:
class A {
int i;
public void show(){
System.out.println("A");
}
}
class B extends A {
int i;
public void show(){
super.show()
System.out.println("B");
}
}
public class overriding example {
public static void main(String[]args) {
B obj1 = new B();
obj1.show();
}
}
I have a base class
public class base
{
//some stuff
}
and several subclasses
public class sub1 extends base
{
static int variable;
}
public class sub2 extends base
{
static int variable;
}
etc
The static int variable exists in every subclass because I store in it information that is characteristic for every subclass. But it would be better if there was a way to move static int variable to base class in the way that it still will be different for every subclass.
In the way that it is now I am repeating myself, when adding some another subclass, it's a bad practice.
So anyone has some idea how to acomplish this? Maybe there's a design pattern that fits to this situation?
You cannot move all the different static variables from derived classes into the base class, because static variables are one-per-class; you want your variables to be one-per-subclass, which is not allowed.
You could work around this issue by defining a registry of subclasses in your base class, and store the int for each subclass there. However, this would add a lot more complexity, and it is not clear how you would differentiate between subclasses in the superclass.
Your current solution appears optimal.
Don't use a static field for this - that's not the way to go, because static fields of a subclass do not "override" those of a super class.
Instead, because the values are constant for a given class, use a final instance field:
public class Base {
protected final int variable;
public Base() {
this(5);
}
protected Base(int v) {
variable = v;
}
}
public class Sub1 extends Base {
private static int v = 7;
public Sub1() {
super(v);
}
}
Now the variable is fixed and accessible to all instances.
You can certainly move variable into the base class, but it cannot be static. Alternatively, you can make static getters which you override in each subclass. Here is an example of both:
public class base {
protected int variable;
protected static int getVariable() {
return -1;
}
}
public class Sub1 extends base {
public Base() {
variable = 0;
}
protected static int getVariable() {
return 0;
}
}
public class Sub2 extends base {
public Sub2() {
variable = 1;
}
protected static int getVariable() {
return 1;
}
}
As a design principle, it is somewhat rare (in my opinion) that you genuinely want static methods. Usually you will have some instance of the class around that you are working with. If you want a whole bunch of objects to share some common behavior which you configure at runtime, you might want to check out the flyweight pattern.
I have an abstract class that should implement a public field, this field is an interface or another abstract classe.
something like this:
public abstract class GenericContainer {
public GenericChild child;
}
public abstract class GenericChild {
public int prop1=1;
}
public abstract class SpecialChild extends GenericChild {
public int prop1=2;
}
Now i have another specialized class Container:
public abstract class SpecialContainer extends GenericContainer {
public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
}
Java allow me to compile this, and i IMAGINE that the field child in SpecialContainer is automatically overloading the field child of the GenericContainer...
The questions are:
Am i right on this? The automatic 'overloading' of child will happen?
And, more important question, if i have another class like this:
public class ExternalClass {
public GenericContainer container=new SpecialContainer();
public int test() {
return container.child.prop1
}
}
test() will return 1 or 2? i mean the GenericContainer container field what prop1 will call, the generic or the special?
And what if the special prop1 was declared as String (yes java allow me to compile also in this case)?
Thanks!
In Java, data members/attributes are not polymorphic. Overloading means that a field will have a different value depending from which class it's accessed. The field in the subclass will hide the field in the super-class, but both exists. The fields are invoked based on reference types, while methods are used of actual object. You can try it yourself.
It's called, variable hiding/shadowing, for more details look on here
It isn't overriding anything, you're just hiding the original field at the current class scope. If you use a variable with the subtype you will still be able to access the original property. Example:
abstract class GenericContainer {
public GenericChild child;
}
abstract class GenericChild {
public int prop1=1 ;
}
class SpecialChild extends GenericChild {
public int prop1=2;
}
class SpecialContainer extends GenericContainer {
public SpecialChild child;
}
public class Main {
public static void main( String ... args ) {
GenericContainer container = new SpecialContainer();
container.child = new SpecialChild();
System.out.println( container.child.prop1 );
SpecialChild child = (SpecialChild) container.child;
System.out.println( child.prop1 );
}
}
This prints 1 and then 2.
From SpecialChild you would also be able to go up one level using super:
class SpecialChild extends GenericChild {
public int prop1=2;
public int getOriginalProp1() {
return super.prop1;
}
}
Regarding
....and i IMAGINE that the field "child" in SpecialContainer is automatically overloading the field 'child' of the GenericContainer...
No. Fields don't get overridden, only methods do.
This is one reason why use of (overridable) getter and setter methods are preferred to direct access to fields. Your fields should almost all be private.
As for your design, there's no need for your SpecialContainer class to have a SpecialChild field, but instead the SpecialChild object should be placed in the GenericChild field.
Why nobody is observing that program will throw NullPointerException.
subclass's field with same name will hide super class's field. There is no overriding with field. Overriding is only possible with methods.
Original Code by Author:
public abstract class GenericContainer {
public GenericChild child;
}
public abstract class GenericChild {
public int prop1=1;
}
public abstract class SpecialChild extend GenericChild {
public int prop1=2;
}
public abstract class SpecialContainer extends GenericContainer {
public SpecialChild child=new SpecialChild(); //PAY ATTENTION HERE!
}
public class ExternalClass {
public GenericContainer container=new SpecialContainer();
public int test() {
return container.child.prop1
}
}
Java allow me to compile this, and i IMAGINE that the field "child" in
SpecialContainer is automatically overloading the field 'child' of the
GenericContainer...
Firstly, Inheritence doesn't apply to variables. Fields(Insatnce variables) are not overridden in your sub-class.they are only visible in your subclass if they are marked with either public, protected or default.
To answer your question it maintains both instances. And depending on how you refer to the container (either through the abstract or the impl) determines which variable you are referring to.
public class Test {
public abstract class Container{
public Generic gen = new Generic();
}
public class ContainerImpl extends Container{
public GenericImpl gen = new GenericImpl();
}
public class Generic{
public int prop = 0;
}
public class GenericImpl extends Generic{
public int prop = 1;
}
public Test(){
Container c = new ContainerImpl();
System.out.println(c.gen.prop); // Outputs "0"
System.out.println(((ContainerImpl)c).gen.prop); // Output "1"
}
public static void main(String[] args) {
new Test();
}
}
The bigger question at hand is, why would you design something like this? I'm assuming you are asking from a theoretical perspective.
My 2 cents, this isn't great OO design. You would be better off making the public variables private and assigning their values through a constructor or property setter. As-is, it will lead to unexpected results in your code.
This question is a common issue, and I have tried to look at some thread as Is Java "pass-by-reference" or "pass-by-value"? or How to change an attribute of a public variable from outside the class
but in my case I need to modify a boolean variable, with a Singleton instance.
So far I have a class, and a method which changes the boolean paramter of the class. But I would like to separate this mehod in a manager. The scheme is something like:
public class Test{
private boolean b;
public String getb(){}
public void setb(){}
String test = ClassSingleton.getInstance().doSomething();
}
public class ClassSingleton{
public String doSomething(){
//here I need to change the value of 'b'
//but it can be called from anyclass so I cant use the set method.
}
}
Thanks,
David.
If I understand your requirement - this can solve your problem:
public interface IUpdatable
{
public void setB(boolean newValue);
}
public class Test implements IUpdatable
{
private boolean b;
public String getb(){}
public void setB(boolean newValue) {this.b = newValue;}
}
public class ClassSingleton
{
public String doSomething(IUpdatable updatable)
{
updatable.setB(true);
...
}
}
This way the Singleton does not need to know your Test class - it just knows the interface IUpdatable that supports setting the value of B. Each class that needs to set its B field can implement the interface and the Singleton can update it and remain oblivious to its implementation.
You could extract public void setb(){} into an interface (let's call it BSettable), make Test implement BSettable, and pass an argument of type BSettable into doSomething.
Alternatively, you could make b into an AtomicBoolean and make doSomething accept (a reference to) an AtomicBoolean.
Define b as static variable.
Then call Test.b = value
Perhaps:
public class Test {
private static boolean b;
public static String getB() {}
public static void setB() {}
}
should help? Static fields and methods can be called without an instance (i.e. Test.setB();).
I think your question is not very clear and your sample code is really badly done. Do you actually mean something like this?
public class Test{
private boolean b;
public boolean getb(){return b;}
public void setb(boolean b){this.b = b;}
String test = ClassSingleton.getInstance().doSomething(this);
}
public class ClassSingleton{
private static ClassSingleton __t__ = new ClassSingleton();
private ClassSingleton() {}
public String doSomething(Test t){
t.setb(true);
return null;
}
public static ClassSingleton getInstance(){
return __t__;
}
}
Do you mean your manager is a singleton? or your test class should be singleton? Please be more specific