Object-oriented Part Two


public class Father {
    public void run(){
        System.out.println("Dad Runs Again");
    public void say(){
        System.out.println("A sentence was said");
public class Son extends Father {
    public void run() {
        System.out.println("Son Runs Again");
    public void say() {
public static void main(String[] args) {
        Father son = new Son();//Class points to different references, parent class and self
        Son son1 = new Son();
        son.run();//When a subclass overrides a method, the parent references a method that calls the subclass
        son.say();//Did the subclass not override or did it call the parent class's
    //Son Runs Again
    //Son Runs Again
    //A sentence was said
    //A sentence was said

Polymorphic considerations

  • Inheritance is required, there is no polymorphism without inheritance
  • Method can be overridden, property cannot
  • Polymorphism must be a reference to the parent class

instanceof type conversion

Used to determine what type of object it is

public static void main(String[] args) {
    Father son = new Son();
    Object object = new Object();
    System.out.println(son instanceof Son);
    System.out.println(son instanceof Father);
    System.out.println(son instanceof Object);
    System.out.println(object instanceof Son);
    System.out.println(object instanceof Father);
    System.out.println(object instanceof Object);

Summary: (You can understand intanceof as if you are a direct descendant of me)

  • x instanceof y.As long as x is a descendant of y, (self, son, grandson, etc.), it is true
  • Not the other way around

Object Cast

Principle of Coercive Conversion

  • Parent Reference Points to Subclass
  • The parent class does not lose a method by turning to a subclass, and all methods of the subclass can be used
  • Subclasses that turn to their parent lose their own methods.

Function: Convenient method invocation, reduce duplicate code

Static Properties

Static Properties

Static method

Static Code Block

public class StaticTest {
    static {//Execute only once, create n objects, and execute only once
        System.out.println("Static Code Block");
    {//Executes every time, which can be used to assign an initial value to an object because it is created with the object each time
        System.out.println("Anonymous Code Block");
    public StaticTest(){
        System.out.println("Construction method");
    public static void main(String[] args) {
        StaticTest st = new StaticTest();
    //Static Code Block
    //Anonymous Code Block
    //Construction method

Instantiate an object, unexpectedly loading order is (static code block - > anonymous code block - > construction method)

Static Import Package

import static java.lang.Math.random;//After importing the package statically, you can call it directly with random(), without requiring
System.out.println(Math.random());//No static import, only this
System.out.println(random());//You can do this

final modifies a class that cannot be inherited

Abstract classes (inheritance alone is better than interfaces)

Cannot newabstract class, only newsubclass

An abstract method requires a subclass to implement, and an abstract method can only be defined in an abstract class

Ordinary methods can also be written in abstract classes

Think: What is the role of abstract classes?

Why use abstract classes?Improving development efficiency

Do abstract classes have constructors?


Specification only


  • Specification of interface, what type of method?
  • Constant type in interface?
  • Naming specifications for interfaces and implementation classes


  1. To implement an interface, you must override the methods in the interface
  2. Methods in interfaces are abstract methods, and the default is public abstract modification
  3. Properties in interfaces are static constants, defaulting to public static final
  4. An implementation class in an interface such as a User whose implementation class is UserImpl

Tags: Java

Posted on Sat, 18 Apr 2020 21:14:02 -0700 by moret