Java Abstract class concept and usage instance analysis, java instance analysis

Source: Internet
Author: User

Java Abstract class concept and usage instance analysis, java instance analysis

This example describes the concept and usage of a Java Abstract class. We will share this with you for your reference. The details are as follows:

Abstract:Is a rough description of a thing.

Abstract method:In the abstract-modified method, this method only declares the returned data type, method name and required parameters, and there is no function body. Such as abstract void study ();

Abstract class features:

1. abstract classes do not necessarily contain abstract methods, but abstract methods must be in abstract classes.
2. abstract classes are not actually functional and can only be used to derive child classes.
3. abstract classes can contain constructor, but constructor cannot be declared as abstract. The member methods in abstract classes include general methods and abstract methods.
4. abstract methods and abstract classes must be modified by abstract keywords.
5. the abstract class cannot use new to create an object. It must be called after all the abstract methods are rewritten by the subclass.
6. To use abstract methods in abstract classes, subclasses must be rewritten to all abstract methods before calling them. If the subclass only involves some abstract methods, the subclass is still an abstract class.
7. The abstract method must be public or protected (because if it is private, it cannot be inherited by the quilt class, And the subclass cannot implement this method)

Abstract class Student // abstract class {private String name; private int age; abstract void study (); // abstract Method Student (String name, int age) {this. name = name; this. age = age ;}} class GaoZhongStudent extends Student {private String xuehao; GaoZhongStudent (String name, int age, String xuehao) {super (name, age ); // execute the parent class constructor this. xuehao = xuehao;} public void study () {System. out. println ("study gaozhong") ;}} class ChuZhongStudent extends Student {ChuZhongStudent (String name, int age) {super (name, age);} public void study () {System. out. println ("study chuzhong") ;}} class abstractDemo {public static void main (String [] args) {ChuZhongStudent p1 = new ChuZhongStudent ("zhangsan", 20 ); p1.study (); GaoZhongStudent p2 = new GaoZhongStudent ("lisi", 20, "yao1_001"); p2.study ();}}

For more java-related content, refer to this topic: introduction to Java Object-Oriented Programming and advanced tutorials, Java data structure and algorithm tutorials, Java DOM node skills summaries, Java files and directories, and java cache operation tips

I hope this article will help you with java programming.

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.