Instantiate superclass variable in Java - java

I have an abstract class which contains a variable 'name' that I want my child classes to initialize. Which of these would be the best way to do so.
Option 1. Use superclass constructor for initialization
#Getter
abstract class A {
private final String name;
protected A(String name) {
this.name = name;
}
}
class B extends A {
private static final NAME = "Raylan";
private final int age;
public B(int age) {
super(NAME);
this.age = age;
}
}
Option 2. Use a getter method.
abstract class A {
private final String name;
public abstract String getName();
}
class B extends A {
private static final NAME = "Raylan";
private final int age;
public B(int age) {
this.age = age;
}
#Override
public String getName() {
return NAME;
}
}

Having a private name in A without a way to access it is useless. Assuming a fully functional getName() in A, consider multiple constructors in B:
class B extends A {
private static final String NAME = "Raylan";
private final int age;
public B(String name, int age) {
super(name);
this.age = age;
}
public B(int age) {
this(NAME, age);
}
public int getAge() {
return age;
}
}
This allows re-use of A, otherwise there doesn't seem much point in B extending A. Composition, instead of inheritance, could be another option.

Related

Adding test in JUnit that fails if Serialized Object changes

I have the following class that is created with the usage of a builder:
public class Foo {
private String name;
private int age;
public String getName() { return name; }
public int getAge() { return age; }
private Foo(Builder b) {
name = b.name;
age = b.age;
}
public static final class Builder {
private String name;
private int age;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder age(int age) {
this.age = age;
return this;
}
}
}
Now I want to add a JUnit test for this where if this class were to change (via new field were to be added, some other changes made to this) that would also be reflected once the class got serialized, I want that test to fail to catch that change. I am not aware of any libraries that can do this, how can this be done?

Implementing interface with inherited type in Java

How can I create addWorker in interface? OfficeWorker inherits from Worker and now I want to make interface for few Workers. I don't know how to do it correct. void addWorker(Worker worker) and void addWorker(Worker worker) are wrong.
Interface
public interface TypeOfWorker {
void addWorker(Worker worker);
}
OfiiceWorker
public class OfiiceWorker extends Worker implements TypeOfWorker {
private int officeID;
private int intelectl;
private List<OfiiceWorker> ofiiceWorkers = new ArrayList<>();
public OfiiceWorker(String name, String surname, int age, int experience, String street, int building,
int local, String city, int officeID, int intelectl) {
super(name, surname, age, experience, street, building, local, city);
this.officeID = officeID;
this.intelectl = intelectl;
}
#Override
public void addWorker(OfiiceWorker ofiiceWorker) {
ofiiceWorkers.add(ofiiceWorker);
}
}
Worker
public abstract class Worker {
private int identifier;
private String name;
private String surname;
private int age;
private int experience;
private String street;
private int building;
private int local;
private String city;
public Worker() {
}
public Worker(String name, String surname, int age, int experience, String street, int building, int local,
String city) {
setIdentifier();
this.name = name;
this.surname = surname;
this.age = age;
this.experience = experience;
this.street = street;
this.building = building;
this.local = local;
this.city = city;
}
public void setIdentifier() {
this.identifier = (int) (System.currentTimeMillis() / 1000);
}
}
You should use generics to let compiler relax with your types. First make your interface generic:
public interface TypeOfWorker<T extends Worker> {
void addWorker(T worker);
}
Then declare the generic type of your class:
public class OfiiceWorke extends Worker implements TypeOfWorker<OfiiceWorke> {
With these modifications the below is fine:
#Override
public void addWorker(OfiiceWorke worker) {

[JAVA]How to get generic class typed name

I would like to get E class name in String value with java reflection.
For example, if i create Node typed in Person, getClassName() has to return "Person"
Someone can help me?
public class Node<E extends AbstractNode> {
String getClassName() {
String name = ??
}
private String alias;
public Node() {
}
}
public abstract class AbstractNode {
}
public class Person extends AbstractNode {
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}
Add a field to your Node class
private static final Class<T> type;
And add this to your constructor signature
public Node(Class type) {
this.type = type;
}
Lastly, create the getClassName method in your Node class:
public String getClassName(){
return this.type.getName();
}
Edit:
As a sidenote, your AbstractNode class is not really necessary (it doesn't do anything) and therefore neither is the extend in Person.

Java casting enum to a class

I have a class which is identical to enum. The only difference is that I create the class so that I can dynamically create the enums. What I want is to override the cast operation of enum so that I can give the enum instance to a method where it gets the class instance.
Example:
public enum SpecialEnum {
FIRST("First"), SECOND("Second");
private String name;
SpecialEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class SpecialClass {
private String name;
public SpecialClass(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public void displayName(SpecialClass specialClass) {
System.out.println(specialClass.getName());
}
Lets say that the SpecialClass instances are coming from a server where I display them. But I also want to use my pre-defined Special enum classes as well. I know that I could create static instances of SpecialClass and use them but not that it looks messy, also using enums are beneficial to my occasion as well. Is there a way to override casting operation of the enum class of a work around maybe?
Extract an interface:
public interface Named {
public String getName();
}
public class SpecialClass implements Named {
private String name;
public SpecialClass(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
}
public enum SpecialEnum implements Named {
FIRST("First"), SECOND("Second");
private String name;
SpecialEnum(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
}
public void displayName(Named specialClass) {
System.out.println(specialClass.getName());
}

how to convert this multiple inheritance to interfaces in Java?

I want to implement the following multiple inheritance case:
(took from http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/)
Person Employee
|_________________|
|
Nurse
at the end I want to print the salaries from Employee and Nurse. In C++ it is easy to be done by using multiple inheritance, but I have problems with Java.
I have the following codes:
public class Person{
protected String name;
protected int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
and the interface:
public interface Employee{
public double getSalary();
}
and the class Nurse:
public class Nurse extends Person implements Employee{
private double salary;
public Nurse(String name, int age, double salary){
super(name,age);
this.salary=salary;
}
#Override
public double getSalary(){
return salary;
}
}
but I do not have a clue how to make the Employee to print its salary, because it is an interface. I do not want to use other abstract class called Employee. How to fix this?
Thanks
Assuming that all employees are people (unless you are hiring monkeys!!), could you not chain the classes?
public class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
public class Employee extends Person {
private double salary;
public double getSalary(){
return salary;
}
}
public class Nurse extends Employee {
}
Java allows multiple inheritance of interfaces only, so in order to implement that you have to define an interface for every class you'd like to combine and compose them in a single class by delegating all methods derived from the partial interfaces to their implementation.
Person interface - as you've defined it
PersonImpl class:
public final class PersonImpl implements Person {
private final String name;
private final int age;
public PersonImpl(String name, int age) {
this.name = name;
this.age = age;
}
#Override
public String getName() {
return name;
}
#Override
public int getAge() {
return age;
}
}
EmployeeImpl class:
public final class EmployeeImpl implements Employee {
private final double salary;
public EmployeeImpl(double salary) {
this.salary = salary;
}
#Override
public double getSalary() {
return salary;
}
}
Nurse class - composed from to other classes and delegates the functionality to them:
public class Nurse implements Employee, Person {
private final Employee employee;
private final Person person;
public Nurse(String name, int age, double salary) {
person = new PersonImpl(name, age);
employee = new EmployeeImpl(salary);
}
#Override
public double getSalary() {
return employee.getSalary();
}
#Override
public String getName() {
return person.getName();
}
#Override
public int getAge() {
return person.getAge();
}
}
It's advisable to define an interface for every class you code in order to able that approach in the future.

Categories