java Foundation Part 1: Introduction

1. This chapter includes the basic introduction to the object;
2. Introduction of class, abstract class and interface;
3. Introduction of this and super;
4. Introduction to the statice keyword;
5. Introduction of final keyword;
6. Introduction of basic data types;

Three characteristics of object oriented

  1. Encapsulation: hide important information and hide it reasonably to make the data more secure
  2. Inheritance: Based on the relationship between two classes, the subclass inherits the non private modified methods and attributes (variables) of the parent class. Note that inheritance belongs to a strong coupling relationship
  3. Polymorphism: three conditions for realizing polymorphism (inheritance, rewriting and upward transformation)

Composition of classes

  • Define class:
    • When multiple class classes are defined in the same file, only one class can be modified by public, and the class name of the class modified by public must be the same as the java file name
  • Five components of the class:
    • Member variable: describes the attribute information of a class or object
    • Member method: describes the behavior of a class or object
    • Constructor: returns an object of the initialization class
    • Code block: all members of a class
    • Inner class: a class defined by a class
  • Note: java classes can only inherit single, but can inherit multiple layers

constructor

  • The constructor name should be the same as the class name. Constructors with different formal parameter lists have different initialized objects. They are generally divided into no parameter and full parameter. You can also define different formal parameters yourself

Composition of abstract classes

  • Abstract classes prefixed with public abstract class can be understood as classes modified by abstract
  • You can have private properties and private methods
  • The declared abstract method must be in the abstract class. The declared abstract class does not have to have abstract methods, but all abstract methods must be implemented in subclasses, otherwise subclasses are also defined as abstract classes
  • You can have constructors, but you cannot instantiate them
  • Abstract methods cannot be modified by private

Abstract class definition format:

/**
Abstract class format
*/
abstract class Class name { 
  
}
/**
Code example
*/
public abstract class Animal {
    public abstract void run();
}

Use cases of abstract classes:

// Abstract class
abstract class AbstractEmployee {
	private String id; //Member variable
	private String name;
	private double salary;
	//Constructors in abstract classes are used to initialize parent class members when subclasses create objects
	//Parameterless constructor
	public AbstractEmployee() {
	}
	//Parametric constructor
	public AbstractEmployee(String id, String name, double salary) {
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
	//Abstract method has no method body
	//Abstract classes can have no abstract methods. At that time, abstract methods must exist in abstract classes or interfaces
	abstract public void work();
}

// Abstract classes are implemented through inheritance
class Manager extends AbstractEmployee {
	public Manager() {
	}
	public Manager(String id, String name, double salary) {
		super(id, name, salary);
	}
	// Override all abstract methods of the parent class
	// If you do not override all methods of an abstract class, the inherited class will become an abstract class
	@Override
	public void work() {
		System.out.println("Manage others");
	}
}
//ditto
class Cook extends AbstractEmployee {
	public Cook() {
	}
	public Cook(String id, String name, double salary) {
		super(id, name, salary);
	}
	@Override
	public void work() {
		System.out.println("Cook fry vegetables with more salt...");
	}
}

public class CaseShow {
	public static void main(String[] args) {
		// An abstract class cannot create an object. The abstract method of an abstract class has no method body and cannot be executed, so it cannot be created as an object
		//Create subclass
		Manager m = new Manager();
		m.work();
		
		Cook c = new Cook("p2", "Cook", 1);
		c.work();
	}
}

Composition of interface

  • Keyword interface modified (public interface)
  • It must be public, because it is to be implemented, and private will make the interface meaningless
  • The method in the interface has no method body and can be regarded as a specification for implementing class methods
  • Interface and class are implementation relationships, which are implemented using the keyword implements
  • Cannot instantiate in interface
  • Interfaces can inherit from each other, and interfaces and implementation classes can be implemented
  • The interface member variable is modified by public static final by default, and must be assigned a value and cannot be modified
  • The method in the interface is defined as public abstract by default, whether written or not
  • After 1.8, the interface also has default methods (instance methods), static methods, private instance methods and private static methods
    Definition of interface
/**
Interfaces can inherit more than one
*/
public interface InterfaceName{
    //Abstract method
    //The member variables defined in the interface are decorated with public static final by default
    //The method defined in the interface defaults to the public abstract modifier
    void run(); //public abstract void run();
    //Default method
    //When the sub interface overrides the default method, default can be retained
    //When the implementation class overrides the default method, default cannot be retained
    public default void method1(){
        //Execute statement
    }
    //Static method
    public static void method2(){
        //Execute statement
    }
    //Private method
    private void method3(){
       //Execute statement
    }
}

Interface implementation

/**
Interfaces can be implemented in multiple ways
*/
[Modifier ] class ClassName implement interface1,interface2,interface3...{

}

this keyword and super keyword

  • this represents the current object reference of the class (representing the current object)
  • super represents the upper level object of the class (representing the parent class object)
  • The highest parent class of all classes is Object
  • this() or super(); Both can only exist in the first row of the constructor, and only one of them can exist in the same constructor
  • Cannot be used in static modified variables, methods or code blocks;

static keyword

  • Members modified by static belong to classes and are stored in the static area. Members not modified by static belong to objects
  • You can modify methods, internal classes, and variables
  • Static code block: it must be decorated with static. It must be placed under the class and loaded with the class. It will be executed once as soon as the program is started

final keyword

  • Class variables and methods can be modified
    -The modified class cannot be inherited
    -Modified methods cannot be overridden
    -The modified variable has and can only be assigned once

Basic data type

Serial numbertypeByte sizePackagingData range
1byte8-bit(1 byte)Byte-128 ~ 127
2char16 bit (2 bytes)Character0~65535
3booleanBooleantrue or false
4short16 bit (2 bytes)Short-32768-32767
5int32 bit (4 bytes)Integer-2147483648-2147483647
6long64 bit (8 bytes)Long
7float32 bit (4 bytes)Float
8double64 bit (8 bytes)Double

Keywords: Java

Added by lzylzlz on Sat, 25 Sep 2021 22:01:16 +0300