Day_15 [java basics] abstract classes, interfaces and internal classes

Keywords: Java Back-end

1, abstract class

When defining a class, it is often necessary to define some methods to describe the behavior characteristics of the class, but sometimes the implementation of these methods is uncertain. For example, if an Employee class is defined, its work() method describes work, but their work is different for different positions. For this purpose, we use the abstract keyword. We only need to define the method name in the parent class without implementing the specific method body.
abstract can modify classes and methods
1.abstract modifier class: abstract class
① Cannot instantiate, can have no abstract method
② It must have its constructor to facilitate subclass instantiation and call
③ Abstract methods are implemented by their subclasses
2.abstract modification method: abstract method
① Abstract methods have no method body and must be defined in abstract classes
② Abstract classes can have no abstract methods
③ A subclass can be instantiated only if it implements all the abstract methods of the parent class

2, Interface


1. What is an interface?
If all the methods in a class are abstract, this class can be defined as another form in Java - interface. Interface is a special abstract class and is a parallel structure with class.
JDK 7 and before, the interface cannot contain ordinary methods, and only abstract methods and global constants can be defined inside the interface.
JDK 8 and later, in addition to abstract methods and global constants, the interface can also have default methods and static methods (also known as class methods). Default methods are decorated with default and static methods are decorated with static. Both methods allow method bodies.
2. How to use the interface?
Use the interface definition, where the keyword decorated in [] brackets can be omitted.

[Permission modifier] interface Interface name [extends Parent interface 1, parent interface 2,...]{
	[public] [static] [final] Constant type constant name = constant value;
	[public] [abstract] Return value type method name([parameter list]);
	[public] default Return value type method name([parameter list]){
		//The method body of the default method;
	}
	[public] static Return value type method name([parameter list]){
		//Method body of static method
	}
}
public class Test {
	public static void main(String[] args) {
		Dog dog = new Dog();
		Animal.show();					//Static method call: interface name. Method name
		dog.eat();						//Abstract method call: object name. Method name
		dog.sleep();					//Default method call: object name. Method name
		//Cat cat = new Cat(); 			// Cannot instantiate type cat
		//cat.eat();
	}
}
interface Animal{
	public static final int id = 1;		//constant
	String name = "Xiao Huang";				//constant
	public abstract void eat();			//Abstract method (eat)
	public abstract void getName();		//Abstract method getName()
	public static void show() { 		//Static method
		System.out.println("id = "+id+", name = "+name);
	}
	public default void sleep() {		//Default method
		System.out.println("All animals have to sleep!");
	}
}
class Dog implements Animal{
	public void eat() {
		System.out.println("Dogs eat meat!");	
	}
	public void getName() {
		System.out.println("name = "+name);
	}
}
abstract class Cat implements Animal{	//Only one abstract method is implemented and defined as an abstract class (cannot be instantiated)
	public void eat() {
		System.out.println("Cats eat fish!");	
	}
}

3. Description of interface:
① Constructors cannot be defined in the interface, which means that the interface cannot be instantiated. If subclasses implement all abstract methods in the interface, subclasses can be instantiated; on the contrary, if subclasses implement some abstract methods, they cannot be instantiated.
② Interfaces can inherit single or multiple. A class can inherit a parent class and implement multiple interfaces at the same time.

New features of interfaces after JDK 8:

① By implementing the object of the class, you can call the default method in the interface. If the implementation class overrides the default method in the interface, the overridden method will be called
② If the default method with the same name and parameter is declared in the inherited parent class and the implemented interface of the subclass (or implementation class), the subclass will call the method with the same name and parameter in the parent class by default without overriding this method
③ If the implementation class implements multiple interfaces and default methods with the same name and parameters are defined in these interfaces, an error will be reported if the implementation class does not override this method. - > interface conflict. This requires us to override this method in the implementation class
(4) the method of calling the parent class or interface in the subclass called the interface name.super. method name () or the parent class.super. method name ();
⑤ The default method is only allowed in the interface, and the default keyword is removed when it needs to be rewritten in the implementation class.

3, Inner class

In java, it is allowed to define another class B inside a Class A, which becomes an internal class. Class A is called an external class. According to the location of the internal class, it is divided into member internal class and local internal class.
1. Member internal class:
On the one hand, as a member of an external class:
The structure of calling external classes can be modified with static and four permission modifiers;
On the other hand, as a class:
Attributes and methods can be defined and modified by final and abstract, respectively indicating that they cannot be inherited or instantiated.
To create an internal class object:
External class name. Internal class name object name = new external class name (). New internal class name ()

Create a static inner class:
External class name. Static internal class name object name = new external class name. Static internal class name ()

2. Local internal class:
It is also called method inner class. Like local variables, it is defined inside the method, and the valid range is limited to the method.

//Instructions for inner class 1: member inner class, local inner class, static inner class
public class ExampleTest {
	public static void main(String[] args) {
		Outer.Inner inner = new Outer().new Inner();		//Create an object of an inner class
		inner.print();
//		inner.show(); 					// The show () method is undefined in the Inner class. You cannot call the show method of an external class
		Outer.Inner01 inner01 = new Outer.Inner01();		//Create an object of a static inner class
		inner01.say();	
		Outer outer = new Outer();							//Create an object of an external class and call a local internal class
		outer.show();	
	}
}
class Outer{										//External class
	int m = 0;
	public void show() {
		class Inner02{								//1. Local internal class Inner02
			int y = 0;
			public void shout() {
				System.out.println("Local inner class shout method! y = "+y);
			}		
		}
		Inner02 i  = new Inner02();					//Call local internal class variables and methods inside local internal class methods
		i.shout();
		System.out.println("External class show method!");
	}
	class Inner{									//2. Member internal class
		int n = 0;
		public void print() {
			System.out.println("Member inner class print method!");
		}
	}
	static class Inner01{							//3. Static internal class
		int x = 5;
		public void say() {
			System.out.println("Of static inner classes say method!");
		}
	}
}
//Description of inner class 2: use of anonymous inner class
public class Example02 {
	public static void main(String[] args) {
		String name = "floret";
		//Define an anonymous inner class and pass parameters to the animalShout() method
		animalShout(new Animal() {
			//Implement the shot method
			public void shout() {
				System.out.println(name+"Meow meow...");
			}
		});
	}
	public static void animalShout(Animal an) {
		an.shout();			//Call the shot method of the incoming object
	}
}
//Define animal class interface
interface Animal{
	public void shout();
}


Extended interview questions:

1. Does the following program compile incorrectly? What is the result of no error?

A: compilation error, because x is not clear whether it is in parent class B or interface A. if you want to output X in parent class A, the parent uses super.x to call; if you use X in interface B, use interface name B.x to call, because only constants can be defined in the interface.

2. Does the following program compile incorrectly? What is the result of no error?
A: the variable ball at the arrow is a constant and cannot be assigned again. The rewrite of the play() method is considered to be a rewrite of the interface Playable and the interface Bounceable.

Posted by enlight on Fri, 12 Nov 2021 20:17:18 -0800