Is it possible to automatically generate Getters / Setters with Java Interface?

Introduction

In conclusion, the interface does not automatically generate Getters and Setters. If you understand the nature of the interface, it's obvious, but I'm personally confused, so I'll summarize it.

The development environment is as follows.

Copy and paste of getter and setter is a hotbed of bugs

I think you often create classes in Java coding. I think there are many people who define the following getters and setters every time. (At least I was)

//Ordinary class
public class SampleClass {
	//Member variables
	private String foo;

	// Getter
	public String getFoo() {
		return this.foo;
	}

	// Setter
	public void setFoo(String foo) {
		this.foo = foo;
	}
}
//Caller
public void SampleMethod {
	//Ordinary class
	SampleClass sample1 = new SampleClass();
	sample1.setFoo("hoge");
	System.out.println("sample1.getFoo(): " + sample1.getFoo());
}

For example, if you have a lot of classes and member variables (fields) and you have to code a lot of simple methods like getters like this, many people will copy and paste.

Even if you make a mistake, it's hard to notice this kind of copy and paste, so I think it's easy to become a hotbed of bugs.

What if you add @Data to a class?

Lombok solves that problem. Just write the following and it will automatically generate getters and setters. (Something like C # properties)

import lombok.Data;

//In class@Add Data
@Data
public class SampleClassWithLombok {
	//Member variables
	private String foo;
}
//Caller
public void SampleMethod {
	// @Class with Data
	SampleClassWithLombok sample2 = new SampleClassWithLombok();
	sample2.setFoo("hoge");
	System.out.println("sample2.getFoo(): " + sample2.getFoo());
}

Now I've reduced the amount of code and crushed the hotbed of bugs.

Please refer to the introduction method of Lombok, which was summarized here before.

-Introduce Spring Tool Suite to Windows -Install Spring Tool Suite on Mac

What if you add @Data to the interface?

I was wondering what happens if I add @Data to the interface. about it.

Since private cannot be specified for the member variable of the interface, I would like to add getter and setter to the member variable that is public. (It doesn't make sense)

Let's add @Data to an interface like this.

//On the interface@Add Data
@Data
public interface SampleInterface {
	//Member variables
	public String bar = "bar";
}

As soon as I turned it on, Eclipse threw the error @Data is only supported on a class..

Since the member variables of the interface are constants in the first place, I think the reason is that we cannot define a setter.

What if I add @Data to the implementation class?

What about the implementation class? Suppose you have an interface and implementation class like this:

//interface
public interface SampleInterface {
}
import lombok.Data;

//In implementation class@Add Data
@Data
public class SampleImpl implements SampleInterface {
	//Member variables
	private String foo;
}
//Caller
public void SampleMethod {
	// @Implementation Class with Data
	SampleInterface sample3 = new SampleImpl();
	sample3.setFoo("hoge");
	System.out.println("sample3.getFoo(): " + sample3.getFoo());
}

When I wrote code like this, I didn't get an error in the interface / implementation class, but I got an error in the caller that method setFoo (String) is undefined in type SampleInterface.

It seems that getter and setter are defined in the implementation class, but it seems that an error occurred because the interface does not have that definition.

Here, I added getter and setter to the interface.

//Getter on interface,Define setter
public interface SampleInterface {
	// Getter
	public String getFoo();

	// Setter
	public void setFoo(String foo);
}

Now the error is gone. However, I ended up defining getters and setters for the interface. With this, you can't suppress copy-pemis.

What if you add @Data to an abstract class?

What about abstract classes? Suppose you have an abstract class and an inheritance class like this.

import lombok.Data;

//In abstract class@Add Data
@Data
public abstract class SampleAbstract {
	//Member variables
	private String foo;
}
//Inheritance Class
public class SampleExtends extends SampleAbstract {
}
//Caller
public void SampleMethod {
	// @Abstract Class with Data
	SampleAbstract sample4 = new SampleExtends();
	sample4.setFoo("hoge");
	System.out.println("sample4.getFoo(): " + sample4.getFoo());
}

When I wrote a code like this, no error occurred. Unlike the interface, abstract classes can have private member variables, so getters and setters may also be auto-defined.

at the end

For ordinary classes, implementation classes, and abstract classes, the getter and setter of member variables can be automatically defined by adding @Data.

However, I found that the interface had to manually define getters and setters.

I think that you can easily avoid it by switching to the abstract class, but when you need getters and setters for the interface, it may be better to review the design of the interface once.

Recommended Posts

Is it possible to automatically generate Getters / Setters with Java Interface?
Getters and setters (Java)
Make it possible to search Japanese sentences with ElasticSearch
How to automatically generate ER diagram when migrating with Rails6
[Java] UTF-8 (with BOM) is converted to 0xFFFD (REPLACEMENT CHARACTER)
Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
Java encapsulation and getters and setters
Since the Rspec command is troublesome, I tried to make it possible to execute Rspec with one Rake command
Project facet Java version 13 is not supported. How to deal with
[Note] Java: Is it necessary to override equals for equality judgment?
6 points to doubt when user registration is not possible with devise
It may have been possible to introduce bootstrap to rails with this
When calling sshpass from Java with shell etc., it seems that it is necessary to have a path.
Java to learn with ramen [Part 1]
[Java] Points to note with Arrays.asList ()
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
What is thread safe (with Java)
Java, interface to start from beginner
Java, arrays to start with beginners
Automatically generate Loto 6 purchase plans with Java that super beginners have learned
[Java] I want to make it easier because it is troublesome to input System.out.println.
Minimum configuration sample to automatically release Lambda by Java with Code pipeline
Is it possible to put the library (aar) in the Android library (aar) and use it?
When I try to sign up with devise, it automatically redirects to root_path