Abstract class
1, syntax definition
Abstract keyword is used to decorate the class before it is abstract class
2. Application Scenarios
(1) In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods.
(2) abstract an abstract class from multiple classes with the same characteristics, using this abstract class as a template for subclasses, thus avoiding the arbitrariness of sub-class design
3. Role
Restriction subclasses must implement some methods, but do not care about implementation details
(Main purpose: Do not focus on the implementation of subclasses.) But what subclasses must be constrained to constrain subclasses)
4. Applicable methods (usage rules)
(1) abstract definition Abstraction class
(2) Abstract definition abstraction method, declaration only, do not need to implement
(3) classes that contain abstract methods are abstract classes
(4) An abstract class can contain ordinary methods, or there can be no abstract method
(5) Abstract classes cannot be created directly, you can define reference variables
* * Abstract class does not have a method body, but ends with a semicolon
Little Tricks
"syso+alt+/" System.out.println (""); shortcut keys
When creating a subclass, tick browse to select the parent class name
When creating a subclass, tick abstract to generate abstract classes directly
When creating a test class (Initail class Main method), tick the public static void main (string[] args) to generate 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 (" Phone by keypad "); @Override public void message () { // TODO auto- Generated method stub System.out.println ("Call through the keyboard"
class SmartPhone extends Telphone {@Override public void call () { // T ODO auto-generated Method Stub System.out.println ("Can call by voice" public void message () { // TODO auto- Generated method stub System.out.println ("can be sent by voice message"
public class Initail { public static void main (string[] args) { // TODO auto-generated method stub telphone tel1=new CellPhone (); Tel1.call (); Tel1.message (); Telphone tel2 =new SmartPhone (); Tel2.call (); Tel2.message (); }}
Questions:
@Override for the automatically generated code, what does that mean, what does it do?
For automatically generated code, what does that mean, what does it do?
Practice:
Existing shape Graphics class, he has two graphic classes, with rectangle rectangle and Circle Circle sub-class, to find the circumference and area of the circle.
Java_ Abstract class