Java-Why use abstract classes (24th sets)

Source: Internet
Author: User

The following is a java4android video tutorial from marschen.

Why use abstract classes for 24th Sets
Main content.
Abstract classes express a concept.

// If a piece of code is incorrect in semantics, it should also be incorrect in syntax. this is a point that modern programming theory emphasizes, but there is no such language completely real // abstract class printer {void open () {system. out. println ("open");} void close () {system. out. println ("close");} void print () {}// abstract void print (); // The abstract function // has an error in semantics, let's make it syntactic incorrect. // syntax errors are easier to check .}

// This printer prints class hpprinter extends printer {void print () {system. Out. println ("Print with ink jet printer ");}}

// This printer is a dot matrix printer class canonprinter extends printer {void print () {system. Out. println ("Print with dot matrix printer ");}}

Class test {public static void main (string ARGs []) {printer p1 = new hpprinter (); // transforms upwards. The subclass object is assigned to the reference of the parent class. p1.open (); p1.print (); p1.close (); printer P2 = new canonprinter (); // transforms upwards. The subclass object is assigned to the reference of the parent class. p2.open (); p2.print (); p2.close ();}}

Why use abstract classes,

For example, if you do not use an abstract class,

Subclass may forget to implement the print () method of the parent class.

The error will be hidden.

Related Article

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.