Java tip [001]: Must abstract classes have abstract methods?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.