Abstract classes in java, java Abstract classes

Source: Internet
Author: User

Abstract classes in java, java Abstract classes

Abstract class: abstract is used. If a class is modified by abstract, it is called abstract class.
If the abstract modifier method is used, this method is called an abstract method.
If the abstract modifier class is used, this class is called an abstract class.
If a class inherits the abstract class, you must implement the abstract methods in the abstract class.

Use of abstract keywords:

Method body: the content in braces in the method is the method body.
1. If a method does not have a method body, the method must be modified using abstract.
2. If an abstract method exists in a class, this class must be modified using abstract as an abstract class.
3. If a non-abstract class inherits this abstract class, all abstract methods must be overwritten in this non-abstract class.
4. An abstract class can contain non-abstract methods.
5. A constructor can exist in an abstract class. The function is to allow the subclass to initialize the variables in the parent class.
6. the abstract class cannot create objects.
Cause: if an object is created, the object can call the abstract method, which makes no sense to call the abstract method.

7. abstract classes do not contain abstract methods.

Use Cases of abstract keywords:
When we describe a thing, we find that this thing does have some behavior, but this behavior is not specific. At this time, we can extract this behavior to declare an unimplemented behavior, this kind of behavior is called abstract behavior, so it is necessary to use abstract classes.

// The class modified by the abstract keyword is called an abstract class.
Abstract class Animal
{
String name;
String color;
Abstract public void run (); // The abstract method has no subject and no specific implementation of the method.

Public void eat (){
System. out. println ("animals are eating ");
}

}

// A non-abstract class inherits the abstract class and must implement all methods in the abstract class.
Class Dog extends Animal
{

// You need to override the run method in the parent class.

Public void run (){

System. out. println (name + "very fast ");
}

}

Class Fish extends Animal
{
// Rewrite method: the run method is not required to be rewritten.

// It should be rewritten, but I can not rewrite it. In this way, problems may occur.

// Think of a method that requires the subclass to override a method in the parent class.

Public void run (){
System. out. println (name + "swim fast ");
}
}

Class Demo8
{
Public static void main (String [] args)
{

Dog d = new Dog ();
D. name = "North America ";
D. run ();
D. eat ();
Fish fish = new Fish ();
Fish. name = "whale ";
Fish. run ();
Fish. eat ();
}
}

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.