Zero foundation Java self-study process - advanced Java language 41

Posted by steonkeys on Wed, 13 Oct 2021 01:45:16 +0200

Java abstract class

1, Definition of abstract class

The class modified by the keyword "abstract" is an abstract class. (moreover, abxtract can only modify classes and methods)

The simplest empty abstract class is shown below

public abstract class AbstractClass {
    public static void main(String[] args) {
        AbstractClass abstractClass=new AbstractClass();
    }
}

When instantiating this empty abstract class, the compiler will report an error:

  'AbstractClass' is abstract; cannot be instantiated'

  Now extend this abstract class and add properties and methods:

public abstract class AbstractClass {
    public int constInt = 5;

    /*Overload method()*/
    public void method() { }

    //No compilation errors
    public abstract void method(int a);

    public static void main(String[] args) {
        AbstractClass abstractClass=new AbstractClass() {
            @Override
            public void method(int a) {
                System.out.println("Instantiate abstract classes");
            }
        };
        System.out.println(abstractClass.constInt);
        abstractClass.method(5);
    }
}

//Operation results
/*
  5
  Instantiate abstract classes
*/

In this abstract class, I added an instance property, an abstract method and an overloaded instance method of the abstract method, all of which are legal.

In the main method, use the new operator to directly instantiate the abstract class, and the IDE will unexpectedly prompt the operator directly -- generate an Anonymous Inner class.

Here are some things you need to know about anonymous inner classes:

A special local internal class has no class name, no class keyword, and no modifications to the extensions and implements keywords.

Anonymous inner classes cannot be abstract (that is, they cannot have abstract methods). They must implement all abstract methods of their abstract superclasses or interfaces.

Because there is no class name, anonymous inner classes can only be used once, usually to simplify code writing.

A prerequisite for using anonymous inner classes is to inherit the parent class or implement the interface.

The new operator is used to specify which parent class to inherit or which interface to implement. (in fact, new calls the constructor directly. New is a very cool operator), and then constructs an instance of the new class by directly defining the class body (implementing some methods).

Therefore, in the above code, I generated a new instance of anonymous inner class, which inherited AbstractClass, implemented the abstract method of the parent class, assigned the instance to AbstractClass, and called the new method (int 5) method from the instance.

2, Abstract classes and abstract methods

Abstract methods have only method declarations, and methods without method bodies. It will be implemented by subclasses (either abstract or non Abstract).

It can be seen from the methods of the empty abstract class above that having an abstract method is not a necessary and sufficient condition for building an abstract class.

So, is it legal to have an ordinary class with abstract methods? Probability is illegal, because what's the point if such a design is legal?

In fact, if we define an abstract method in a non abstract class, the IDE will prompt:

  "Abstract method in non-abstract class"

If we have to run an error code as follows:  

public class AbstractTest {

    public abstract void test();

    public static void main(String[] args) {
        
    }
}

The compiler error message is:

Error: Java: AbstractTest is not abstract and does not override biguo classConstruct. Abstract method in abstract test()

Therefore, abstract methods can only exist in abstract classes.

Can abstract methods be static?

In the next test, I thought of an interesting question. Can abstract methods in abstract classes be static?

Error: Java: invalid modifier combination: abstract and static. Error: Java: invalid modifier combination: abstract and static

public abstract class AbstractTest {
 //Illegal modifier combination
    public static abstract void test();

    public static void main(String[] args) {
        
    }
}

Static member methods mean that they can be used (inside or through a class) without instantiation. However, it can also be accessed from the instance without errors, but the IDE will issue a warning because it violates the static design semantics;

Abstract methods mean that there is no method body, that is, there is only one method declaration that needs to be implemented by subclasses.

We need to realize that we can have static methods in abstract classes. For example, if we put the main method in an abstract class, our program can run from it.

In this case, the combination of static abstract methods has no meaning to this abstract class. Because it has no method body, it cannot be used by classes.

It is completely acceptable to allow such a "static abstract" method, because in a non Abstract subclass, the sub method implementing the abstract method is still static, that is, the class method of the subclass.

This statement has some meaning, but what it still does not solve is the violation of the "static" semantics of the parent class by the "static abstract" method

Can static methods be overridden by subclasses?

The answer is: static methods cannot be overridden by subclasses. (including rewrite definition)

But! In fact, we can redefine the same method as its static parent method test() in the subclass.

package biguo.classConstruct;

public class AbstractTest {

    public static  void test(){
        System.out.println("This is AbstractTest's static test!");
    }

    public static  void print(){
        System.out.println("This is AbstractTest's static print!");
    }

    public static void main(String[] args) {
        AbstractTest.test();
        AbstractTestSon.test();
        AbstractTestSon.print();
        System.out.println();

        AbstractTestSon abstractTestSon=new AbstractTestSon();
        abstractTestSon.print();
        abstractTestSon.test();
    }
}

class AbstractTestSon extends AbstractTest{
    public static void test(){
        System.out.println("This is AbstractTest-Son's static test!");
    }
}


//output
/*
This is AbstractTest's static test!
This is AbstractTest-Son's static test!
This is AbstractTest's static print!

This is AbstractTest's static print!
This is AbstractTest-Son's static test!
*/

The output results show that static methods in the parent class that are not overridden by subclasses can be accessed by subclasses and their objects, but for methods overridden by subclasses, subclasses and their objects can only call their own methods.

Why can't a subclass "override" the method of the parent class, but "method hiding"?

Because method Overriding is a feature of OOP language. In Java, it is a way to realize "runtime polymorphism!! in the case of a method with the same name with the same signature of a parent class and a child class, there won't be any run-time polymorphism.

four   Inheritance of abstract classes

If the abstract parent class cannot implement all abstract methods, the subclasses derived from the abstract parent class must also be declared abstract.

Abstract classes can define constructors that can be called by subclasses in constructors.

Subclasses of non abstract classes that can be declared abstract.

5, Contradiction between definition and abstraction

The last keyword can modify classes, methods, and variables.

The last modified class cannot be derived; the last modifier method prohibits overriding subclasses.

Thus, definition and abstraction are incompatible

Want to learn JAVA systematically, recommend JAVA300 set

Java300 set zero basic video tutorial for beginners

Topics: Java Programming