Notes on interfaces, polymorphism, inner classes, and more!

1. Static

The static keyword can modify member variables and member methods

basic concept:

static member belongs to class
It can be accessed through the class name, and is generally modified with public
classname.propertyname; classname.methodname(); can also be accessed by object name (not recommended)

Memory explanation:

Static content is too static to be deployed with any object that belongs to a class, and static variables are also called class variables;
Static variables are assigned when the class is loaded (class level takes precedence over object level)
Multiple objects share a static variable. When one object modifies the variable, it will affect all objects

Static Notes:

Static can only access static! (because of load order)
Non-static can access static

2. Interface

Interface (English interface) in the Java programming language, is an abstract type, is a collection of abstract methods (functions), the interface is declared by interface
It makes up for the drawbacks of single inheritance in Java. Subclasses can implement one or more interfaces while inheriting a class to extend their functions.

Interface definition syntax

public interface interface name {
		public abstract void method name();
		public void method name();  //After compilation it is public abstract void method name();
		void method name(); //After compilation it is public abstract void method name();
}

Interface usage syntax

//class implements interface
public class C implement B ,A {    }

interface inherits interface
public interface D extends B,A{    }

2.1 Characteristics of the interface

  • Interface cannot be instantiated
  • The implementing class must implement all abstract methods in the interface, otherwise the class is a class containing abstract methods and should be defined as an abstract class
  • A class can implement multiple interfaces while inheriting other classes
  • An interface can inherit from multiple interfaces

2.2 Characteristics of members in interfaces

  • Ordinary methods become abstract public methods: add public abstract by default
  • Ordinary member variables become static variables, the default is added: public static final
  • default modifies the default method: subclasses can override or not override
  • The static method in the interface is called directly with the interface class name
public interface IA{
	//1. Static variables can be inherited by subclasses
	public static final int A = 10;
	int B = 20; //After compilation public static final int B = 20 ;
	public int C = 30;   //After compilation public static final int C =30;
	//2. Abstract methods are inherited by subclasses, forcing subclasses to override
	public abstract void show();
	void show1(); //After compilation: public abstract void show1(); 
	//3. Default methods are inherited by subclasses, and subclasses may or may not override them
	public default void show2(){
		Systeam.out.println("IA...>> default show2()........");
}
	//4. Static methods cannot be inherited by subclasses, only belong to this interface and can only be accessed through this interface name
	public static void show3(){
		Systeam.out,println("IA --->  static show3().....");
}

}

3. Polymorphism

Polymorphism: Multiple Forms of One Thing
Polymorphic code embodiment: parent class references point to subclass objects such as: Animal a = new Dog();
After polymorphic invocation: the method invoked is determined by the reference type!

3.1 Polymorphic correlation

  • Polymorphism Premise:

  • There must be an inheritance relationship or an implementation relationship

  • There must be a way for the subclass to override the parent class object (this only makes sense)

  • The advantage of polymorphism: improve code scalability

  • Disadvantage of polymorphism: cannot use methods specific to subclasses

Polymorphism case:

//Two parent classes/interfaces

public class Animal {

    public void eat() {
        System.out.println("eat fragrant...");
    }

}

public interface Fliable {

    void qifei();
    void fei();
    void luodi();
}

//Subclass
public class Cat extends Animal{

    //catch mice
    public void catchMouse() {
        System.out.println("catch mice...");
    }

    @Override
    public void eat() {
        System.out.println("cat scratching...");
    }
}

public class Dog extends Animal {

    public void lookHome() {
        System.out.println("Watching the house....");
    }

    @Override
    public void eat() {
        System.out.println("eat bones...");
    }
}
public class Bird extends Animal implements Fliable{

    //tell the time
    public void baoshi() {
        System.out.println("cuckoo,Now"+new Date());
    }

    @Override
    public void eat() {
        System.out.println("peck to eat..");
    }

    @Override
    public void qifei() {
        System.out.println("wings fly");
    }

    @Override
    public void fei() {
        System.out.println("fly with wings");
    }

    @Override
    public void luodi() {
        System.out.println("fold the wings to the ground");
    }
}

//polymorphic use
public static void main(String[] args) {

        Cat cat = new Cat();
        cat.eat();
        cat.catchMouse();
        System.out.println("===================");
        Dog dog = new Dog();
        dog.eat();
        dog.lookHome();
        System.out.println("===================");
        Bird bird = new Bird();
        bird.eat();
        bird.baoshi();
        bird.qifei();
        bird.fei();
        bird.luodi();
        System.out.println("======The above is before polymorphism=============");
        Animal animal = new Cat();
        animal.eat();
        System.out.println(animal.address);

        Animal animal2 = new Dog();
        animal2.eat();

        Animal animal3 = new Bird();
        animal3.eat();

        Fliable fliable = new Bird();
        fliable.qifei();
        fliable.fei();
        fliable.luodi();

        //==========The following code reflects the benefits of polymorphism =======================
        Animal c = new Cat();  //Different subclass objects are passed into the method method. Although all eat methods are executed, the execution results are different.
        method(c);
        Animal d = new Dog();
        method(d);
        Animal b = new Bird();
        method(b);
}
    
//With polymorphism, you only need to define the parent class type and receive different subclass objects
public static void method(Animal a) {
    a.eat();
}

3.2 Up and down transition

The subclass object of the polymorphism itself is assigned to the parent class variable >> >> >> upward transformation For example: Animal a = new Dog();

Downcasting : coercion
Downcasting is that we can use instanceof to determine the type of object

Such as code:
//create breeder class

public class SiYangYuan {

    public void feed(Animal animal) { //Animal animal = new Cat() defines that the parent class variable can receive subclass objects
        //all animals have to eat
        animal.eat();

        //Determine the type of object
        //If it's a cat, catch the mouse
        if(animal instanceof Cat) {  
            Cat cat = (Cat)animal;  //downcast
            cat.catchMouse();
        }
        //If it's a dog, watch the door
        if(animal instanceof Dog) { 
            Dog dog = (Dog)animal;  //downcast
            dog.lookHome();
        }
    }
}

4. Inner class

Member inner classes are defined at member locations, outside methods

Member inner class definition:

package com.itheima.demo09;

public class Outer{
    //Member variables
    private String name = "big power";
    //member method
    public void method(){...}
    //member inner class
     class Inner{
        public void innerMethod() {
            System.out.println("Member inner class method was called");
            //I am an inner class method
            System.out.println(name);
        }
    }
}

Member inner class object creation:

public class Demo09 {
    public static void main(String[] args) {
        //Create external class object
        Outer outer = new Outer();
        outer.method();

        //Member inner class creates object
        Outer.Inner inner = outer.new Inner();
        Outer.Inner inner2 = new Outer().new Inner();

        inner.innerMethod();
        inner2.innerMethod();
    }
}

The compiled class file of the inner class of the member: Outer$ inner.class The inner class of the member is also an independent class file, which belongs to the outer class of Outer
The member inner class can be modified by modifiers such as private/public/static. If it is modified with static, the object creation method is as follows:

//If the member inner class is modified with static, the object is created in the following format
Outer.Inner inner = new Outer.Inner();

Local inner class definition:

public class Outer{
    //Member variables
    private String name = "big power";
    //member method
    public void method(){

        System.out.println("outer method was called");
        //local inner class
        class Inner2 {
            public void innerMethod2() {
                System.out.println("Local inner class method was called");
                //I am an inner class method
                System.out.println(name);
                System.out.println("=================");
            }
        }

        //Local inner classes are defined, objects are created on-the-fly and used
        Inner2 inner2 = new Inner2();
        inner2.innerMethod2();
    }
}

The compiled class file of the local inner class: Outer $1Inner2.class Local inner class Note; the local inner class has a number

5. Anonymous inner classes

object of local inner class without class name

Format

//Use a format that both defines a local inner class and creates objects of that class
  new parent type(){
      //Override method
  }
  
  //The target parent class, in this case, a subclass object of this class is to be created
  public abstract class Animal {
    public abstract void eat();
  }

  public static void main(String[] args) {
    //After creating an object, you can use polymorphism to assign the subclass object to the parent class reference
    Animal animal = new Animal(){

        @Override
        public void eat() {
            System.out.println("eat.....");
        }
    };
    //call method
    animal.eat();
  }

Tags: Java C++ Algorithm

Posted by ess14 on Wed, 31 Aug 2022 03:28:19 +0300