Proper use of interface and abstract class

How to use interface and abstract class properly?

The next thing that beginners wonder after understanding what interfaces and abstract classes look like __ How should each be used properly? __ I think that is the case.

Interfaces for users, abstract classes for implementers

--The interface defines the method API in __ for the __caller --Abstract classes implement some in __ for those who implement __ child classes

In short, that's it.

In particular

Implement the interface for the class you want to call using polymorphism.

The user should not declare variables of abstract class type. Let's declare a variable as an interface type.

AbstractFooBar = new FooBar(); // NG
IFooBar = new FooBar(); // OK

You should also not define methods that use abstract classes as formal argument types. Let's specify the interface type.

void fooMethod(AbstractFooBar fooBar); // NG
void fooMethod(IFooBar fooBar); // OK

Supplement What if I want to apply polymorphism in an abstract class?

So what if you want to use polymorphism for the child classes of an abstract class? Because the question seems to arise. In that case, it looks like the following.

抽象クラス-インタフェース.png

That way, the user should always call via the IFooBar interface, and For those who implement the IFooBar interface, You can choose to extend AbstractFooBar or implement IFooBar directly, Everyone will be happy.

Recommended Posts

Proper use of interface and abstract class
Use of Abstract Class and Interface properly in Java
abstract (abstract class) and interface (interface)
Difference between interface and abstract class
Proper use of redirect_to and render
Proper use of Mockito and PowerMock
interface and abstract
Use of Date class
Interface / abstract class / override
Criteria for proper use of render and redirect_to
Example of using abstract class
Summary of "abstract interface differences"
[JAVA] Difference between abstract and interface
[Java] Inheritance and structure of HttpServlet class
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Contents of Collection interface and List interface
(Determine in 1 minute) About the proper use of empty ?, blank? And present?
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
Class and model
[Java] How to use FileReader class and BufferedReader class
java (abstract class)
About next () and nextLine () of the Scanner class
[java] abstract class
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement
Inheritance and interface.
I want to recursively get the superclass and interface of a certain class
[Java] How to use Calendar class and Date class
[Java] How to use compareTo method of Date class
Use of Japanese fonts and external characters in JasperReport
Until the use of Spring Data and JPA Part 2
[JAVA] What is the difference between interface and abstract? ?? ??
Use variables for class and id names in haml
[Ruby] Class nesting, inheritance, and the basics of self
[Java] Handling of character strings (String class and StringBuilder class)
How to use and apply Java's JFrame / Canvas class
Handling of date and time in Ruby. Use Date and Time properly.
Class and instant part2
About java abstract class
[Java] Set structure of collection class (about HashSet and TreeSet)
Implement Java Interface in JRuby class and call it from Java
[Java] I thought about the merits and uses of "interface"