Java foundation super detail

Posted by Ulujain on Tue, 15 Feb 2022 08:37:22 +0100

catalogue

preface

1, What is object-oriented?

2, Three characteristics of object-oriented

1. Packaging

2. Succession

3. Polymorphism

1. Heavy load

2. Rewrite

3. Abstract class

4. Interface

summary

preface

Last time we talked about Java's basic data types, operators and permission modifiers. This time, let's learn the most familiar object-oriented concepts in Java. Dear friends, it's not easy to sum up. If you feel helpful, please come and pay attention!!!

1, What is object-oriented?

Characteristics of the object:

  • Objects have properties and behaviors.
  • The object has a changing state.
  • Object is unique.
  • Objects are instances of a category.
  • Everything is an object, and everything in the real world can be regarded as an object.

The object-oriented development mode is more conducive to people's open thinking, facilitate the division of programs in the specific development process, facilitate the division of work and cooperation of programmers, and improve the development efficiency. Advantages of object-oriented programming:

  1. Reusability: code reuse, reduce the amount of code and improve development efficiency.
  2. Scalability: it means that new functions can be easily added to the system to facilitate software modification.
  3. Manageability: it can combine functions with data to facilitate management.

2, Three characteristics of object-oriented

1. Packaging

Basic concept: it refers to a method to wrap and hide the implementation details of abstract function interfaces

advantage:

  • 1. Good packaging can reduce coupling.

  • 2. The internal structure of the class can be modified freely.

  • 3. You can control the member variables more accurately.

  • 4. Hide information and realize details.

When we create an object, we can restrict the direct access to the object attributes by modifying the visibility of the attributes (generally decorated with private), and then provide external public methods for each attribute value to access private attributes through public methods

The code is as follows (example):

public class User {

    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

2. Succession

Basic concept: a subclass inherits the properties and behaviors of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behaviors as the parent class.

Advantages: avoid repetition, easy to maintain and understand

The code is as follows (example):

Create a parent User class and define public properties and methods

public class User {

    //attribute
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    //behavior
    void say(){
        System.out.println("I am"+name+","+"this year"+age+"Yes");
    }
}

Define the subclass vip class, inherit the User parent class with the extend keyword, and create its unique properties and methods.

public class Vip extends User{

    //attribute
    private String level;

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    //behavior
    void level(){
        System.out.println("my vip The grade is"+level);
    }
}

Create a test class userTest, create a vip object, and find that it also the properties and behaviors of user and vip

public class UserTest {

    public static void main(String[] args) {
        Vip vip = new Vip();
        //The vip class inherits the user class and has the user's properties and methods
        vip.setName("Zhang San");
        vip.setAge("22");
        vip.setLevel("3");
        vip.say();
        vip.level();
    }

}

3. Polymorphism

Basic concept: allows different objects to respond to the same message. That is, the same message can adopt many different behavior modes according to different sending objects. (sending a message is a function call)

Advantage: decoupling between types

Polymorphic expressions include overloading, rewriting, abstract classes and interfaces. They are introduced one by one below.

1. Heavy load

Basic concept: overloading is in a class with the same method name but different parameters. The return type can be the same or different. Each overloaded method (or constructor) must have a unique list of parameter types.

Rules:

  • The overloaded method must change the parameter list (the number or type of parameters are different);
  • Overloaded methods can change the return type;
  • The overloaded method can change the access modifier;
  • Overloaded methods can declare new or broader check exceptions;
  • Methods can be overloaded in the same class or in a subclass.
  • The return value type cannot be used as a distinguishing criterion for overloaded functions.

The most common is the construction method. The construction method with three different parameters but the same method name in the following code is the implementation of overloading.

The code is as follows (example):

public class User {

    //attribute
    private String name;

    private String age;

    //Nonparametric structure
    public User() {
    }
    //Parametric structure
    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }
    //Parametric structure
    public User(String name) {
        this.name = name;
    }

    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    //behavior
    void say(){
        System.out.println("I am"+name+","+"this year"+age+"Yes");
    }
}

2. Rewrite

Basic concept: rewriting is that a subclass rewrites the implementation process of the accessible methods of the parent class, and the return value and formal parameters cannot be changed.

Rules:

  • The parameter list must be exactly the same as the parameter list of the overridden method.

  • The return type can be different from the return type of the overridden method, but it must be a derived class of the return value of the parent class (the return type of java5 and earlier versions should be the same, and java7 and later versions can be different).

  • The access permission cannot be lower than the access permission of the overridden method in the parent class. For example, if a method of the parent class is declared public, overriding the method in the child class cannot be declared protected.

  • Member methods of a parent class can only be overridden by its subclasses.

  • A method declared final cannot be overridden.

  • Methods declared as static cannot be overridden, but can be declared again.

  • If the subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except those declared as private and final.

  • If the subclass and the parent class are not in the same package, the subclass can only override the non final methods declared as public and protected by the parent class.

  • The overridden method can throw any non mandatory exception, regardless of whether the overridden method throws an exception or not. However, the overridden method cannot throw new mandatory exceptions, or mandatory exceptions that are more extensive than those declared by the overridden method, and vice versa.

  • Constructor cannot be overridden.

  • If you cannot inherit a class, you cannot override the methods of that class.

Using the example of user, we define an identity() method in the parent class user

The code is as follows (example):

public class User {

    //attribute
    private String name;

    private String age;

    public User() {
    }

    public User(String name, String age) {
        this.name = name;
        this.age = age;
    }

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    //behavior
    void say(){
        System.out.println("I am"+name+","+"this year"+age+"Yes");
    }

    void identity(){
        System.out.println("I'm a user!");
    }

}

Then we inherit this method in the subclass vip

public class Vip extends User{

    //attribute
    private String level;

    public Vip(String name) {
        super(name);
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    //behavior
    void level(){
        System.out.println("my vip The grade is"+level);
    }

    void identity(){
        System.out.println("I am VIP!");
    }
}

Finally, in the test class, we create sub class objects, parent class objects, and parent class references to sub class objects respectively to see how the output results of their corresponding identity() methods are different

public class UserTest {

    public static void main(String[] args) {
        Vip vip = new Vip("Zhang San");
        //The vip class inherits the user class and has the user's properties and methods
//        vip.setName("Zhang San");
        vip.setAge("22");
        vip.setLevel("3");
        vip.say();
        vip.level();
        vip.identity();
        //A parent class reference points to a child class object
        User user = new Vip("Li Si");
        user.setAge("23");
        user.say();
        user.identity();
        //Parent object
        User user1 = new User("Wang Wu");
        user1.setAge("23");
        user1.say();
        user1.identity();
    }

}

 

The running result is obvious. Except for the objects created by the parent class itself, those created by the child class will override the identity() method of the parent class.

3. Abstract class

Basic concept: except that an abstract class cannot instantiate an object, other functions of the class still exist. The access methods of member variables, member methods and construction methods are the same as those of ordinary classes

Rules:

  • 1. Abstract classes cannot be instantiated (a mistake that beginners can easily make). If they are instantiated, an error will be reported and the compilation cannot pass. Only non Abstract subclasses of abstract classes can create objects.

  • 2. Abstract classes do not necessarily contain abstract methods, but classes with abstract methods must be abstract classes.

  • 3. The abstract method in the abstract class is only a declaration and does not contain the method body, that is, it does not give the specific implementation of the method, that is, the specific function of the method.

  • 4. Construct methods. Class methods (Methods decorated with static) cannot be declared as abstract methods.

  • 5. The subclass of an abstract class must give the concrete implementation of the abstract method in the abstract class, unless the subclass is also an abstract class.

We create an abstract class, which is decorated with the abstract keyword, and define attributes, behaviors, construction methods, and abstract methods in the abstract class (abstract methods can only be defined in abstract classes)

The code is as follows (example):

public abstract class People {

    //attribute
    private String name;
    private String age;
    private String sex;

    //Construction method
    public People(String name, String age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    //behavior
    void say(){
        System.out.println("I am"+name+",One"+age+"Year old"+sex+"Child.");
    }

    //Abstract method
    public abstract void love();
}

Because there is no way to instantiate an abstract method, it can only be implemented through subclasses that can be instantiated, so we create an object to inherit this abstract class and rewrite the abstract method

public class Teacher extends People{
    public Teacher(String name, String age, String sex) {
        super(name, age, sex);
    }

    //Rewrite abstract methods
    public void love() {
        System.out.println("Teachers love teaching!");
    }
}

Next, let's create an object to test

public class AbstractDemo {

    public static void main(String[] args) {
        Teacher t = new Teacher("Xiao Hong","20","female");

        t.say();
        t.love();

    }

}

 

4. Interface

Basic concept: an interface is not a class. The way of writing an interface is very similar to that of a class, but they belong to different concepts. Class describes the properties and methods of an object. Interface contains the methods to be implemented by the class. The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class.

Rules:

  • Interfaces cannot be used to instantiate objects.
  • Interface has no constructor.
  • After the default keyword in the interface, all non abstract methods in the interface can be used.
  • An interface cannot contain member variables except static and final variables.
  • The interface is not inherited by the class, but implemented by the class.
  • The interface supports multiple inheritance.

First, use the interface keyword to create a pet interface and define two methods eat() and play ()

public interface Pet {

    void play();
    void eat();

}

Create a class to implement this interface, and rewrite the methods in the interface to call the methods in the interface by instantiating the created object

public class cat implements Pet {
    public void play() {
        System.out.println("Cat catches mouse");
    }

    public void eat() {
        System.out.println("Cats eat dried fish");
    }

    public static void main(String[] args) {
        cat cat = new cat();
        cat.eat();
        cat.play();
    }
}

Operation results:

 

summary

Java object-oriented relatively speaking, this small knowledge point is complex and easy to be confused. It needs more contact. Understand it first and then do it. Since there are many confusing knowledge points here, I will make a comparison between them in the next issue to help you understand. It's not easy to sort it out. I hope you can pay attention to the trouble that is helpful to you. If you find any problems, you are welcome to correct them in time and communicate with each other for progress!!!

Topics: Java Back-end