An abstract class

Abstract class features:

  • An abstract class is also a class
  • Classes modified by abstract are abstract classes, and methods modified by abstract are abstract methods
  • Abstract methods must exist in abstract classes, and abstract methods have no method body
  • There are constructors in abstract classes, but no instances can be created
  • Fields, methods exist and can be called by subclasses
  • Abstract classes are usually used as template classes, and a class that inherits an abstract class must override the abstract method

Code examples:

public class Test {
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle(1.5.3.0);
        doublearea = rectangle.getArea(); System.out.println(area); }}/** * Graphics abstraction class */
abstract class Graph{
    // Area abstraction method
    public abstract double getArea(a);
}

/** * rectangle */
class Rectangle extends Graph{
    / / width
    private double width;
    / / the length
    private double length;

    public Rectangle(double width,double length){
        this.width = width;
        this.length = length;
    }
    
    // Override the abstract method
    @Override
    public double getArea(a){
        returnwidth * length; }}Copy the code

The results

4.5 I am a method of an abstract class. I am a field of an abstract classCopy the code

interface

An interface is a similar structure to a class. It is defined by the interface keyword and is used to define specifications and global constants

Note: ‘

  • The fields in the interface are global constants and are defined by defaultpublic static finalModify, so it can passInterface name. Field name call
  • The methods in the interface are abstract methods and default topublic abstractModifier, so there is no method body
  • The implementation between classes and interfaces (implementsClass implementation interfaces must override all methods in the interface
  • Interfaces and interfaces are also implementation relationships
  • Abstract classes are also polymorphic

Interface Definition

Code examples:

/** * defines the interface */
interface A{
    // Fields in the interface are global constants and are modified by public static final by default
    String CD = "Chengdu";

    // The methods in the interface are abstract methods, which are by default public abstract
    void test(a);
}
class B implements A{
    // Override interface abstract methods, which must be specified by
    @Override
    public void test(a){
        System.out.println("Override interface methods"); }}Copy the code

Interface inheritance and implementation

Class inheritance features: Single inheritance, multiple inheritance, no explicit inheritance relationship, must implicitly inherit the Object class

Interface inheritance and implementation features:

  • Interfaces can have multiple inheritance and multiple inheritance
  • There can be multiple implementations between interfaces and classes
  • A class inherits both a class and implements the interface. Inheritance must be written first
interface A{}interface C{}/** ** multiple inheritance */
interface D extends A.C{}/** * multiple inheritance */
interface E extends D{}/** ** multiple implementations */
class B implements A.C{}/** * inherits and implements */
class F extends B implements A.C{}interface A{}interface C{}/** ** multiple inheritance */
interface D extends A.C{}/** * multiple inheritance */
interface E extends D{}/** ** multiple implementations */
class B implements A.C{}/** * inherits and implements */
class F extends B implements A.C{}Copy the code

The methods in the interface are abstract methods, decorated by public Abstract by default


The inner class

Inner class: a class within a class

Inner class method call:

public class InnerClassDemo {
    public static void main(String[] args) {
        // Create an inner class object and call the inner class method
        new Outer().new Inner(a).test(a); }}/** * External class */
class Outer{
    /** * Inner class */
    class Inner{
        public void test(a){
            System.out.println("Ha ha ha ha."); }}}Copy the code

Static inner class method calls:

public class InnerClassDemo {
    public static void main(String[] args) {
        // Call a static inner class method
        newOuter.Inner().test(); }}/** * External class */
class Outer{
    /** * Inner class */
    static class Inner{
        public void test(a){
            System.out.println("Ha ha ha ha."); }}}Copy the code

Invokes static methods of static inner classes

public class InnerClassDemo {
    public static void main(String[] args) {
        // Call static inner class static methodsOuter.Inner.test(); }}/** * External class */
class Outer{
    /** * Inner class */
    static class Inner{
        public static void test(a){
            System.out.println("Ha ha ha ha."); }}}Copy the code

Anonymous inner class

An anonymous inner class is an inner class that has no name and is used once

Define anonymous inner class syntax:

newInterface name (){override interface method};Copy the code

Code examples:

public class InnerClassDemo {
    public static void main(String[] args) {
        // Anonymous inner class definition
        new Usb(){
            @Override
            public void test(a){
                System.out.println("I'm a method of anonymous inner class rewriting."); } }.test(); }}interface Usb{
    void test(a);
}
Copy the code

Running results:

I'm an anonymous inner class override methodCopy the code