Can a Java class be forced to implement a specific property?

1

Is there any way to force a class to implement a property of a certain type?

For example, I have the classes Xxx and Zzz , and I want both of them to be forced to implement the property name of type String .

  

Edited

What I want is Xxx and Zzz (and any other class that is subsequently created in the package), have the following structure:

package xxx.tests;

public class Foo {

    private final static String NAME;

    static {
        NAME = "mi name";
    };


    public static String get(String name) {

        if (name.equals("")) {
            return name;
        }

        return NAME;
    }

} // class

This means that all these classes have a property of a specific type that they must initialize and the method with a defined argument and return type. For the method implemented an interface.

    
asked by Orici 10.11.2018 в 01:26
source

2 answers

1

The properties are not implemented (interface), they extend (extends).

public class Padre {
    protected String name;
}
public class Xxx extends Padre {
    public Xxx() {      
    }
}
public class Zzz extends Padre {
    public Zzz() {  
    }
}
public class Main {
    public static void main(String args[]) {
        Xxx x=new Xxx();
        Zzz z=new Zzz();
        x.name="algoX";//Heredo la propiedad name
        z.name="algoZ";//Heredo la propiedad name
    }
}

In contrast with interfaces:

public interface IPadre {
    public String getName();
    public void setName(String name);
}
public class Xxx implements IPadre {
    private String name;//Esto podria no existir en esta clase
    @Override
    public String getName() {
        return name;//Esto podria no existir en esta clase
    }
    @Override
    public void setName(String name) {
        this.name=name;//Esto podria no existir en esta clase
    }
}
public class Zzz implements IPadre {
    private String name;//Esto podria no existir en esta clase
    @Override
    public String getName() {
        return name;//Esto podria no existir en esta clase
    }
    @Override
    public void setName(String name) {
        this.name=name;//Esto podria no existir en esta clase
    }
}
public class Main {
    public static void main(String args[]) {
        Xxx x=new Xxx();
        Zzz z=new Zzz();
        x.setName("algoX");
        z.setName("algoZ");
        System.out.println(x.getName());
        System.out.println(z.getName());
    }
}

As you can see with extends , a property is inherited . And with implements a method is implemented .

    
answered by 10.11.2018 в 02:00
0

You can create an abstract class that has as an attribute "name" of type String and that is called ClassFather or something like that, and that the classes xxx and zzz inherit from it by means of extends, all subclasses of ClassFather inherit the attributes and methods . I do not know if it is convenient to use attributes in the interfaces, I think it can generate errors correct me if I'm wrong

    
answered by 10.11.2018 в 05:54