Day05 instanceof keyword, abstract class, interface

Keywords: Java

instanceof keyword

Person a = new Student();


 // Object -->  Person -->  Student
        Object a = new Student();

        System.out.println(a instanceof Object);			//true
        System.out.println(a instanceof Person);			//true
        System.out.println(a instanceof Student);			//true

        Person b = new Student();
        System.out.println(b instanceof Object);			//true
        System.out.println(b instanceof Person);			//true
        System.out.println(b instanceof Student);			//true

        Student c = new Student();
        System.out.println(c instanceof Object);			//true
        System.out.println(c instanceof Person);			//true
        System.out.println(c instanceof Student);			//true

		Object d = new Person();
        System.out.println(d instanceof Object);			//true
        System.out.println(d instanceof Person);			//true
        System.out.println(d instanceof Student);			//false


Summary: when using polymorphism, the class accepted on the left must be itself or its parent class or higher level. When using the instancof keyword to judge the type, you actually look at the type of new on the right. If the class on the right of instancof is a new class or a parent class or higher class of new class, it is true; otherwise, if it is a child class of new class, it is false


Conversion between types

Low to high does not require strong turn, while high to low requires strong turn

/*
Student Class has a run method
 There is no run method in the Person class
*/
Student student = new Student();
student.go();
Psron person = student;

//In the last line, the parent class refers to the object pointing to the child class. Person cannot call the run method because there is no run method in the person class
//Therefore, if a subclass is converted to a parent class, it may lose some of its original methods


  1. A parent class reference points to an object of a subclass (an object that cannot be a subclass points to an object of a parent class)
  2. Convert the subclass into the parent class and transform upward; No cast is required
  3. Convert the parent class into a child class and transform downward; Force conversion
  4. Convenient method call and reduce duplicate code!

static keyword
  1. The member method modified by static belongs to a class and can be called directly through the class instead of creating an object
  2. When creating multiple objects of a class, the static code block will only be executed when creating the first object. When creating other objects of this kind in the future, the static code block will not be executed, because the static code block belongs to a class. It has been created when creating the first object, and it does not need to be created after that.

Clear the difference between static code blocks and anonymous code blocks:

Static code blocks are written outside with static, and the code blocks are wrapped in curly braces

Anonymous code blocks do not have static, and code blocks are wrapped in curly braces

Note: anonymous code blocks are generally used for data initialization. Anonymous code blocks belong to objects rather than classes


Execution order: static code block > anonymous code block > construction method


package com.kuang.www;
import javax.management.ObjectName;
import java.util.Scanner;			//Scanner is a class

import static java.lang.Math.*;		//Math is a class

public class HelloWorld {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.println(random());//When importing all static code blocks (packages) under the class math, math in Math.random() can be omitted and only random () is written
          
        in.close();   
    }
}


Note: a class modified by final cannot be inherited


abstract class

Abstract abstract class: class extensions: single inheritance (interfaces can inherit more than one)

package com.kuang.www;
	//abstract class
public abstract class Action {

    //Abstract, abstract method, only method name, no method implementation!
    public abstract void doSomething();
    public Action(){
        
    }
    /*
    1.new is not an abstract class, it can only be implemented by subclasses; Constraints!
    2.Ordinary methods can be written in abstract classes
    3.Abstract methods must be in abstract classes

    */

}

  • The abstract modifier can be used to modify a method or a class. If you modify a method, the method is an abstract method; If you modify a class, it is an abstract class
  • Abstract classes can have no abstract methods, but abstract methods must be declared as abstract classes
  • An abstract class cannot use the new keyword to create an object. It is used to allow subclasses to inherit
  • Abstract methods have only method declarations and no method implementations. They are used to implement subclasses
  • If a subclass inherits an abstract class, it must implement an abstract method that the abstract class does not implement, otherwise the subclass must also be declared as an abstract class

Interface

  • Common class: only concrete implementation

  • Abstract classes: concrete implementations and specifications (abstract methods) are available!

  • Interface: only specification! I can't do my homework ~ professional constraints! Separation of constraints and Implementation: interface oriented programming

  • An interface is a specification that defines a set of rules.

  • The essence of interface is contract, just like the law between us. After making it, everyone will abide by it

  • The essence of object-oriented is the abstraction of objects. The interface can best reflect this.

The keyword for declaring a class is class, and the keyword for declaring an interface is interface

package com.kuang.www;

//The keyword of the interface is interface, and all interfaces need to have implementation classes
public interface UserService {

    //All defined methods in the interface are abstract. The default is public abstract, so the public interface can be omitted without writing
    void add(String name);
    void delete(String name);
    void modify();
    void query();

    public abstract void eat();
}

Implementation class

package com.kuang.www;

//Abstract class extends


//Class can implement the implements interface
//If you implement the class of the interface, you need to override all the methods of the interface

//Multi inheritance, using interfaces to achieve multi inheritance
```java
public class UserServiceImpl implements UserService,TimeService{

    @Override
    public void add(String name) {

    }

    @Override
    public void delete(String name) {

    }

    @Override
    public void modify() {

    }

    @Override
    public void query() {

    }

    @Override
    public void eat() {

    }

    @Override
    public void timer() {

    }
}


Function of interface

  1. constraint
  2. Define some methods for different people to implement
  3. The default modifier of a method in the public abstract interface is equivalent to declaring a method in the interface. The default is an abstract method
  4. public static final is the default modifier of variable. Defining a variable is equivalent to defining a constant (generally not used)
  5. The interface cannot be instantiated. There is no constructor in the interface
  6. implements can implement multiple interfaces
  7. You must override the methods in the interface

Posted by M. Abdel-Ghani on Fri, 24 Sep 2021 00:45:25 -0700