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.call(); tel1.message(); Telphone tel2=new SmartPhone(); tel2.call(); tel2.message(); }}
Question:
@ 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?
Exercise:
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.