The abstract class of Java Learning 4

Source: Internet
Author: User

In the process of programming for the parent class, the abstract parent class has a generalized trait. The parent class function is just an abstraction, just to unify the interface service in object-oriented programming.

Example

Sometimes the parent class defines some behavior that cannot be implemented:

public void call (people people) {

Peole.speak ();

}

This function is designed for the parent class people and is suitable for all human use, but people subclasses are not defined and speak in people cannot be implemented specifically. This code is a common piece of code, and you cannot wait for all subclass designs to complete before you design the call function. So the system needs to provide a mechanism for defining a function that is not implemented, called an abstract function.

Public abstract class people{//Abstract classes

public abstract void speak (); Abstract functions

}

Abstract functions use the abstract keyword modifier to express an incomplete concept.

Only after the subclass implements the abstract function, does it constitute a complete, concrete type before it can be used.

Public class Chinese extends people{

public void Speak () {

System.out.println ("Chinese language");

}

public static void Main (string[] args) {

People people = new Chinese ();

People.speak ();

}

}

Attention: in an abstract class, there is at least more than one abstract function, which can be mixed to define a specific function.

If there is no abstract element in a class, it can also be defined as an abstract type. This means that the designer does not want the user to instantiate it directly, only to implement the specific application through subclasses.

For implementation functions that are not fully deterministic by the parent class, you can use an empty implementation body. However, it is not recommended to do so because the execution result after the call does nothing and the customer may be confused.

Use the throw exception to indicate that this method is not implemented:

public class people{

public void Speak () throws illegalaccessexception{

throw new Illegalaccessexception ("Not implemented, inaccessible");

}

}

When the user calls this function, the system throws an exception, which is more friendly than an empty implementation.

Summarize:

(1) Abstract classes can have construction methods, ordinary member variables, non-abstract ordinary methods, static methods, static member variables.

(2) The access type of an abstract method in an abstract class can be public, protected, and the default type. An abstract class can inherit only one class.

Example

Describe a simple tree-like structure:

Import java.util.ArrayList;

Import java.util.List;

Public abstract class node{

protected String name;

protected int data;

Public abstract node Add (node child);

}

Class Concretenode extends node{

Private List<node>children = new arraylist<node> ();

Public Concretenode (String name,int data) {

THIS.name = name;

This.data = data;

}

Public node Add (node child) {

Children.add (child);

return this;

}

public static void Main (stringp[] args) {

Node root = new Concretenode ("root", 0);

Node first = new Concretenode ("first", 0);

Root.add (first). Add (New Concretenode ("second", 2));

First.add (New Concretenode ("Third", 3)). Add (New Concretenode ("fourth", 4));

}

}

The abstract class of Java Learning 4

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.