I'm just a Java beginner who just recently learned how to inherit classes If you have a wrong opinion, it would be helpful if you could tell us in an easy-to-understand manner. This time, I will introduce the understanding of interface and when it is convenient to use it with concrete examples.
interface is used to implement and use the method later without specifically writing the processing content at the beginning. Effective when you want to change the process because the method is implemented later.
My view when I knew only the above ---> ** Do you use it when you can't see the previous vision concretely? ** ** ---> ** If you write the code by yourself, do you need to use it? ** ** ---> ** Is it useful when you want to declare variables to be used later and use them together later? ** **
Only ** constants ** and ** methods ** can be defined Also, the member variables of the interface are automatically given "public static final", so they become ** constants **.
Interface definition (example)
interface interface name{
int constant 1= 1;
int constant 2= 2;
String constant 3= "hello";
void Function name for which processing will be written later();
}
The constants defined in the interface can be reused with the defined function names! ✌️
Example
Main.java
interface Calc {
int num1 = 1;
int num2 = 2;
String str1 = "multiplication";
String str2 = "division";
void calculation();
}
class Multi implements Calc {
public void calculation() {
System.out.println(str1);
System.out.println(num1 * num2);
}
}
class Divi implements Calc {
public void calculation(){
System.out.println(str2);
System.out.println(num1 / num2);
}
}
public class Main {
public static void main(String[] args){
Multi mlt = new Multi();
mlt.calculation();
Divi div = new Divi();
div.calculation();
}
}
Run
$ java Main
multiplication
2
division
0
Constants and functions defined by the interface name *** Calc *** You can see that it is available in the multiplication class *** Multi *** and the division class *** Divi ***
I think some people may have thought
Yes, ** the same thing if you inherit what you defined in the parent class to the child class! ** **
That is not a mistake.
So what are the advantages of ** interface **?
That is, ** you can inherit multiple interfaces at the same time **
In ** class inheritance **, when inheriting a parent class to a child class, The inheriting child class should only be able to specify ** one parent class **.
** "Can handle (inherit) multiple interfaces created earlier" **
That's the advantage of the ** interface ** over ** class inheritance **.
I thought that the function as an interface would be useful in such a situation.
First, take a look at the code below. Enter *** number of students, number of classrooms, number of grades *** of my own school, Outputs ** population per classroom and population per grade ** at each school
Main.java
//primary school
interface Primary_school {
int p_people = 500;
int p_classes = 18;
int p_year = 6;
String p_name = "Mine";
void pcal();
}
//Junior high school
interface Junior_high_school {
int j_people = 750;
int j_classes = 27;
int j_year = 3;
String j_name = "Yoto";
void jcal();
}
//University
interface University {
int u_people = 10000;
int u_classes = 50;
int u_year = 4;
String u_name = "Tsukuba";
void ucal();
}
//Class population density
class PopulationDensity implements Primary_school, Junior_high_school, University{//, Junior_high_school, University {
public void pcal() {
System.out.print(p_name+"--->");
System.out.println(p_people / p_classes);
}
public void jcal(){
System.out.print(j_name+"--->");
System.out.println(j_people / j_classes);
}
public void ucal(){
System.out.print(u_name+"--->");
System.out.println(u_people / u_classes);
}
}
//Grade population density
class PopulationPerYear implements Primary_school, Junior_high_school, University{
public void pcal(){
System.out.print(p_name+"--->");
System.out.println(p_people / p_year);
}
public void jcal(){
System.out.print(j_name+"--->");
System.out.println(j_people / j_year);
}
public void ucal(){
System.out.print(u_name+"--->");
System.out.println(u_people / u_year);
}
}
public class Main {
public static void main(String[] args){
String pink = "\u001b[00;35m";
String cyan = "\u001b[00;36m";
String end = "\u001b[00m";
PopulationDensity PD = new PopulationDensity();
System.out.println(pink+"【PopulationPerClass】: "+end);
PD.pcal();
PD.jcal();
PD.ucal();
PopulationPerYear PPY = new PopulationPerYear();
System.out.println(cyan+"【PopulationPerYear】: "+end);
PPY.pcal();
PPY.jcal();
PPY.ucal();
//div.calculation();
}
}
Execution result
You can see that each class can inherit multiple defined interfaces.
I think this is a very useful function later when multiple people are in charge of developing different input data.
Class inheritance | interface | |
---|---|---|
merit | Can handle variables and functions defined in the parent class | Can handle multiple interfaces at the same time |
Demerit | Only one class can be inherited by one child class | Can only handle constants |
For the time being, it looks like this today
Updated: 2019/04/25
Recommended Posts