Subclass instance to Superclass - java

How do I create subclass objects, based on an superclass objects?
eg:
class Super {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Sub extends Super {
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Super sup = new Super();
sup.setId(1);
sup.setName("Super");
Sub sub = new Sub();
System.out.println(sub.getName());
}
}
How can I create a 'Sub' object with the properties of a 'Super' created earlier?
Or should I pass the properties manually, like:
sub.setName(sup.getName());
sub.setId(sup.getId());

you could add a copy constructor to Super Class
public class Super {
private int id;
private String name;
public Super(String id, String name) {
this.id = id;
this.name = name;
}
public Super(Super other) {
this.id = other.id;
this.name = other.name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and then use this constructor in Sub class
class Sub extends Super {
public Sub(Super other) {
super(other);
}
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and you can call
Sub sub = new Sub(sup);

I would go with creating a static method in Sub:
Sub.fromSuper(Super s, String last)

I would use apache commons
BeanUtils.copyProperties(toBean, fromBean);
I wouldn't add a method to the class itself unless its really needed on every object. BeanUtils seem appropriate as it appears like something needed only in a specific situation.
In case that you really need the behaviour on every object, than implementing a copy constructor is a way to go

Related

How to access to an instance of another class which has constructor of another class

I have a class A with setters and getters.
I have a class B where I call a constructor from A and set some variables.
I have a class C where I want to call variables set up in class B.
How can i call that in class C?
public class A{
private String name;
private String status;
private int id;
public A(String status, String name) {
this.status = status;
this.name = name;
}
public String getStatus() {
return status;
}
public String getName() {
return name;
}
public void setStatus(String status) {
this.status = status;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}}
public class B{
A a;
public void add() {
a= new A("Test", "Test");
}
public void add_to() {
int id = Math.abs(new Random().nextInt());
a.setId(id);
}
public class C{
public void add() {
//How can I call variable id declared in class B???
}
}
Instead of just adding more methods in the class B I would like to continue to use the same constructor with stored variables that I have created in class B already.

How to prepare the third list using two lists using java 8

I have a two different list and using those i have prepare third list using streams.
Student.java
public class Student {
int id;
String name;
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentLoc.java
public class StudentLoc {
int id;
String loc;
public StudentLoc(int id, String loc) {
super();
this.id = id;
this.loc = loc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
and I have third class like below.
StudentDetLoc.java
public class StudentDetLoc {
int id;
String name;
String Loc;
}
I have to compare the Student list and StudentLoc list by using id property.
if id present in both list then i have to prepare StudentDetLoc list using both lists.
My approach would be:
Make set of student ids from first list using streams() and map()
Filter filter() second list using set obtained from step 1
Use forEach() as terminating operation of step 2 and append to final 3rd list (keeping only id, name and Loc).

How to use a constructor that already exists?

I am building a simple app for children with Firebase and I'm constantly getting this error:
Android Firebase Database exception: not define a no-argument constructor
I have a class Activities and two other helper classes HomeActivities and OutsideActivities. This is my code for my classes:
public class Activities {
private String type;
private int count;
public Activities() { }
public Activities(String type, int count) {
this.type = type;
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
class HomeActivities {
private String name;
private int noOfChildren;
HomeActivities() { }
public HomeActivities(String name, int noOfChildren) {
this.name = name;
this.noOfChildren = noOfChildren;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfChildren() {
return noOfChildren;
}
public void setNoOfChildren(int noOfChildren) {
this.noOfChildren = noOfChildren;
}
}
class OutsideActivities {
private String name;
private int noOfChildren;
public OutsideActivities() { }
public OutsideActivities(String name, int noOfChildren) {
this.name = name;
this.noOfChildren = noOfChildren;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfChildren() {
return noOfChildren;
}
public void setNoOfChildren(int noOfChildren) {
this.noOfChildren = noOfChildren;
}
}
}
Please see all the constructors. Even if I already have defined the correct constructor in each class, I get this error. How to solve this? Please help me.
There are two methods in which you can get rid of this error.
Make both inner classes static.
Make both inner classes independent (each class in it's own separte file).
That's it!
Constructor of HomeActivities() has default visibility. public keyword is missed. Database ORM provider can't instantiate objects when the constructor is not public.
Should be:
class HomeActivities {
private String name;
private int noOfChildren;
public HomeActivities() { }
All other suggestions are valid as well. You should externalize classes in separate files.

create unique id without using constructure and setters

class person {
private int id ;
private String name;
private boolean gender;
public person() {
}
public AtomicLong getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
I want to create unique id in this class without using constructors and setters.
To construct a person instance, the field initializer will be copied into the constructor. Assuming that's okay, you could use an AtomicInteger and something like,
private static AtomicInteger ai = new AtomicInteger(0);
private int id = ai.incrementAndGet();
you could add:
private static int ID_GENERATOR = 0;
then, in the constructor, you will use:
public person() {
id = ID_GENERATOR++;
}

Java create a dynamic ObservableList

is it possible to create a dynamic ObservableList with relative StringProperty?
For example, using the code below, how is it possible to recreate it dynamically and add new StringProperty if necessary?
private final ObservableList<Record> recordList = FXCollections.observableArrayList();
public static class Record {
private static int trackId;
private final SimpleIntegerProperty id;
private final SimpleStringProperty name;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Record(String name, String lastName, String email) {
this.id = new SimpleIntegerProperty(trackId);
this.name = new SimpleStringProperty(name);
this.lastName = new SimpleStringProperty(lastName);
this.email = new SimpleStringProperty(email);
trackId++;
}
public int getId() {
return this.id.get();
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return this.name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getLastName() {
return this.lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public String getEmail() {
return this.email.get();
}
public void setEmail(String email) {
this.email.set(email);
}
}
You can't add fields to class even if will use Reflection. Maybe is better to change architecture? Let's see next class:
class Property <T> {
private final T propertyValue;
private final String propertyName;
public Property (String name, T value) {
this.propertyName = name;
this.propertyValue = value;
}
public T getValue(){
return propertyValue;
}
public String getName(){
return propertyName;
}
}
This class helps you to create new property and store it. Now, you can create List of properties and store it in class Record. And now you can dynamically add new properties. It is more flexible in my opinion.

Categories

Resources