[JAVA] What is the difference between interface and abstract? ?? ??

Roughly explain the difference

interface ・ ・ ・ Not a class (important)
abstract ・ ・ ・ Modifiers for classes, etc.

About interface

① Variable

Can declare and implement variables. However, "public static final" is implicitly added.

interface noTsukaikata {
	int Num = 6; //Since final is attached, it cannot be initialized. → It is treated as a constant.
}
② Method

Only abstract methods can be declared.

interface noTsukaikata {
	void method(); //Declaration of abstract method (method in interface is given abstract clause)
	// abstract void method();Is automatically converted to.
}

In fact, it can be implemented by using the default clause or static clause.

interface deJisou{
	//Implement methods in interface by using default clause
	default void method1(){
		//more codes here
	}

	//Implement methods in interface by using static clause
	static void method2(){
		//more codes here
	}

	//Compile error without default or static clause
	void method3() {
		//more codes here
	}
}
③ Inheritance method

interface can only inherit implements.

interface Super{
	int Num;
	default void method1(){
		//more codes here.
	}
	static void method2(){
		//more codes here.
	}
	void method3();
}

class Sub implements Super{
	//The variable is final so you don't have to write it.

	//Since the default method has already been implemented, it is not necessary to implement it in the Sub class. Can be overridden as needed.

	//Since static methods are not inherited, they cannot be implemented in Sub class.

	//Implement method3
	@Override 
	protected void method3(){//Disclosure range(scope)Access modifiers may be changed as long as is not narrowed.
		//more codes here.
	}
}

The contents of the Sub class by inheriting the above are as follows.


class Sub implements Super{
	int Num = 6;
	default void method1(){
		//more codes here.
	}
	//There is no static method because it is not inherited.
	protected void method3(){
		//more codes here.
	}
}

About abstract

① Variable

Treated exactly the same as a regular class.

abstract class Super {
	int Num1;      //Can be declared
	int Num2 = 6;  //Can be initialized
	abstract int Num3 = 10;  //Compile error (variables cannot be abstracted)
}
② Method

Abstract methods can also be declared.

interface Super {
	abstract void method1(); //You can declare abstract methods.
	void method2() {}        //It can be implemented without the abstract clause.
}
③ Inheritance method

abstract can only inherit extends.

abstract class Super {
	int Num1;
	int Num2 = 6;
	abstract void method1();
	void method2() {}
}

class Sub extends Super{
	@Override 
	void method1() {}
}

The contents of the Sub class by inheriting the above are as follows.


class Sub extends Super{
	int Num1;
	int Num2 = 6;
	void method1() {}
	void method2() {}
}

What was the difference after all? ?? ??

interface abstract
variable Automatically "public static final" Exactly the same as the regular class
Method 基本的に抽象Methodのみ宣言できる。ただし、「default」や「static」句を利用し、実装できる。 抽象Methodも宣言できる。
Inheritance method Inherited by implements. Subclasses implement all abstract methods. Inherited by extends. Subclasses implement all abstract methods.

Supplement

What is the difference between implements and extends? ?? ??

implements ・ ・ ・ Inherits interface. extends ・ ・ ・ Inherits the class.

Conclusion,

interface ・ ・ ・ Not a class
abstract ・ ・ ・ Modifiers for classes, etc.

Recommended Posts

[JAVA] What is the difference between interface and abstract? ?? ??
[JAVA] Difference between abstract and interface
What is the difference between Java EE and Jakarta EE?
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Java] What is the difference between form, entity and dto? [Bean]
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
What is the difference between skip and pending? [RSpec]
[Rails] What is the difference between bundle install and bundle update?
What is the difference between an action and an instance method?
What is the difference between a web server and an application server?
[Java] Understand the difference between List and Set
Understand the difference between abstract classes and interfaces!
[Java] Difference between == and equals
[Java] Difference between Hashmap and HashTable
Understand the difference between each_with_index and each.with_index
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Difference between Java and JavaScript (how to find the average)
What is the LocalDateTime class? [Java beginner] -Date and time class-
Jersey --What is Difference Between bind and bindAsContract in HK2?
[Java] Check the difference between orElse and orElseGet with IntStream
Is short-circuit evaluation really fast? Difference between && and & in Java
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
Difference between final and Immutable in Java
What is java
interface and abstract
[For beginners] Difference between Java and Kotlin
What is the best file reading (Java)
What is Java and Development Environment (MAC)
What is Java <>?
What is the main method in Java?
What is Java
About the difference between irb and pry
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Java vs. JavaScript: What ’s the Difference?
A note on the differences between interfaces and abstract classes in Java
Understand the difference between int and Integer and BigInteger in java and float and double
[Rails / ActiveRecord] About the difference between create and create!
What is the Java Servlet / JSP MVC model?
What is the volatile modifier for Java variables?
Summarize the differences between C # and Java writing
[Java] Difference between "final variable" and "immutable object"
What is an interface?
abstract (abstract class) and interface (interface)
What is Java Encapsulation?
What is Java technology?
What is Java API-java
Difference between vh and%
[Java] What is flatMap?
Difference between i ++ and ++ i
[Java] What is JavaBeans?
[Java] What is ArrayList?
[Java] Difference between equals and == in a character string that is a reference type
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
[Ruby] I thought about the difference between each_with_index and each.with_index
[Rails] I learned about the difference between resources and resources