Understanding of abstract classes in Java

Source: Internet
Author: User

----------------------------------------

Let me share with you today.

The understanding of image classes and abstract methods:

----------------------------------------

Abstract class:

You cannot use the new method for real

The class that is instantiated, i.e. there is no specific actual

Class of the example object, you can only pass

The abstract class derives a new subclass,

The object is then created by its subclasses;

Abstract method:

The abstract method is

Modified method, this method only

Declares the type of data returned by the

The name of the law and the required parameters,

There is no method body, which means pumping

The image method only needs to be declared when a

Method is an abstract method, the meaning

Taste This method must quilt class

method is overridden, otherwise its child

The method of the class is still abstract;

----------------------------------------

Features of abstract classes:

A: Abstract classes and abstract methods must be decorated with the abstract keyword;

B: Abstract classes do not necessarily have abstract methods, but classes with abstract methods must be defined as abstract classes;

C: Abstract classes cannot be instantiated directly; (Can be instantiated by a polymorphic way of subclasses (subclasses after overriding the method);

1. It is not specific;

2. Abstract class has a construction method for subclasses to access the initialization of the parent class data;

D: Subclasses of abstract classes;

1. If you do not want to rewrite the abstract method, the subclass must be an abstract class;

2. To be a concrete class, you must rewrite all the abstract methods;

----------------------------------------

An example of an abstract class is given below:

----------------------------------------

1  Public classAbstractdemo {2      Public Static voidMain (string[] args) {3         //Tree T = new tree ();//an abstract subclass cannot be instantiated; error!4Grass g =NewGrass ();//subclass instantiation of non-abstract classes5 G.grow ();6System.out.println ("-------------");7Plant p =NewGrass ();//Polymorphic Way8 P.grow ();9     }Ten } One Abstract classplant{ A      Public Abstract voidgrow (); - } -  the /* - * 1. Subclasses are abstract classes; -  */ - Abstract classTreeextendsplant{ + Tree () { -System.out.println ("Tree"); +     } A } at  - /* - * 2. Subclasses are not abstract classes, you must override all abstract methods; -  */ - classGrassextendsplant{ - Grass () { inSystem.out.println ("Grass"); -     } to      Public voidgrow () { +System.out.println ("Grass Growing"); -     } the}

----------------------------------------

Features of the members of the abstract class:

A: Member variables

1. It can be either a variable or a constant;

B: Construction method

1. Initialization for subclass access to the parent class data;

C: Member Method

1. It can be abstract or non-abstract;

Abstract class member method attributes:

A: Abstract method

1. What the subclass is required to do;

B: Non-abstract method

1. Subclass inherited things, improve code reusability;

----------------------------------------

The following gives the members of an abstract class

Instance:

----------------------------------------

1  Public classAbstractmember {2      Public Static voidMain (string[] args) {3         4Phone p =NewVivo ();//here is polymorphic; (used to instantiate abstract classes)5P.height = 1460;//14606 System.out.println (p.height);7 System.out.println (p.width);8System.out.println ("-----------");9P.call ();//Vivo CallTenP.method ();//Vivo Method One          A          -          -     } the } -  - Abstract classphone{ -      Public intHeight = 1600; +      Public Final intwidth = 960; -      PublicPhone () {} +      PublicPhone (String name,intcount) {} A     /*member Methods*/ at      Public Abstract voidCall ();//abstract member method; -      Public voidMethod () {//non-abstract member method; -SYSTEM.OUT.PRINTLN ("Phone Method")); -     } -      - } in //non-abstract subclass, overriding abstract methods of the abstract parent class: - classVivoextendsphone{ to      +      Public voidCall () { -System.out.println ("Vivo call"); the     } *      Public voidmethod () { $System.out.println ("Vivo Method"));Panax Notoginseng     } -      the}

----------------------------------------

The above is my reason for abstract class

Solution, thank you for reading;

Understanding of abstract classes in Java

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.