Interface oriented interface

Keywords: Java

Interface


1 concept of interface


Like the abstract classes learned before, Interface is also an abstract type in Java. The content of the Interface is the function to be implemented formed by abstraction, and the Interface is more like a rule and a set of standards

2 interface format

3. Characteristics of interface:
1. Define the interface through the interface keyword
2. Let subclasses implement interfaces through implements
3. All the methods in the interface are abstract methods (JAVA8), and there is no construction method.
4. The interface can be understood as a special abstract class (but the interface is not a class!!!)
5. A class describes the attributes and methods of a class of things, and an interface contains the methods to be implemented by the implementation class
6. The interface breaks through the limitation of java single inheritance
7. Multiple interfaces and classes can be implemented, and multiple interfaces can inherit
8. The interface is a set of rules for external exposure and a set of development specifications
9. The interface improves the function expansion of the program and reduces the coupling

 

  practice

1. Define the interface through the interface keyword

2. Can there be common methods in the interface-- may not!!

3. Can there be abstract methods in the interface-- Yes, the methods in the interface are abstract methods!!!

4. If the implementation class wants to use the function of the interface, it should establish an implementation relationship with the interface and implement it through the keyword implements

5.1 scheme 1: after establishing the relationship between the implementation class and the interface, you can choose not to implement the abstract method in the interface, but turn yourself into an abstract subclass

//abstract public class InterImpl implements Inter{

5.2 scheme 2: if the implementation class implements the interface, it can rewrite all abstract methods in the interface

6. The interface cannot create an object. You can call a method through polymorphism

package cn.tedu.inter;

public interface Inter {
	//Normal methods cannot exist
	//public void eat() {}
	
	//Abstract method
	public abstract void eat();
	public abstract void play();
}
package cn.tedu.inter;

public class InterImpl implements Inter{

	@Override //As a tag, it represents an abstract method that implements the parent interface
	public void eat() {
		System.out.println("Eat hot pot");
	}

	@Override
	public void play() {
		System.out.println("Play code");
	}

}
package cn.tedu.inter;
/**This class is used to test the implementation class of the interface*/
public class InterTests {
	
	public static void main(String[] args) {
		//7. Test interface creation object
		/**Interface cannot create object*/
		//Inter i = new Inter();
		
		//Create polymorphic objects for testing
		Inter i = new InterImpl();
		//Test by calling methods on objects
		i.eat();
		i.play();
		
		//10. Create subclass objects and test them
		InterImpl i2 = new InterImpl();
		i2.eat();
		i2.play();
	}
}

Exercise 2

package cn.tedu.inter2;
/**This class is used to further test the use of interfaces*/
public class TestUserInter {
	//5. Create the entry function main()
	public static void main(String[] args) {
		//6. Create polymorphic objects for testing
		/**Problem: when a subclass creates an object, the constructor of the parent class will be called by default
		 * At present, the parent of the interface implementation class is an interface, and the interface has no constructor
		 * Who implements the super() call in the class constructor?
		 * Conclusion: if a class does not explicitly specify a parent class, it inherits the top-level parent class Object by default
		 * So super() will automatically call the parameterless constructor in the Object class
		 * */
		/**View class inheritance structure: Ctrl+T*/
		Inter2 i = new Inter2Impl();
	}
}

//1. Create an interface
interface Inter2{
	/**1.Is there a constructor in the interface-- may not!!!*/
	//2. Test whether the interface can have a construction method
//	public Inter2() {}

}

//3. Create the implementation class of the interface
//class Inter2Impl extends Object implements Inter2{
class Inter2Impl implements Inter2{
	//4. Create the constructor of the implementation class
	public Inter2Impl() {
		super();
		System.out.println("I am Inter2Impl Nonparametric structure of");
	}
}
package cn.tedu.inter2;
/**This class is used to further test the use of interfaces*/
public class TestUserInter {
	//5. Create the entry function main()
	public static void main(String[] args) {
		//6. Create polymorphic objects for testing
		/**Problem: when a subclass creates an object, the constructor of the parent class will be called by default
		 * At present, the parent of the interface implementation class is an interface, and the interface has no constructor
		 * Who implements the super() call in the class constructor?
		 * Conclusion 1: if a class does not explicitly specify a parent class, it inherits the top-level parent class Object by default
		 * So super() will automatically call the parameterless constructor in the Object class
		 * */
		/**View class inheritance structure: Ctrl+T*/
		Inter2 i = new Inter2Impl();
		/**Conclusion 2: the variables in the interface are actually static constants, which can be called directly through the class name*/
		System.out.println(Inter2.age);
		/**Conclusion 3: variables in the interface are actually static constants, and their values cannot be modified*/
		//Inter2.age = 200;
	}
}

//1. Create an interface
interface Inter2{
	/**1.Is there a constructor in the interface-- may not!!!*/
	//2. Test whether the interface can have a construction method
//	public Inter2() {}
	/**2.Can there be member variables in the interface-- No,
	 * Is a static constant, which is actually written as public static final int age = 10;
	 * But you can omit no writing in the interface
	 * */
	int age  = 10;

}

//3. Create the implementation class of the interface
//class Inter2Impl extends Object implements Inter2{
class Inter2Impl implements Inter2{
	
	//4. Create the constructor of the implementation class
	public Inter2Impl() {
		super();
		System.out.println("I am Inter2Impl Nonparametric structure of");
	}
}
package cn.tedu.inte2r;
/**This class is used to further test the use of interfaces*/
public class TestUserInter {
	public static void main(String[] args) {
		//3. Create a polymorphic object in main() for testing
		/**Problem: when a subclass creates an object, it will automatically call the constructor of the parent class, but now the parent is an interface
		 * There is no constructor in the interface. What is called by super() in the subclass?
		 * Conclusion 1: the subclass inherits the top-level parent class obejct by default, and super () will automatically call the parameterless construction of Object
		 */
		Inter2 in = new Inter2Impl();
		/**Conclusion 2.1: the variables in the interface are actually static constants, which can be called directly through the class name*/
		System.out.println(Inter2.age);
		/**Conclusion 2.2: the variables in the interface are actually static constants and cannot be re assigned*/
		//Inter2.age = 20;
	}
}

//1. Create an interface
interface Inter2{
	/**1.Is there a constructor in the interface-- No, There is no ordinary method*/
	//public Inter2() {}
	/**2.Can there be member variables in the interface-- No,*/
	int age = 10;//Static constant, actually: final static int age = 10;
	/**3.Can there be abstract methods in an interface-- sure!!!*/
	abstract public void eat2();
	void eat();//It can be abbreviated -- public ABSTRCT will be spliced automatically
}
//2. Create the implementation class of the interface
class Inter2Impl implements Inter2 {
	public Inter2Impl() {
		super();//By default, the parameterless constructor of the top-level parent class Object is called first
		System.out.println("I am Inter2Impl Nonparametric structure of");
	}
	/**4.If an abstract method is added to the interface, all the unimplemented abstract methods need to be implemented in the implementation class*/
	@Override
	public void eat2() {
	}
	@Override
	public void eat() {
	}
}
package cn.tedu.design;
/**This class is used to transform the teacher's design case, using interface oriented programming*/
public class TestDesignInter {
	public static void main(String[] args) {
		CGBTeacher2 ct = new CGBTeacher2();
		ct.ready();
		ct.teach();
	}
}
//1. Create interface Teacher2 -- extract commonalities, form an abstraction layer - reflect interfaces - define rules
/**1.Define the interface through the interface keyword*/
interface Teacher2{
	/**2.The methods in the interface are abstract methods, which can be abbreviated as public abstract*/
	//2. Define abstract methods in the interface
	//2.1 lesson preparation methods
	void ready();
	//2.2 class methods
	void teach();
}
/**3.If the implementation class wants to use the functions of the interface, it needs to establish an implementation relationship with the interface*/
//3. Create the implementation class of the interface and add all unimplemented methods
class CGBTeacher2 implements Teacher2{
	@Override
	public void ready() {
		System.out.println("Preparing lessons...E-commerce project");
	}
	@Override
	public void teach() {
		System.out.println("Having class...E-commerce project");
	}
}
//4. Create an abstract subclass of the interface
abstract class SCDTeacher2 implements Teacher2{}

//5. Create an abstract subclass of the interface 2
abstract class ACTTeacher2 implements Teacher2{
	@Override
	public void ready() {
		System.out.println("Preparing lessons...Foundation strengthening..Framework strengthening..High and new technology");
	}
	public abstract void teach() ;
}
package cn.tedu.inner2;

import cn.tedu.inter.Inter;

/*This class is used to test the complex relationship between interfaces and classes*/
public class TestRelation {
    public static void main(String[] args) {
        //Create objects for functional testing
        Inter3Impl i = new Inter3Impl();
        i.save();
        i.delete();
        i.update();
        i.find();
    }
}

//1. Create interface 1
interface Inter1{
    void save();//Save function
    void delete();//Delete function
}
//2. Create interface 22
interface Inter22{
    void update();//Update function
    void find();//Query function
}
//3. Create the implementation class of interface 1
class Inter1Impl implements Inter1{
    @Override
    public void save() { }
    @Override
    public void delete() { }
}

//4. Create interface 3 and inherit two interfaces at the same time
/*1.Interfaces can inherit interfaces and can inherit multiple interfaces. Multiple interfaces are separated by commas*/
interface Inter3 extends Inter1,Inter22{ }

//5. Create the implementation class of interface 3
/*2.Interfaces and implementation classes are implementation relationships and can be implemented in multiple ways. Multiple interfaces are separated by commas
* For classes in Java, follow: single inheritance and multiple implementations
* A class can only have one parent class, but a class can implement multiple interfaces*/
//class Inter3Impl implements Inter3{/ / writing method 1
class Inter3Impl implements Inter1,Inter22{//Writing method 2
    @Override
    public void save() {
        System.out.println("wait a moment...Trying to save...");
    }
    @Override
    public void delete() {
        System.out.println("Delete succeeded!");
    }
    @Override
    public void update() {
        System.out.println("The sophomore is constantly updating~");
    }
    @Override
    public void find() {
        System.out.println("Sir, I'll check it right away. Wait a minute~");
    }
}

8 summary


1. Relationship between classes
Inheritance relationship. Only single inheritance is supported
For example, A is A child class, B is A parent class, and A has all the functions of B (except the private resources and construction methods of the parent class)
If the subclass wants to modify the original function, it needs to be overridden (the method signature is consistent with the parent class + permission modifier > = parent class modifier)

2. Relationship between class and interface
Implementation relationship. It can be implemented alone or multiple
class A implements B,C{}
Where A is the implementation class, B and C are interfaces, and A has all the functions of the BC interface, but needs to rewrite the method, otherwise A is an abstract class

3. Relationship between interfaces
It is an inheritance relationship. You can inherit alone or multiple
interface A extends B,C{}
Where ABC is an interface and A is A sub interface, which has all the functions (abstract methods) of BC interface
class X implements A{}
The X implementation class needs to override all methods of the ABC interface, otherwise it is an abstract class
class A extends B implements C,D{}
A is the implementation class and a subclass of B. It also has all the functions of the CD interface
At this time, A needs to rewrite all abstract methods in the CD interface

4. Differences between abstract classes and interfaces
An abstract class is a special class. An abstract class can contain methods without method bodies (abstract methods)
An interface can be understood as a special abstract class. All the methods in the interface are abstract methods, and there are no ordinary methods
The interface will automatically splice public abstract for methods and public final static for variables
Abstract classes can have member variables and member constant interfaces can only have static constants
Abstract classes can have constructors - used to create objects for subclasses. There are no constructors in the interface
Abstract classes and interfaces cannot be instantiated (create objects)
Interfaces can inherit interfaces, and can inherit multiple interfaces, but classes can only inherit single
Abstract methods can only be declared but not implemented. Interfaces are the result of design and abstract classes are the result of reconstruction

Posted by fredley on Wed, 13 Oct 2021 07:57:28 -0700