Object oriented in Java

object-oriented

concept

1. Object oriented is process oriented
2. Both object-oriented and process oriented are ideas
3. Process oriented: it emphasizes functional behavior, and object-oriented: it encapsulates functions into objects and emphasizes objects with functions.
4. Object oriented is based on process oriented

Code display

public class Demo{
    public static void main(String [] args){
        // Process oriented
        System.out.println("Process oriented ---> start --->");
        openIceBox();
        save();
        closeIceBox();
        System.out.println("Process oriented <--- end <---");
        // object-oriented
        System.out.println("object-oriented ---> start --->");
        IceBox iceBox = new IceBox();
        iceBox.openIceBox();
        iceBox.save();
        iceBox.closeIceBox();
        System.out.println("object-oriented <--- end <---");

    }

    public static void openIceBox(){
        System.out.println("Open the refrigerator");
    }

    public static void save(){
        System.out.println("storage");
    }

    public static void closeIceBox(){
        System.out.println("Turn off the refrigerator");
    }
}

class IceBox{
    void openIceBox(){
        System.out.println("Open the refrigerator");
    }

    void save(){
        System.out.println("storage");
    }

    void closeIceBox(){
        System.out.println("Turn off the refrigerator");
    }
}

Object oriented features

Object oriented features: encapsulation, inheritance, polymorphism

encapsulation

Hide the properties and implementation details of the object, and only provide public access
principle
1. Hide all contents that do not need to be provided externally.
2. Hide all attributes and provide public methods to access them.
advantage
1. Isolate changes.
2. Easy to use.
3. Improve reusability.
4. Improve safety.

inherit

1. Improve the reusability of the code
2. Make the relationship between classes and have the characteristics of polymorphism
3. Only single inheritance is supported in the Java language - when multiple parent classes have the same method, the subclass does not know which method to execute - Optimization: multiple implementations

Code display

class Person{
    String name;
    int age;
}
class Student extends Person{
    void study(){
        System.out.println("study hard");
    }
}
class Worker extends Person{
    void work(){
        System.out.println("Work hard");
    }
}

abstract class

Characteristics of abstract classes
1. The abstract method must be in the abstract class
2. Abstract methods and abstract classes must be modified by abstract keyword
3. Abstract classes cannot be created with new
4. When the methods in the abstract class are used, the subclass must copy all the abstract methods, create the subclass object and call it; If only some abstract methods are covered, the class is still an abstract class

Code display

abstract class Animal{
    abstract void eat();
}
class Cat extends Animal{
    void eat(){
        System.out.println("Eat fish");
    }
}
class Dog extends Animal{
    void eat(){
        System.out.println("eat meat");
    }
}

Interface

1. When the methods in an abstract class are abstract, the class can be represented by an interface and defined by an interface
2. Common definitions in interfaces: constants and abstract methods
3. All members in the interface have fixed modifiers
a. Constant: public static final
b. Method: public abstract
4. An interface cannot create objects. Subclasses must implement all its abstract methods and create subclass object calls. On the contrary, subclasses are abstract types if all abstract methods are not implemented
5. Interfaces can be implemented by classes
6. Interface is the principle of external exposure and the function expansion of the program. There can be inheritance relationship between interfaces

polymorphic

There are many forms of withdrawal

Code display

public class Demo{
    public static void main(String [] args){
        Animal dog = new Dog();
        dog.eat();
        Animal cat = new Cat();
        cat.eat();
    }
}

interface Animal{
    void eat();
}

class Dog implements Animal{
    public void eat(){
        System.out.println("Eat bones");
    }
}

class Cat implements Animal{
    public void eat(){
        System.out.println("Eat fish");
    }
}

Embodiment of polymorphism
The reference of the parent class points to its own subclass object
Benefits of polymorphism
Polymorphism improves the extensibility of the program, but it is limited to the members in the parent class
Polymorphic premise
There must be a relationship, inheritance or implementation between classes, and there must be member overrides

Cast of type

The reference of the parent class points to the object of the child class. In a specific case, the parent class reference is cast to the object type of the child class

Code display

public class Demo{
    public static void main(String [] args){
        Animal dog = new Dog();
        dog.eat();
        // Type cast
        Dog dog1 = (Dog) dog;
        dog1.dogFunction();
    }
}

interface Animal{
    void eat();
}

class Dog implements Animal{
    public void eat(){
        System.out.println("Eat bones");
    }
    public void dogFunction(){
        System.out.println("Housekeeping");
    }
}

final keyword

1. You can modify classes, functions and variables
2. The class modified by final cannot be inherited
3. Functions modified by final cannot be overwritten
4. The variable modified by final is a constant and can only be assigned once during initialization. It can modify both local variables and member variables.

Keywords: Java JavaSE

Added by hooch_au78 on Tue, 21 Sep 2021 06:31:54 +0300