Abstract class for Java

Source: Internet
Author: User
Tags define abstract

In the real world, people characterize the world as an abstract class of things that have the same characteristics in many classes in the real universe. For example, fruit is a general term for many plant fruits, we can define an Apple class, define a watermelon class, can instantiate an Apple object, can instantiate a watermelon object, we can also define a fruit class, but you cannot instantiate a fruit object, because the fruit instance must be a specific plant fruit, this time , fruit class is an abstract class, the fruit class can be inherited by the Apple class extension, can be extended by the watermelon class, which is the only use of fruit class.

Another example, if the development of a graphical editing software, you will find that the problem domain exists in the circle, the triangle of some specific concepts, they are different, but they are all part of the concept of shape, shape this concept in the problem domain is not exist, it is an abstract concept. Abstract classes used to characterize abstract concepts cannot be instantiated because abstract concepts have no corresponding specific concepts in the problem domain.

Abstract class definition form:

The general format is as follows:

Abstract class class Name {

Class Body

}

Description

An abstract class is a class that cannot instantiate an object directly. That is, an abstract class cannot use the new operator to create an object.

Abstract classes generally include one or several abstract methods. The so-called abstract method needs to be modified in the abstract modifier, the abstract method only the declaration part of the method, there is no specific method implementation part. Subclasses of an abstract class must override the abstract method of the parent class in order to instantiate it, otherwise the subclass is an abstract class.

Abstract methods are not necessarily included in an abstract class, but classes that contain abstract methods must be described as abstract classes.

Example: The dog class inherits from the abstract class animal.

Abstract class animal{

String name;

Animal (Stringname) {//non-abstract method

This.name=name;

}

void GetName () {//non-abstract method

System.out.println ("Animal's name is" +name);

}

abstract void Move (); Abstract methods, modified with an abstract

}

Class Dog extends animal{

Intage

Dog (String Name,int age) {

Super (name);

This.age=age;

}

void Move () {

System.out.println ("Dog is running!");

}

void Getage () {

System.out.println ("Dog is" +age+ "years");

}

}

public class abstractdemo{

public static void Main (String args[]) {

Dogd=new Dog ("Wangwang", 5);

D.move ();

D.getname ();

D.getage ();

}

}

Program Run Result:

Dog is running!

Animal ' s name is Wangwang

Dog is 5 years old

For example, an abstract vehicle class car with startup (startup) method, each of its specific subclasses must implement its own specific startup (startup) method that belongs to a particular type of vehicle.

Define abstract class Car

Abstract class Car {

Defining abstract methods Startup

Public abstract void startUp ();

}

Define the abstract class Audi and inherit the class from car

Abstract class Audi extends car{

Defining abstract Methods Turbo

Public abstract void Turbo ();

}

Define non-abstract classes audi_a6 inherit from Audi

class Audi_a6 extends audi{

Implementing the Startup method

Public void startUp () {

System. out. println ("Call the start function of the Audi A6!!!" ");

}

Implementing the Turbo Method

Public void Turbo () {

System. out. println ("Call the acceleration function of the Audi A6!!!" ");

}

}

Define non-abstract classes audi_a8 inherit from Audi

class Audi_a8 extends audi{

Implementing the Startup method

Public void startUp () {

System. out. println ("Call the start function of the Audi A8!!!" ");

}

Implementing the Turbo Method

Public void Turbo () {

System. out. println ("Call the acceleration function of the Audi A8!!!" ");

}

}

Public class abstractclassdemo{

Public static void main (string[] args) {

Create a Audi_a6 object and make the class reference A6 point to the object

Audi_a6 a6=New audi_a6 ();

Calling methods in the Audi_a6 object

A6.startup ();

A6.turbo ();

Create a Audi_a8 object and make the class reference A8 point to the object

Audi_a8 a8=New audi_a8 ();

Calling methods in the Audi_a8 object

A8.startup ();

A8.turbo ();

}

}

Program execution Results:

The launch function of the Audi A6 is called!!!

The acceleration function of the Audi A6 is called!!!

The launch function of the Audi A8 is called!!!

The acceleration function of the Audi A8 is called!!!

Abstract class for 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.