Class-The objective world is made up of many things that can be tangible (such as a car) or intangible (such as a meeting). The object is to map things in the objective world to object-oriented program design. By summarizing the objects of the same category, they form the class. Including properties and methods, construction properties: Record the characteristics of Things (state) method: Record the behavior of things;
For example: Students
Characteristics: Study number, gender, name, age
Behavior: study, eat
public class Student {
int id;
String name;
int age=20;
String sex;
public void Study (String St) {
System.out.println (name+ "studied" +st);
}
Public String Eat () {
return "OK";
}
Public Student () {}
Public Student (int id,string name,int age,string sex) {
This.id=id;
This.name=name;
This.age=age;
This.sex=sex;
}
Abstract method: Only defined, no concrete implementation of the method body
Abstract class: A class containing abstract methods,
Abstract classes have constructs, but cannot invoke constructs.
Abstract class has no object, abstract method cannot execute,
A reference to an abstract class that points to a non-abstract subclass object (polymorphic),
The invocation of an abstract method is actually a non-abstract method called after the rewrite,
Classes containing abstract methods must be abstract classes, when abstract methods are not necessarily abstract.
Abstract classes cannot be instantiated, that is, they cannot have their own objects
For example:
An account in a banking system, including a regular account and a current account, a regular account and a current account are basic accounts,
However, they have different methods of calculating interest and require different implementations to define basic accounts as abstract classes
Public abstract class Acount {
Basic Account
Private double total=2000;
Public acount () {}
Abstract methods have mandatory and normative
public abstract double Get ();
Abstract methods
public void Settotal (double total) {
This.total = total;
}
Public double gettotal () {
return total;
}
}
public class Hacount extends Acount {
Current Account
public double Get () {
Return this.gettotal () *0.02;
}
public static void Main (string[] args) {
Acount a=new Hacount ();
System.out.println (A.get ());
}
}
public class Staticacount extends acount{
// regular account
public double Get () {
Return this.gettotal () *0.03;
}
public static void Main (string[] args) {
Acount a=new Staticacount ();
Abstract class references to non-abstract subclass objects
System.out.println (A.get ());
The invocation of an abstract method is actually a non-abstract method that calls the subclass rewrite later
}
}
Declaring the interface using the keyword "interface"
Interfaces typically contain only a set of
Public abstract method (and must be a publicly-abstracted method, but the method definition can be omitted), so the interface can be considered a special abstract class, the interface can contain public static final data (generally do not write)
Java a class can only directly inherit (extends) Another class, does not allow a subclass to inherit multiple superclass, but allows a subclass to inherit a superclass and implement (implements) multiple interfaces
The inheritance relationship is not detected when the coercion type is converted to an interface, but if the type does not match, a type-forwarding exception is thrown at run time.
For example:
Gold:
Gold is a kind of currency
Gold is also a metal
Public interface Jinshu {//define gold as the interface of the metal
/* Private */int i=8; public static final constant
void Shine ();
}
Public interface Huobi {
Define gold as the currency's interface
void Change ();
}
public class Gold implements huobi,jinshu{
public void Shine () {
System.out.println ("The Gold shines! ");
}
public void Change () {
System.out.println ("Gold can be exchanged!") ");
}
public static void Main (string[] args) {
Jinshu js=new Gold ();
Js.shine ();
Huobi hb=new Gold ();
Hb.change ();
System.out.println (JS.I);
}
}
Classes, abstract classes, interfaces, can be an example of the following:
A company, a boss, a boss, a manager, an employee,
The class is the employee, the abstract class is the manager, the interface is the boss.
In the interface is to give a method,
But he didn't do it himself.
Like the boss said I wanted that file,
Give me a ticket.
I want that plan, and so on, are the following people to do. The boss deeds.
The method given by the abstract class,
Yes, he did it himself.
Some other people do.
Like the manager said I wanted that document,
The employee is going to send it to him, but he has to do something himself, like a plan for the boss. The manager said and did it again.
The general class gives the method, is everything to do, must realize.
The distinction and connection between abstract classes and interfaces:
Both are abstract classes and cannot be instantiated
Interface implementations of classes and abstract subclasses must implement the abstract method differences that have been declared:
A class can inherit only one abstract class, but a class may implement multiple interfaces
Interface emphasizes the realization of the function, while the abstract class emphasizes the affiliation
Interface methods are abstract methods, there is no method body, its implementation class must contain implementation methods to implement the interface, and the subclass of abstract class can have a selective implementation of the abstract method
Interface is completely abstract, the interface can only declare methods, and must be public abstract, not private and protected,interface no method body, and abstract class may have a non-abstract method,
The variables defined in the interface are public static final types and must have initial values that cannot be redefined or changed in the implementation class, and variables in the abstract class can be redefined and re-assigned.
Example:
Door action can be defined on and off two action, while the door can also realize the function of alarm
Abstract class door{
abstract void open ();
abstract void close ();
}
Interface alarm{
void Alarm ();
}
Java abstract classes and interfaces