Introduction to JAVA Basic Object-Oriented Abstract Class

Abstract class

What is an abstract class?
The abstract class is actually the parent class. The abstract class defines the form that all subclasses should share, but it is not implemented. Instead, each subclass realizes its own personalized function, that is, personalized customization.
So what is an interface?
If we say this abstract class. There are also some methods that can be implemented and can be inherited by subclasses, but interfaces have no method implementation that can be inherited by subclasses. They are all defined methods.
So what is the use of the interface? Can't inherit or use?

In real life, many TV series and movies will lose a lot of shots when they are released. Why? It is because the State Administration of Radio, Film and Television has set some standards. For example, the collar of the clothes cannot be lower than that, and the screen cannot be too bloody and violent. They are not responsible for reducing the standards. People who make movies must be responsible for achieving this standard. , So this is the relationship between standards and implementation in reality. In fact, interfaces are standards in programs.

One, abstract class, interface and inner class

Insert picture description here

Abstract classes and abstract methods

●Abstract class is to define only the form shared by all subclasses, without defining the parent class of specific implementation details

(The parent class really doesn't know how to help the subclass to achieve, but these functions are necessary, so I had to give a definition, relying on the subclass to self-reliance. When to use the abstract class? In the future, as long as some functions in the parent class cannot help Subclass implementation, the parent class is defined as an abstract class)

●Syntax: public abstract class class name...
●In abstract classes, those methods that have only method definitions and no implementation details are called abstract methods;
●Syntax: [access modifier] abstract return value type method name (;
●In a class If it contains abstract methods, the class should be declared as an abstract class with the abstract keyword;
● If a class inherits an abstract class, its abstract methods must be rewritten (unless the class is also declared as an abstract class). Different subclasses can be different The realization.

Insert picture description here


Listen to stories and learn abstract

Insert picture description here

package day08;

/**
 * 封装父亲属性和功能的类
 * 因为内部包含抽象方法所以也要定义为抽象类
 */
public abstract class Father {
    
    
    /**
     * 父亲养育儿女的方法
     * 儿女可以无条件继承
     * @return 养育的结果
     */
    public String raise(){
    
    
       return "学业有成,成家立业";
    }

    /**
     * 父亲请求子女帮忙偿还债务
     * 也就是说,父亲自己无法完成
     * @return 每个子女负担债务
     */
    public abstract double pay();
}

Insert picture description here


Encapsulate the eldest son's class

package day08;

/**
 * 封装大儿子的类
 */
public class Child1 extends Father{
    
    
//编译错误:因为只要继承了抽象类,就必须重写父类的抽象方法!
// 解决: 鼠标方法在错误上,选择add unimplemented method
    @Override
    public double pay() {
    
    
        return 10;//大儿子给10万
    }
}

Encapsulate the second son class

package day08;

/**
 * 封装二儿子的类
 */
public class Child2 extends Father{
    
    

    @Override
    public double pay() {
    
    
        return 25;//二儿子给25万
    }
}

Encapsulate the class of the younger son

package day08;

/**
 * 封装小儿子的类
 */
public class Child3 extends Father{
    
    

    @Override
    public double pay() {
    
    
        return 7.5;//小儿子给7.5万
    }
}

Encapsulate the little girl's class

package day08;

/**
 * 封装小女儿的类
 */
public class Child4 extends Father{
    
    

    @Override
    public double pay() {
    
    
        return 5;//小女儿给5万
    }
}

Test category:
remember! Abstract class cannot be instantiated

package day08;

public class Test {
    
    
    public static void main(String[] args) {
    
    
      //Father f=new Father(); 编译错误:切记!抽象类不能实例化
      //因为抽象类都是不完整的!
      //要想使用抽象类中的普通方法,只能通过子类对象继承后调用
      //父亲抚养4个孩子成人
        Child1 c1=new Child1();
        Child2 c2=new Child2();
        Child3 c3=new Child3();
        Child4 c4=new Child4();
        System.out.println("大儿子"+c1.raise());
        System.out.println("二儿子"+c2.raise());
        System.out.println("小儿子"+c3.raise());
        System.out.println("小女儿"+c4.raise());
        //结论:子类可以继承抽象类中的已实现方法和继承普通父类完全一样。
    }
}

Conclusion: The subclass can inherit the implemented methods in the abstract class and it is exactly the same as the ordinary parent class.
Insert picture description here


Case: Father called 4 children together

package day08;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        Child1 c1=new Child1();
        Child2 c2=new Child2();
        Child3 c3=new Child3();
        Child4 c4=new Child4();
        System.out.println("大儿子"+c1.raise());
        System.out.println("二儿子"+c2.raise());
        System.out.println("小儿子"+c3.raise());
        System.out.println("小女儿"+c4.raise());
        //结论:子类可以继承抽象类中的已实现方法和继承普通父类完全一样。
        //父亲将4个儿女叫到一起
        //程序中,集中保存东西,都是使用数组
        //问题1:4个儿女对象,数据类型不相同,怎么放到同一个数组中呢?
        //解决:父类型的引用,可以指向任何子类的对象
        //完全可以将数组元素的类型,统一定义为父类型
        Father[] children={
    
    c1,c2,c3,c4};
        System.out.println("大儿子 | 二儿子 | 三儿子 | 小女儿");
        //因为每个孩子都帮父亲实现了pay方法。可以循环调用每一个孩子的pay方法
        for (int i = 0; i < children.length; i++) {
    
    
            //问题2:children中每个元素是什么类型?凭什么可以调用子类实现的pay方法?
            //原因:children中的每一个元素都是Father父类
            //子类都重写了父类抽象方法pay,所以父类型的引用,
            //可以调用子类对象重写的父类方法
            System.out.print(children[i].pay()+"万   ");
        }
        System.out.println();
        System.out.println("---------分割线--------");
        //父亲公布的遗产:
        for (int i = 0; i < children.length; i++) {
    
    
            System.out.print(children[i].pay()*5+"万   ");

        }
    }
}

Abstract classes cannot be instantiated, because the parent class is incomplete:

Father father=new Father(); //Compile error

In addition, even if there is no abstract method in a class, you can also use abstract to modify the class. The effect is to prevent the class from being instantiated.

abstract VS final:

Abstract class: it is born to be inherited! Without inheritance, it has no meaning.
Final: It is specifically used to prohibit inheritance! The meaning of an abstract class is just the opposite!
So: abstract and fina| cannot modify a class at the same time

The significance of abstract classes is:

-Provide a common type for its subclasses-the meaning of inheritance
-encapsulate the repeated members in the subclass (to avoid duplication)-the meaning of inheritance
-define abstract methods that stipulate that the subclass is obliged to help the parent class to complete the function- An abstract meaning

Simply put: abstract classes are born to be inherited and rewritten, and fathers and sons pay for their debts!

Guess you like

Origin blog.csdn.net/QQ1043051018/article/details/112495201