Java inheritance - how to set value from instance of child class to be shared (same) by other child classes?

I would like to do something like this:

Class A 
Class B extends A
Class C extends A

B b = new B();
C c = new C();

b->setField("foo");
c->getField; //return "foo"

Is it somehow possible to implement these classes?

I need it for persistance in hibernate, to make entity split into two entites and have each in its own table. And I need to have some fields same (id, some FK, some values). So I would like to set these fields in one subclass and have the same values in second subclass. All subclasses need to have/share exactly same value.

Any idea?

Answers


you can use below code:

public class A {

    private static String field;

    public static String getField() {
        return field;
    }

    public static void setField(String field) {
        A.field = field;
    }
}


public class B extends A{

}


public class C extends A{

}


public class Test {

    public static void main(String[] args){
        B.setField("Thank you"); // or 
           new B().setField("Thank you"); 

        System.out.println(C.getField()); // or
            System.out.println(new C().getField());

  // it will work for object and for class as well
    }
}

output : Thank you


Regardless of whether that is possible or not from a Java perspective (it is, but in a round-about way), it's not the correct way to handle what you require. If you're using foreign keys and update a value in some persisted entity, the corresponding value should be updated in the entities that use it as a foreign key. Just let JPA/Hibernate take care of this for you. I suggest you read up on the Java Persistence API in the Java EE Tutorial as well as the Hibernate guide to learn what it does and how data shared/divided across different entity classes and/or tables should be handled.

EDIT: since everyone keeps hammering on about static fields which is entirely not what the OP wants, I'll provide a sample of how I'd do this...

public class Key {
    private String value;
    public Key(final String value) {
        this.value = vale;
    }
    public String getValue() {
        return value;
    }
    public void setValue(final String value) {
        this.value = value;
    }
}

public class A {
    private final Key field;
    public A(final Key field) {
        this.field = field;
    }
    public setField(final String value) {
        field.setValue(value);
    }
}

public class B {
    private final Key field;
    public B(final Key field) {
        this.field = field;
    }
    public setField(final String value) {
        field.setValue(value);
    }
}

//When using...
Key k = new Key("value");
A a = new A(k);
B b = new B(k);

But like I said, this is only a solution on Java level but won't work properly in the context of persistance. You need to read up on the use of primary and foreign keys with regards to JPA and Hibernate. Judging from the notation used in the original question I'd say the poster comes from a non-Java background, therefor it is important that he learns how to properly use these technologies before accidentally reinventing the wheel.


You can try making your field static. Then you will be able to access it this way.


If the field is static, then they will share the same value. Not to up on Hibernate, so I am not sure if that changes anything from a persistence standpoint.


Hibernate would normally not manage static fields. In that case, you could just add a static getter and setter to class A. If you don't shadow them in a subclass (there's no overriding of static methods) calling C.setField("foo"), A.setField("foo"), c.setField("foo") (note that c is an instance) etc. will all be calls to the same method.

In case you want to provide common properties for your entities, but the entities can have different values, try to use the @MappedSuperClass annotation.


Need Your Help

A kind of thread pool

c++ windows multithreading threadpool

I used to call CreateThread() for all my threads, and WaitForMultipleObjects(), an leave the routine.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.