Abstract class (abstract)
Overview abstract class : a class is abstract modification indicates that this class is an abstract class, define their own methods but not implementation, to achieve the offspring
Abstract methods : a method to be abstract modification indicates that this method is an abstract method, abstract method is no method body
Features :
1, there must be a class abstract methods abstract class, an abstract method of the abstract class does not necessarily have. Abstract class has abstract methods 0-N
2, abstract class can not be instantiated, meaning not new an abstract class
3, those who inherit the abstract class, must implement the abstract class method is an abstract class or their own
4, constructors and static methods can not be abstract
The following is an example :
Abstract class Person
. 1 public abstract class the Person { 2 // private attributes gender . 3 Private String Sex; . 4 // has parameters configured . 5 public the Person (String Sex) { . 6 the this .sex = Sex; . 7 } . 8 // abstract method . 9 public abstract void doSomething (); 10 . 11 }
Subclass Student
. 1 public class Student the extends the Person { 2 . 3 Private String name; // name . 4 Private int Age; // Age 5 // there is a reference configuration . 6 public Student ( int Age) { . 7 Super ( "M" ); . 8 the this .age = Age; . 9 } 10 public Student ( int Age, String name) { . 11 the this (Age); 12 is the this .name = name; 13 is } 14 public void doSomething () { 15 System.out.println ( "I am a student class implements the abstract method doSomething" ); 16 } . 17 18 is }
Subclass Teacher
. 1 public class Teacher the extends the Person { 2 public Teacher (String Sex) { . 3 Super (Sex); . 4 } . 5 . 6 public void doSomething () { . 7 System.out.println ( "I am a teacher class implements the abstract method doSomething" ); 8 } . 9 10 }
Test class Text
1 public class Text { 2 3 public static void main(String[] args) { 4 Person p=new Student(12); 5 p.doSomething(); 6 Person p1=new Teacher("男"); 7 p1.doSomething(); 8 } 9 }
Console output is:
I am a student class implements the abstract method doSomething
I am a teacher class implements the abstract method doSomething
Interface (interface)