Java Basic Learning--9, abstract class

Source: Internet
Author: User

I. Concepts and definitions of abstract classes

In object-oriented concepts, all objects are depicted by classes, but conversely, not all classes are used to depict objects , and if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Abstract class Format: modifier Abstract class name

Abstract classes are often used to characterize the abstract concepts of analysis and design of problem areas, and are abstractions of a series of concrete concepts that look different, but are essentially the same. For example, in the analysis and design process of a graphical editing software, it is found that there are some specific concepts of circle and triangle in the problem domain, they are different, but they all belong to the concept of shape, The concept of shape is not directly present in the problem domain, 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. Can't instantiate that specific how to use it. This is illustrated in a simple example. Students Wang Xiaoming to Shanghai to learn Java this is what is required through the above content we know that object-oriented body is Students (object-oriented development is primarily to find objects in requirements) method is Learning   method implementation is ( student +name (Wang Xiaoming) + "Go" +address (Shanghai) + "Learn Java")
 Public Abstract class Person {String name= ""; // that is, the person must have a name.  int age=0; // Age Person (String name,Int.  ) {this. name=name; this. age= age;} abstract void  Study ();}

Note: The red one above is the abstract method. Some people will ask, how this method has no method body.

Abstract method: A method that does not have a method body in a class is an abstract method.

Abstract methods in abstract classes must be implemented!
If a subclass does not implement an abstract method in the parent class, the subclass becomes an abstract class!
The normal method in an abstract class can be implemented without having to implement it.

In the above abstract class we define an abstract method of learning

Below we define the student this subject class. We have one more address definition in student class.

 Public class extends Person {String address= ""; // Add one more attribute address Student (String name,int  age,string address) {  Super(Name,age); // to call the constructor of a super class this. address=address;} @Overridevoid Study () {// implemented abstract method System.out.print ("Student" +name+ "going to" +address+ " Learn Java ");}}  

Let's write a test class and test it:

 Public class Test {publicstaticvoid  main (string[] args) {person PerA=new Student ("Wang Xiaoming", 38, "Shanghai");p era.study ();}}

results after the run: students Wang Xiaoming to Shanghai to learn Java

Java Basic Learning--9, abstract class

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.