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