2
There is a way to copy only superclass content in another class of the same type through a subclass instance in Java, without accessing getters and setters one by one?
Example:
public class A {
private Integer id;
private String nome;
//outros
}
public class B extends A {
private String age;
}
I need to do something like:
B objB = new B();
objB.setId(10);
objB.setNome("Jonh Doe");
objB.setAge(21);
A objA = // AQUI COPIAR DE objB apenas o conteúdo da superclasse A
Cloning maybe?
– user28595
I used Clone @Articuno, but mysteriously the object came with the attributes of the subclass and the application launched an Exception because Hibernate did not recognize the entity...
– touchmx
You can put the use case on what you’re trying to do ?
– Isac
The simplest way to do this is not to use inheritance but rather composition. That is to say,
public class B { private A a; private String age; }
.– Victor Stafusa