Abstract class

Abstract class concept:

1, Java can define a method without a method body, the concrete implementation of the method is completed by the subclass, the method is called abstract method, containing abstract method class is abstract class;

2. For example, if the methods used to calculate the circumference and area of shape are uncertain, we can declare such methods abstract so that we can implement them in concrete subclasses

Abstract class is also called abstract base class. Abstract class is a middle way between the base class and the interface. It has properties of the base class (implemented method body) and interface properties (abstract method).

Abstract class characteristics:

Declaration of abstract methods

Modifier abstract Return value type method name ([parameter list]);

Note: Abstract methods do not have a method body because they do not determine what functions they perform, so they need to be followed by semicolons

Declaration of an abstract class

Syntax: modifier Abstract class name extends parent class name

Abstract and ordinary classes are similar to ordinary classes except for the abstract modifier;

Abstract classes can have no abstract methods; But once a class has abstract methods, that class must be declared abstract

Static final Private cannot coexist with abstract

Use of abstract classes:

Because an abstract class is not a concrete class, it cannot be instantiated, but it can be used to declare variables;

Abstract classes can be inherited, and all the abstract methods of all abstract classes can be implemented in the subclass to achieve the materialization of abstract classes.

Note: Because abstract methods cannot determine what functions they perform, all abstract methods do not have a method body and need to be followed by semicolons.

Abstract methods: method bodies are not allowed; Subclasses need to override the parent class’s abstract methods, otherwise, the subclass is also abstract

Public abstract void eat ();

Abstract methods: Java can define methods that do not contain a method body, and the implementation of the method body is left to the subclasses of that class to implement according to their own circumstances. Abstract classes: Classes that contain abstract methods are called abstract classes. An abstract class can have one or more abstract methods.

Abstract classes must be defined with the abstract modifier, and abstract methods must also be modified with abstract. Abstract classes cannot be instantiated, and objects cannot be generated using the new keyword. Abstract methods can only be declared, not implemented. A class containing abstract methods must be declared as abstract, and a subclass of an abstract class must override all abstract methods before it can be instantiated. Otherwise, the subclass is still abstract.

2. Interface

Interface concepts:

Interfaces give us a more structured way to separate interfaces from implementation. An interface is a completely abstract class that provides no concrete implementation of any kind. It allows the creator to determine method names, parameter class tables, and return types of methods without any method body.

Interfaces are used to establish protocols between classes. Interface class modifiers must be public, default, or a combination of the preceding two and abstract. Interfaces can also contain fields, but these fields are of type public static final. All interface fields must be initialized. The default access level of all methods in the interface is public. If the access level is defined less than public, the compilation will fail.

An interface is a structure used to implement multiple inheritance between classes. The interface cannot be instantiated, that is, objects cannot be created with the new operator. A class implements one or more interfaces by itself using the keyword implements declaration. Constants defined in the interface can be used in the class body, and all methods defined in the interface must be implemented. Methods defined in the interface must be automatically public, and methods must be declared public when implementing the interface. Methods in interfaces are abstract.

An interface can inherit from another interface.

Any class (ordinary, inner, abstract) can implement an interface.

Interface features:

Interfaces in Java are not just “conventions” in program development, but more abstract classes

Can’t generalize to a class but some characteristics are the same

Grammar:

Interface Interface name {[constant]; [Abstract method]; }

Abstract methods in an interface can be left out of the abstract keyword, and access modifiers default to public

When a class implements an interface, it needs to implement all the abstract methods in the interface, otherwise it needs to make the class abstract.

The interface can contain constants, which default to public static final

Interfaces can also implement inheritance, and can inherit from multiple parent interfaces

Functions of interfaces:

Improve program reuse; Improve the scalability of the program; Reduce the coupling degree of the program; Multiple inheritance is implemented.

Default: the default method can be added after the method body jdk1.8. Can be overridden in an implementation class and can be called by reference to an interface

Static: static methods can be added with method body jdk1.8. Cannot be overridden in an implementation class, but can be called by the interface name

Inner class

What is an inner class? In Java, classes can be defined inside another class or a method. Such classes are called inner classes

Definition:

Define a class inside a class

An inner class can directly access and reference all variables and methods of its outer class, which can be declared private or protected in contrast to the outer class.

When we use static to modify an inner class (a nested class), the class is equivalent to an externally defined class. A static inner class cannot use a non-static member variable of an external class

The local variables of the function (parameters are also local variables), the member variables of the inner class, and the member variables of the outer class have the same name, specifying exactly which variables we really want to access in the following way.

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n311" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); The font - size: 0.9 em. display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative ! important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" > public class Outer { private int size; class Inner { private int size; public void doStuff(int size) { size++; // the doStuff parameter this.size++ is referenced; Outer. This. size++; }}}</pre>Copy the code

Method to define an inner class?

Inner classes are defined not only within a class, but also within the scope of several blocks. For example, inside a method or the body of a for loop. An inner class defined ina method can access only local variables of the final type in the method.

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n362" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); The font - size: 0.9 em. display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative ! important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" > public void testClassInMethod(final String arg) { class ClassInMethod { void test() { System.out.println(arg); } } }</pre>Copy the code
How can an inner class be referenced by an outer class?

An inner class can be called from outside the outer class by creating an object, as long as the inner class is declared public

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n330" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); The font - size: 0.9 em. display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative ! important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" >public class Outer { private int size; class Inner { private int size; public void doStuff(int size) { size++; // the doStuff parameter this.size++ is referenced; Outer. This. size++; }} public static void main(String[] args) {Outer Outer = new Outer(); Outer.Inner inner = outer.new Inner(); inner.doStuff(1); } }</pre>Copy the code

Internal class classification:

Member inner classes: define a class as a member of another class; Get an instance of an inner class object,

Method 1: new External class New Inner class 2: external class object new Inner class 3: external class object Access method

1. When an inner class is used externally, it cannot be instantiated directly.

2, the inner class access modifier, can be arbitrary, but the access scope is affected;

The inner class can access the members of the outer class directly. If a property with the same name appears. Priority access to those defined in the inner class;

You can use the.this member of the external class to access information of the same name.

5, the external class to access the internal class information, through the internal class instance, cannot directly access;

Class file name: outer class $inner class.class;

Whether the inner class can contain methods with the same method signature as the outer class

Local inner class:

Define a class in a method body or code block of another class.

1. The definition is within the method and the scope of action is also within the scope

Public, private, protected, static are not allowed before class

3. Classes cannot contain static members

4. A class can contain a final, abstract modifier

Static inner class:

Define one class as a member of another class with the static modifier;

1, static inner class, can only directly access the static member of the external class, if you need to call a non-static member, can be through the object instance

2, static inner class object instance, can not rely on the external class object

3. External classes can be used. Inner class. Static member access to static members of an inner class

4. When an inner class attribute has the same name as an outer class attribute, a member of the inner class is called by default. If you need to access static properties in an external class, you can do so through the external class. The manner of attributes; If you need to access non-static properties in an external class, you can do so through new external class (). Property mode

Anonymous inner class:

Anonymous inner classes have no name

You only need to create an object for the local inner class; you do not need to specify a name for the class.

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n293" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); The font - size: 0.9 em. display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative ! important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" > public Inner testAnonyClassInMethod() {return new Inner() { @override public void doStuff(int size) {super.dostuff (size); }}; }</pre>Copy the code

1. Anonymous inner classes have no type name, instance object name

2, the compiled file name: external class $digit.class

3. You cannot use private, public, protected, abstract, static

4, unable to write a constructor, can add a constructor code block

5. Static members cannot appear

Anonymous inner classes can implement interfaces or inherit from superclasses, but not both

What are the differences and connections between abstract classes and interfaces?

A class can implement multiple interfaces, and a class can inherit only one abstract class. An interface defines only methods, and to implement an interface is to implement all the methods of the interface. Abstract classes can implement partial methods. Multiple unrelated classes can implement the same interface, and a class can implement multiple unrelated interfaces