Easy to understand, take you to understand the application object-oriented (including super simple examples)

Some basic concepts of object-oriented

Software engineering Sixth Edition

The following are the essence of object-oriented, well absorbed, completely mastered.

  • Take a Circle as an example. It has radius, perimeter, color and position. Now we have three different circles, and each Circle is a different object. But they all have the same data (radius, perimeter...) and the same operation (show yourself, reduce the radius, etc.), so they are the same kind of things, which are defined by the Circle class, and three different circles can be declared as circle1, circle2 and circle3, which are specific objects.

  • An instance is a specific object described by a specific class. The class is abstract, not really exists, and no one has seen an abstract circle. In other words, a class is a * * template used when creating objects. The specific objects created according to this sample are the actual examples of the class and become instances * *.

  • A message is a specification of an operation, such as MyCircle Show (GREEN), MyCircle is the name of the object, show is the message name, and GREEN is the argument of the message.

  • Method is the operation performed by the object and the method of responding to the message.

  • Property is the data defined in the class

  • Encapsulation is to wrap a transaction so that the outside world does not know the specific content of the thing.

  • Inheritance is a mechanism that can directly obtain the existing properties and features without repeatedly defining them, and subclasses automatically share the data and methods defined in the base class.

    For example: now there are two classes, class A and class B. let class B inherit class A. class B inherits features from parent class A in addition to its own features (data and operations). When creating class a a1, a1 uses class A as a template to create instance variables (allocate the required space in memory), but it does not benefit from class A.

    When creating instance b1 of class B, b1 should create instance variables with class B as the template and class A as the template. The operations that b1 can perform include both methods defined in class B and methods defined in class A, which is inheritance. If the data or operation with the same name as that in class A is defined in class B, b1 only uses the data or operation defined in class B. unless special measures are taken, the data or operation with the same name in class a cannot be used in b1.

  • Polymorphism is meant to have many forms ", Greek. It means that a subclass object can be used like a parent object, and the same message can be sent to both the parent object and the subclass object. That is, the name of a behavior can be shared in different levels of the class level, but each class in different levels implements the behavior according to its own needs. When the object receives the message sent to it When sending a message, the implementation algorithm defined in the class is selected dynamically according to the class to which the object belongs.

    For example: a = New B (); a.show(); This is that polymorphic / / show must be an inherited method.

    Details of polymorphic use:

    1. In the case of polymorphism, when the child parent class has a member variable with the same name, the member variable of the parent class is accessed.
    2. In the case of polymorphism, when the child parent class has a non static member function with the same name, the member function of the child class is accessed.
    3. In the case of polymorphism, when the child parent class has a static member function with the same name, the member function of the parent class is accessed.
    4. In polymorphic cases, subclass specific members cannot be accessed.

    [premise of polymorphism: there must be inheritance or implementation relationship.]

  • Overloading means that several functions with different parameter characteristics in the same scope can use the same function name. Note the difference between and rewriting.

Take the above idea as an example

package cn.extendsdemo;

public class Animal {
    private int age;
    private  String name;
    private String fiece;

    public int getAge() {
        return age;

    public void setAge(int age) {
        this.age = age;

    public String getName() {
        return name;

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

    public String getFiece() {
        return fiece;

    public void setFiece(String fiece) {
        this.fiece = fiece;
    public  void  run(){
        System.out.println("I can run");
    public  void eat(){
        System.out.println("I can eat");

    public String toString() {
        return "Animal{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", fiece='" + fiece + '\'' +

    public static void main(String[] args) {
        Animal a=new Dog();//polymorphic


public class Cat extends Animal{
static void main(String[] args) {
        Cat tom=new Cat();//tom is an object, a concrete object, while Cat is an abstract concept, so it is called class. Design requirements: the attributes in the class are shared by all objects.
        //Method of inheriting parent class
        //Inherits the properties of the parent class


package cn.extendsdemo;

public class Dog extends Animal {
    public  void  eat(){//rewrite
        System.out.println("I only eat meat and bones");

    public static void main(String[] args) {
        Dog dog=new Dog();
        dog.eat();//When the methods of the subclass and the parent class conflict, the subclass shall be executed first, and the function of the parent class cannot be used.


Keywords: Java Interview OOP

Added by dub on Wed, 02 Mar 2022 04:29:53 +0200