Interfaces and inner classes

Posted by gioahmad on Sun, 03 Oct 2021 00:13:32 +0200

1, Inner class

1.1. Definition:

  an internal class is simply to perform other defined operations inside the class

for instance:

public class Inner_class {
    public static void main(String[] args) {
        outer out = new outer();
        out.println2();
    }
}
class outer{
    public String str1 = "Defined external class";
    class inner{
        public String str2 = "Defined inner class";
        public void println(){
            System.out.println(str1);
        }
    }
    public void println2(){
        inner in = new inner();
        in.println();
    }
}

Result of run: defined external class

1.2. Functions of internal classes

1. Internal classes and external classes can easily access each other's private methods and properties

2. The internal class is equivalent to another encapsulation to hide other external classes

3. The inner class can realize the limitation of Java single inheritance

However, the structure of internal classes is more complex

1.3. Create internal class

1.3.1 create a non static inner class outside the outer class

for instance:

outer.inner inn = new outer().new inner();
grammar:
External class.Inner class object = new External class().new Inner class();

1.3.2 create a static inner class outside the outer class

for instance

outer2.inner2 inn2 = new outer2.inner2();
Syntax:
External class.Inner class inner object = new External class.Inner class

1.3.3 create an internal class inside an external class

for instance:

inner in = new inner();

1.4. Classification of internal classes

1.4.1 member internal class

  no static variables or methods are allowed inside the member's internal class. The member's internal class is attached to the external class. The internal class can be created only after the external class is created

1.4.2 static internal class

  internal classes modified by static are called static internal classes. Static internal classes can be created directly without relying on external classes; Static internal classes cannot use non static classes of any external class, but they can have their own member variables

1.4.3 method inner class (local inner class)

  classes defined in methods;

Access modifiers are not allowed in method inner classes

1. The internal class of the method is completely hidden from the outside. Except for the method that creates this class, it cannot be accessed anywhere else

12. If the method internal class wants to use the method parameter, the new parameter must use the final declaration

1.4.4 anonymous inner class

  is a method inner class without a name. Therefore, the characteristics are consistent with the methods, and it also has its own characteristics: the anonymous inner class must inherit an abstract class or implement an interface; The anonymous inner class has no class name, so there is no constructor

If you only need to create an object of this class without knowing its actual type (you don't need to use the class name), you can use an anonymous inner class.

  for example

public class lambda1 {
    //Static inner class
    static class Like2 implements ILike {

        @Override
        public void lambda() {
            System.out.println("I love learning. Java2");
        }
    }
    public static void main(String[] args) {
        ILike like = new Like();  // The implementation class of the interface can be instantiated, but the address of the object implementing the class in memory must point to the interface, and the interface can be used
        like.lambda();

        ILike like2 = new Like2();
        like2.lambda();
        //Local inner class
        class Like3 implements ILike {

            @Override
            public void lambda() {
                System.out.println("I love learning. Java3");
            }
        }
        like = new Like3();
        like.lambda();

        //Anonymous Inner Class 
        like = new ILike() {
            @Override
            public void lambda() {
                System.out.println("I love learning. Java4");
            }
        };
        like.lambda();
    }

}
interface ILike{
    void lambda();
}
class Like implements ILike {

    @Override
    public void lambda() {
        System.out.println("I love learning. Java");
    }
}

2, Interface

2.1. Definition:

  an interface is a collection of definitions of abstract methods and constant values. In essence, an interface is a special abstract class. This abstract class only contains the definitions of constants and methods, without the implementation of variables and methods.

  inheritance only supports single inheritance, but interfaces can implement multiple inheritance. Class can implement the interface through the keyword implements, but to implement the class in the interface, you must implement the method in the interface

2.2. Implementation of interface

Steps:

1. Declare that the class implements the given interface

2. Provide definitions for all methods in the interface

All methods in the interface are public by default. However, when implementing the interface, the method must be declared public. Otherwise, the compiler will default this property to package visibility (the default access property of the class)

for instance:

public interface TimeService {
    void timer();
}

public interface UserService {
    //All definitions in the interface are abstract. The default is public abstract

    //Interface definitions are constants: public static final
    int age = 99;

    void add(String name); 
    void delete(String name);
    void update(String name);
    void query(String name);

}

public class UserServiceImpl implements UserService,TimeService{
    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void update(String name) {

    }

    @Override
    public void query(String name) {

    }

    @Override
    public void timer() {

    }
}

Function of interface:

1. Constraints
2. Define some methods for different people to realize
3. Method: public abstract attribute: public static final
4. The interface cannot be instantiated. There is no construction method in the interface
5.implements can implement multiple interfaces
6, but you must override the methods in the interface

2.3. Interface properties

1. The interface is not a class. You cannot instantiate an interface with new, but you can declare the variables of the interface

2. The variables of the interface must refer to the class object that implements the interface

3. The interface cannot contain instance fields, but can contain constants (default: public static final). Any class implementing the interface automatically inherits constants

3, Lambda expression

Functional interface: the interface with only one method is called functional interface, which can be simplified by lambda expression

Using Lambda expressions can make the code look more concise and avoid too many anonymous inner class definitions

for instance:

public class lambda1 {
    public static void main(String[] args) {
        ILike like = new Like();
        //Simplify with Lambda
        like = () -> {
            System.out.println("I love learning. Java");
        };
        like.lambda();
    }

}
interface ILike{
    void lambda();
}
class Like implements ILike {

    @Override
    public void lambda() {
        System.out.println("I love learning. Java");
    }
}

But when a parameter is passed in the method

public class lambda2 {

    public static void main(String[] args) {
        ILove love = null;
       //lambda expression simplification
        ILove love = (int a)-> {
                System.out.println("I love you-->" + a);
        };
    }
}
interface ILove{
    void love(int a);
}
//Simplified parameter type
love = (a) ->{
    System.out.println("I love you-->" + a);
};
//Simplified parentheses
love = a ->{
    System.out.println("I love you-->" + a);
};
/*lambda The expression can only be simplified into one line if there is only one line of code. If there are multiple lines, wrap them with code blocks
    The premise is that the interface is functional
    Parameter types can also be removed for multiple parameters. Remove all parameters, but do not remove parentheses
*/
love = a -> System.out.println("I love you-->" + a);
        love.love(2);

Topics: Java