Java learning notes -- interfaces and packages

Posted by mw-dnb on Mon, 17 Jan 2022 18:07:15 +0100

In the process of self-study Java, I sorted out some knowledge of interfaces and packages

Interface

Definition and basic usage

interface definition: an abstract class without fields

interface person{
    void hello();
    String getName();
}
/*An interface is essentially an abstract class
abstract class person{
	public abstract void fun();
	public abstract String getName();
}
*/

In the above code, the method is not materialized, and the specific functions are realized by overwriting in the class to be called. The rewriting functions among multiple classes do not affect each other. When inheriting an interface, you must override all methods in the interface

class student implements person{
    private String name;
    public student(String name){
        this.name = name;
    }
    @override
    public void hello(){
        System.out.print("hello, " + this.name);
    }
    @override
    public String getName(){
        return this.name;
    }
}
class teacher implements person{
    private String name;
    public student(String name){
        this.name = name;
    }
    @override
    public void hello(){
        System.out.print("hello, Mr/Mrs." + this.name);
    }
    @override
    public String getName(){
        return this.name;
    }
}

Multiple inheritance

In normal class inheritance, only one class can be inherited, while the interface can implement multiple inheritance

class teacher implements person, parents{/**/}

One interface can inherit from another interface

interface hello{
    void hello();
}
interface person extends hello{
    void fun();
    String getName();
    //At this point, the person interface actually has three abstract methods, one of which inherits from hello
}

Comparison between abstract and interface

abstract

  • Define instance fields
  • Define abstract methods
  • Define non abstract methods
  • but, only one class can be extended

interface

  • Multiple interface s can be implemented
  • Define abstract methods
  • Define default method
  • but, instance fields cannot be defined

default method

public class aa {
    public static void main(String[] args) {
        person p = new student("aaaaa");
        p.fun();
    }
}
interface person {
    String getName();
    default void fun() {
        System.out.println(getName() + " fun()");
    }//default modifies the specific method
}
class student implements person {
    private String name;
    public student(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    //In this example, the student class does not rewrite the fun() function
}

When a method is added to the interface, it will involve modifying all subclasses. If the default method is added, the subclasses do not need to be modified. You only need to overwrite the new method where it needs to be overwritten

Default is different from the ordinary methods of the abstract class. The interface has no fields, and default cannot access the fields, while the ordinary methods of the abstract class can access the instance fields. However, in the interface, default can modify specific methods

Static fields and static methods

public class aa {
    public static void main(String[] args) {
        person.setNumber(99);
        System.out.println(person.number);
    }
}
class person {
    public static int number;
    public static void setNumber(int value) {
        number = value;
    }
}

Static methods belong to class, not instance, and can be called directly through the class name

Static field of interface

Although interface is a pure abstract class, it can have static fields, which must be of type final

//person.java
public interface person{
    public static final int male = 1;
    public static final int female = 2;
}

In fact, because the field of interface can only be of public static final type, we can remove the modifier in front of int

//person.java
public interface person{
    int male = 1;
    int female = 2;
    //The compiler will automatically change this field to public static final type
}

package

For example, in the same folder, a writes a person class, b also writes a person class, and c wants to use the person classes of a and b. at this time, the concept of package is introduced

aa.java

package hello;
class person{}
public class aa{}

bb.java

package hello;
class person{}
public class bb{}

The package can be a multi-layer structure, using Separate, e.g. Java util

Package has no parent-child relationship, Java Util and Java util. Zip is a different package, and there is no inheritance relationship between them

Package scope

For example, if you want to define the package hello, you need to create a new folder named hello, where the Java files of the hello package are stored

hello\person.java

package hello;
public class person{
    void hello(){
        System.out.println("hello");
    }
}

hello\main.java

package hello;
public class main{
    public static void main(String[] args){
        person p = new person();
        p.hello();//result: hello
    }
}

import

In one class, we will refer to other classes, such as Jay's person Jay class, if you want to reference jjlin's hello jjlin class, this package needs to be introduced

hello\jjlin.java

//jjlin.java
package hello;
public class jjlin{
    public void fun(){
        System.out.println("hello");
    }
}

person\jay.java

//jay.java
package person;
import hello.jjlin;//Represents the jjlin file under the hello folder
public class jay{
    public void run(){
        jjlin temp = new jjlin();
    }
}

In addition to introducing specific classes under the package, you can also use *, to import all classes under the package (the classes of the sub package are not recognized)

package person;
import jay.*;//Import all class es in jay folder
public class jay{
    public void run(){
        jjlin temp = new jjlin();
    }
}

import static

This method can import static fields and static methods of a class. This syntax is rarely used

//package main;
import static java.lang.System.*;
//Import all static fields and static methods of the System class
public class main{
    public static void main(String[] args){
        out.println("hello");
        //If you do not reference the System package, write it in the following form
        //System.out.println("hello");        
    }
}

The Java compiler finally compiled it The class file only uses the full class name. Therefore, in the code, when the compiler encounters a class name:

  • If it is a complete class name, you can find the class directly according to the complete class name
  • If it is a simple class name, find it in the following order
    • Find out whether this class exists in the current package
    • Find out if the import package contains this class
    • Find Java Does the Lang package contain this class

If the class name cannot be determined according to the above rules, the compilation will report an error

Here is an example

//main.java
package test;
import java.text.Format;
public class main{
    public static void main(String[] args){
        java.util.list list;//ok, use the full class name
        Format format = null;//ok, use the class of import
        String s = "hi";//ok, use Java Lang package String
        System.out.println(s);//ok, use Java Lang package's System
        MessageFormat mf = null;//Error, unable to find MessageFormat
    }
}

Therefore, when writing a class, the compiler will automatically do two import actions for us

  • By default, other class es of the current package are automatically import ed
  • The default is import Java lang.*

The automatic import is Java Lang package, but similar to Java Lang.reflect these packages also need to be imported manually

Best practices

To avoid name conflicts, we need to determine the unique package name. It is recommended to use inverted domain names to ensure uniqueness

  • org.apache is essentially a class under the path / org/apache /, the same below
  • org.apache.commons.log
  • com.jayden.sample

Sub packages can be named according to their functions

Be careful not to mix with Java Lang's package has the same class name

  • String
  • System
  • Runtime
  • ...

Don't use the same name as the class commonly used in jdk

  • java.util.List
  • java.text.Format
  • java.math.BigInteger
  • ...

Reference link - Liao Xuefeng - Java - object oriented programming - Interface

Reference link - Liao Xuefeng - Java - object oriented programming - package

Topics: Java interface