0910 some basic concepts of Java abstract classes __java

Source: Internet
Author: User
Some basic concepts of abstract classes Directory

Use [TOC] to generate a table of contents:

Some basic concepts of abstract class catalog abstract conceptual code block

The concept of abstraction

Abstract method: In Java, a method without a method body is defined as an abstract method.
Abstract class: If there is an abstract method in a class, the class is defined as an abstract class.
1> Abstract class overview:
    animals should not be defined as specific things, and animals eat, sleep, etc. should not be specific.
    we call a function that is not specific as abstract, and if there is an abstract function in a class, the class must be an abstract class.  
2> The characteristics of abstract classes:
    A: Abstract classes and abstract methods must be decorated with the abstract keyword
    B: there is not necessarily an abstract method in an abstract class, but a class with an abstract method must be defined as an abstract class
    C: An abstract class cannot be instantiated
        because it is not concrete.
        Abstract classes have construction methods, but cannot be instantiated? What
        is the role of the construction method? Initialization of a subclass that accesses the parent class
    D: Abstract Subclass
        A: If you do not want to override an abstract method, the subclass is an abstract class.
        B: Rewrite all the abstract methods, this time the subclass is a concrete class. The instantiation of an

    abstract class is actually implemented by a specific subclass. is the polymorphic way.
        Animal a = new Cat ();
3> the members of an abstract class:
    member variables: can be either a variable or a constant.
    Construction Method: there is.
        used to initialize the subclass to access the data of the parent class.
    member methods: can be abstract or non-abstract.

4> The member method attributes of an abstract class:
    A: Abstract methods < forcing the subclass to do things >.
    B: Non-abstract methods < subclass inherited things, improve code reuse >
code block

code block syntax follows standard markdown code, such as:

abstract class animal{int age=20;
    final int age2=40;
    Public Animal () {} public Animal (String Name,int age) {} public abstract void Show ();
    public void Method () {System.out.println (' Animal method ');
    Class Dog extends animal{public void Show () {System.out.println (""); } public class AbstactDemo2 {public static void main (string[] args) {//TODO automatically generated method stub Animal A=ne
        W Dog ();
a.age=10;
        a.age3=30;
        System.out.println (A.age);
    System.out.println (A.age2); }
}
Related Article

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.