Java Learning notes-abstract classes

Source: Internet
Author: User

-Why do you need abstract classes? How do I define an abstract class?

• is a template mode. Abstract classes provide a generic template for all subclasses, which can be extended on the basis of this template.

· by using abstract classes, you can avoid the arbitrariness of sub-class design. through abstract classes, we can strictly restrict the design of subclasses, so that subclasses are more common.

Points

√ Class-Smart definitions with abstract methods can abstract classes

√ Abstract classes cannot be instantiated, and abstract classes cannot be instantiated with new.

√ Abstract classes can contain properties, methods, and construction methods. However, the construction method cannot be used for new instances, only for the quilt class invocation.

√ Abstract classes can only be used for inheritance.

√ The abstract method must be implemented by the quilt class.

Eg:

Package Study;

Public abstract class Testabstract {
public abstract void run ();
public abstract void Eat ();
public void dosomething () {
System.out.println ("Do Something Good");
Eat ();
}

public static void Main (string[] args) {
Testabstract thing = new Catanimal ();
Thing.run ();
}
}
Class Catanimal extends Testabstract {
public void Run () {
System.out.println ("I can walk the cat step");
This.dosomething ();
}
public void Eat () {
System.out.println ("I can eat cat food");
}
}

Java Learning notes-abstract classes

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.