Saturday 9 July 2022

Can an enum has abstract methods in Java?

Enum can have abstract methods in Java. This feature is useful, when you need to provide different implementation for the enum constants.


 

Example

public static enum StringConverters {

	LOWER {

		@Override
		public String convert(String str) {
			return str.toLowerCase();
		}

	},
	CAMELCASE {

		@Override
		public String convert(String str) {
			return toCamelCase(str);
		}

	};

	abstract public String convert(String str);
};

 

Find the below working application.

 

EnumAbstractMethod.java

package com.sample.app;

public class EnumAbstractMethod {

	public static enum StringConverters {

		LOWER {

			@Override
			public String convert(String str) {
				return str.toLowerCase();
			}

		},
		UPPER {

			@Override
			public String convert(String str) {
				return str.toUpperCase();
			}

		},
		CAMELCASE {

			@Override
			public String convert(String str) {
				return toCamelCase(str);
			}

		};

		private static String toCamelCase(final String str) {
			if (str == null || str.isEmpty())
				return null;

			final StringBuilder builder = new StringBuilder();

			for (final String word : str.split(" ")) {
				if (!word.isEmpty()) {
					builder.append(Character.toUpperCase(word.charAt(0)));
					builder.append(word.substring(1).toLowerCase());
				}
				if (!(builder.length() == str.length()))
					builder.append(" ");
			}

			return builder.toString();
		}

		abstract public String convert(String str);
	};

	public static void main(String[] args) {
		String str = "Hi Krishna, how are you";

		System.out.println(StringConverters.LOWER.convert(str));
		System.out.println(StringConverters.UPPER.convert(str));
		System.out.println(StringConverters.CAMELCASE.convert(str));
	}

}

 

Output

hi krishna, how are you
HI KRISHNA, HOW ARE YOU
Hi Krishna, How Are You

 

If you have this kind of use case, you may prefer the enum which implements an interface.

 

EnumImplementInterface.java

package com.sample.app;

public class EnumImplementInterface {

	interface Converter<T> {
		T convert(T data);
	}

	public static enum StringConverters implements Converter<String> {
		LOWER {

			@Override
			public String convert(String str) {
				return str.toLowerCase();
			}

		},
		UPPER {

			@Override
			public String convert(String str) {
				return str.toUpperCase();
			}

		},
		CAMELCASE {

			@Override
			public String convert(String str) {
				return toCamelCase(str);
			}

		};

		private static String toCamelCase(final String str) {
			if (str == null || str.isEmpty())
				return null;

			final StringBuilder builder = new StringBuilder();

			for (final String word : str.split(" ")) {
				if (!word.isEmpty()) {
					builder.append(Character.toUpperCase(word.charAt(0)));
					builder.append(word.substring(1).toLowerCase());
				}
				if (!(builder.length() == str.length()))
					builder.append(" ");
			}

			return builder.toString();
		}
	}

	public static void main(String[] args) {
		String str = "Hi Krishna, how are you";

		System.out.println(StringConverters.LOWER.convert(str));
		System.out.println(StringConverters.UPPER.convert(str));
		System.out.println(StringConverters.CAMELCASE.convert(str));
	}

}

Output

hi krishna, how are you
HI KRISHNA, HOW ARE YOU
Hi Krishna, How Are You




 

 

 

You may like

Interview Questions

Quick guide to race condition in Java with examples

How to read the file content using an iterator in Java?

Implement a Closeable iterator in Java

Quick guide to generate random data in Java

What are the different cache eviction strategies?


How to call super class method from sub class overriding method?

No comments:

Post a Comment