We all know that classes with abstract methods are abstract classes. In other words, do abstract classes have abstract methods?
In fact, this problem is very clear. The class modified with abstract is an abstract class. It does not mean that abstract methods must exist in the abstract class. Even if all methods in a class have been implemented, abstract can also be used as an abstract class, abstract classes do not necessarily have abstract methods.
The following code contains an abstract class without abstract methods:
Abstract class democlass {<br/> Public void printmessage (string MSG) {<br/> system. Out. println (MSG); <br/>}< br/>}
After reading this code, I don't have to think about it. Can this class be instantiated? How can I call public methods in this class? Pass
Democlass d = new democlass ();
This is obviously not acceptable. abstract classes cannot be instantiated. Even an abstract class without abstract methods cannot be instantiated. Of course, you can also change the printmessage Method to the static type, so you can call it directly. The Code is as follows:
Package net. moon. insignificant. abstractclass; </P> <p> public class abstractdemo {<br/> Public static void main (string [] ARGs) throws instantiationexception, illegalaccessexception, classnotfoundexception {<br/> democlass. printmessage ("Hello, world"); <br/>}</P> <p> abstract class democlass {<br/> Public democlass () {}</P> <p> Public static void printmessage (string MSG) {<br/> system. out. println (MSG); <br/>}< br/>}
Another question is whether the static class can be modified. It will be analyzed in the next article.
Next article:
Java tip [002]: Can classes be modified statically?