Java _ abstract class, java Abstract class

Source: Internet
Author: User
Tags abstract definition define abstract

Java _ abstract class, java Abstract class

Abstract class
1. syntax definition
The abstract keyword is used before the abstract class.
2. Application scenarios
(1) In some cases, a parent class only knows how its subclass should contain, but cannot accurately know how these subclasses implement these methods.

(2) Abstract An abstract class from multiple classes with the same features and use this abstract class as the template of the subclass, thus avoiding the randomness of the subclass design.

3. Role

Limits that subclass must implement certain methods, but does not focus on implementation details

(Main purpose: do not focus on the implementation of sub-classes. But which subclasses must be bound)

4. applicable methods (Use Rules)

(1) abstract definition abstract class

(2) abstract: define abstract methods, declare only, and do not need to be implemented

(3) classes that contain abstract methods are all abstract classes.

(4) An abstract class can contain common methods or abstract methods.

(5) abstract classes cannot be directly created. You can define referenced variables.

** The abstract class does not have a method body, but ends with a semicolon.

*** Tips

"Syso + alt +/" System. out. println (""); shortcut key

When creating a subclass, select Browse and select the parent class name.

When creating a subclass, select abstract to generate an abstract class.

When you create a test class (Initail class main method), check public static void main (String [] args) to generate it directly:

Public static void main (String [] args ){

// TODO Auto-generated method stub


public abstract class Telphone {    public abstract void call();    public abstract void message();}
Public class CellPhone extends Telphone {@ Override public void call () {System. out. println ("call via keyboard") ;}@ Override public void message () {// TODO Auto-generated method stub System. out. println ("call via keyboard ");}}
Class SmartPhone extends Telphone {@ Override public void call () {// TODO Auto-generated method stub System. out. println ("call via voice") ;}@ Override public void message () {// TODO Auto-generated method stub System. out. println ("text message via voice ");}}
public class Initail {    public static void main(String[] args) {        // TODO Auto-generated method stub        Telphone tel1=new CellPhone();;        tel1.message();        Telphone tel2=new SmartPhone();;        tel2.message();    }}
@ Override is the automatically generated code. What does it mean and what does it do?
// TODO Auto-generated method stubFor automatically generated code, what does it mean?



Existing shape graphics class, which has two graphics classes, uses the Rectangle and the Circle subclass to calculate the circumference and area of the Circle.


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: 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.