Java object-oriented-abstract class

Source: Internet
Author: User

Java object-oriented- abstract class

Definition: In Java, a class containing abstract methods is called an abstract class, nor can it generate objects;

Note the point :

1, the class containing an abstract method is an abstract class;

2, abstract and abstract methods are to be modified with abstract keywords;

3, the abstract method only needs to declare but does not need to implement;

4, abstract classes must inherit from the Quilt class, (if not abstract classes) must override all abstract methods in the abstract parent class;

5, abstract classes cannot be instantiated;

Let's start by defining an abstract class:

1  PackageCom.java1234.chap03.sec11;2  3 /**4 * Define an abstract class people5  * @authorUser6  *7  */8  Public Abstract classpeople {9  Ten     PrivateString name; One   A      PublicString GetName () { -         returnname; -     } the   -      Public voidsetName (String name) { -          This. Name =name; -     } +       -      Public voidsay () { +System.out.println ("My name is:" + This. GetName ()); A     } at       -     /** - * Define an abstract method occupation let subclass to concrete realization -      */ -      Public Abstract voidprofession (); -       in}

Abstract class, define a property name and method say () and then define an abstract method profession the implementation of the subclass to achieve;

Let's do a test class:

I'm trying to instantiate the following class of people:

We will see compile error, can not instantiate people class;

We define a student class, and a teacher class:

  1  package   Com.java1234.chap03.sec11;   2   3  public  class  Student extends   people{  4   5   @Override   6  public  void   profession () {  7  System.out.println (" Occupation is: Student "  8  }   9  10 } 
  1  package   Com.java1234.chap03.sec11;   2   3  public  class  Teacher extends   people{  4   5   @Override   6  public  void   profession () {  7  System.out.println (" Occupation is: Teacher "  8  }   9  10 } 

Let's do a test class:

1  PackageCom.java1234.chap03.sec11;2  3  Public classTest {4  5      Public Static voidMain (string[] args) {6          7Student student=NewStudent ();8 student.profession ();9          TenTeacher teacher=NewTeacher (); One teacher.profession (); A           -     } -}

Run output:

Occupation is: Student

Occupation is: Teacher

Java object-oriented-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.