Inheritance and abstraction of java learning

Inheritance and abstraction of java learning

0x00 Preface

The three characteristics of object-oriented are encapsulation, inheritance and polymorphism. Let's talk about inheritance this time.

0x01 inheritance

Overview: when the same attributes and behaviors exist in multiple classes, these contents are extracted into a single class, so multiple classes do not need to define these attributes and behaviors, just inherit that class. Among them, multiple classes can be called subclasses, and a single class is called parent class, superclass or base class. Inheritance describes the ownership relationship between things.

Inheritance can improve the reusability of code, and there is a relationship between classes, which is the premise of polymorphism.

Inherited statement format:

class 父类{
    ...
}
class 子类 extends 父类{
    ...
}

If the child class and parent class have member variables with different names, the access will not be affected at this time.

父类代码:
public class Fu {
    int num = 10;

}

子类代码:
int num2 = 6;
    public void show(){
        System.out.println("父类成员变量"+num);
        System.out.println("子类成员变量:"+num2);

    }

main代码:
public static void main(String[] args) {
        Zi zi = new Zi();
        zi.show();
    }

Successfully input the member variable of the child class and the member variable of the parent class. However, when writing code, it is inevitable that some variables have duplicate names, so it will have an impact when accessing at this time. If the name is the same, the member variables of the subclass will be displayed first. At this time, we will use the two keywords this and super.

Use format:

Super: access member variables or methods of the parent class

This: access member methods in this class

Super is used in subclasses to access the parent class and this class.

Code of member variable:

    int num = 6;
    public void show(){
        System.out.println("父类成员变量"+super.num);
        System.out.println("子类成员变量:"+this.num);
        
    }

Then try accessing the member method with the same name. Before that, let's understand a concept - rewriting.

If a member method with the same name appears in the parent class of a child class, the access is a special case, which is called method override.

父类代码:
public class Fu {
    int num = 10;


    public void show(){

        System.out.println("我是父类");
    }
}

子类代码:
public class Zi extends Fu {
    int num = 6;

    @Override
    public void show() {
        System.out.println("我是子类");
        super.show();
    }
}

main方法内代码:
public static void main(String[] args) {
        Zi zi = new Zi();
        zi.show();
        
    }


Here, let the show () method of the subclass directly call the show () method of the parent class. If you override, you must declare the method override with @ override.

Notes for rewriting:

0x02 construction method

父类代码:
public class Fu {
    int num = 10;

    public Fu() {
        System.out.println("父类构造方法");
    }

子类代码:
public class Zi extends Fu {
    int num = 6;

    public Zi() {
        System.out.println("子类构造方法");
    }

   
}
main方法代码:
    public static void main(String[] args) {
        Zi zi = new Zi();


    }

No method is called here. New creates an instantiated object, but it still prints something. This is the function of the constructor to initialize the object when it is created.

It's extracted here separately for memory.

Insert some features about inheritance here.

In Java, only single inheritance is supported, and multiple inheritance is not supported.

However, Java supports multi-layer inheritance (inheritance system)

For example:

class A{}
class B extends A{}
class C extends B{}

The top-level parent class is the object class. All classes inherit object by default as the parent class.

0x03 abstract class

Let's first understand the basic concepts and uses of abstract classes.

We can directly use the abstract keyword to modify the method, so the method becomes an abstract method. The abstract method only contains a method name and no actual method body.

Format:

修饰符 abstract 返回值类型 方法名 (参数列表);

public abstract void run();

If a class contains abstract methods, the class must be an abstract class, which is easier to understand.

If there are abstract methods, the abstract modifier is also added to the front of the class.

public abstract class 类名{
    
}


public abstract class Animal {
    public abstract void run();
}

Subclasses that inherit abstract classes must override all abstract methods. Otherwise, the subclass must also be declared as an abstract class. Finally, a subclass must implement the abstract method of the parent class. Otherwise, objects cannot be created from the original parent class to the final subclass, which is meaningless.

That is, you need to create a class to override its abstract methods, which is called an implementation class. Then create another implementation class here and rewrite the abstract class just defined.

实现类代码:
public class Cat extends Animal {

    @Override
    public void run() {
        System.out.println("撸猫 吸猫");
    }
}

main方法代码:

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

The run method is rewritten here, which is the complete implementation of the abstract method of the parent class by the subclass. The operation of this method rewriting is called the implementation method.

Precautions for abstract classes:

0x04 end

The foundation of inheritance and abstraction has been written. The next article updates interfaces and polymorphism. These are also closely related to the inheritance and abstraction of this article.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>