Abstract classes and interfaces in Java

Source: Internet
Author: User

An abstract class uses the abstract keyword decoration, which is an abstract class.

The role of abstract classes:

1. In some cases, the parent class constrains which methods the subclass must contain, but does not know how subclasses implement these methods.

2, can be from a number of classes with the same characteristics of an abstract class, with this abstract class as a subclass template, to avoid the arbitrariness of sub-class design.

Rules for using abstract classes:

 Abstract Definition Abstraction object, 2, Abstract definition abstraction method, only declaration, do not need to implement; 3, class containing abstract method is abstract class, 4, abstract class can contain ordinary method, also can not abstract method; 5. Abstract classes cannot be created directly, you can define reference variables, and point to sub-class objects;

First, create an abstract class:

 Public Abstract class Telephone {    // phone public    abstractvoid call () ;     // Send SMS     Public Abstract void sendMessage ();}

Then create two subclasses to inherit the parent class, the subclass must contain the abstract method declared by the parent class, otherwise it will error:

 public  class  CellPhone extends   telephone{ public  void   call () {System.out.println ( "    Call ");  public  void   SendMessage () {System.out.println (" texting via keyboard "
 Public class extends telephone{    publicvoid call () {        System.out.println ("calls via voice");    }      Public void sendMessage ()    {        System.out.println ("Send text by voice");}    }

Then create a test class:

 Public classTest { Public Static voidMain (string[] args) {//Ordinary mobile phoneTelephone cellphone =NewCellPhone (); Cellphone.call (); //to make a phone call through the keyboardCellphone.sendmessage ();//Texting via keyboard//smart PhonesTelephone smartphone =NewSmartPhone (); Smartphone.call (); //Call by VoiceSmartphone.sendmessage ();//send text messages by voice    }}

Interfaces in Java are used to inherit, must be abstract, generally public, and internally as constants and abstract methods, a class can implement multiple interfaces.

Start by creating a new interface:

 Public Interface Iplaygame {    public String todo = "can play game";       Public void playGame ();}

Rewrite the smartphone class to:

 Public classSmartPhoneextendsTelephoneImplementsiplaygame{protectedString name = "Smart phone";  Public voidCall () {System.out.println ("Call by voice."); }     Public voidSendMessage () {System.out.println ("Texting by Voice"); } @Override Public voidPlayGame () {System.out.println (name+todo); }}

Instantiate and output:

New// smart phones can play games
In the process of using the interface, it is often used in conjunction with anonymous internal classes, which are internal classes that do not have names, and are used to focus on implementations rather than on the name of the implementation class.
//The first method of implementationIplaygame ip1 =NewIplaygame () {@Override Public voidPlayGame () {System.out.println ("Anonymous inner class method implementation interface" + TODO);//anonymous inner class method implementation interface can play the game}};ip1.playgame (); //Calling interface Methods//The second method of implementationNewIplaygame () {@Override Public voidPlayGame () {System.out.println ("Anonymous inner class method implementation interface" + TODO);//anonymous inner class method implementation interface can play the game}}.playgame ();

Abstract classes and interfaces in 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.