实例化:指在面向对象的编程中,把用类创建对象的过程称为实例化。是将一个抽象的概念类,具体到该类实物的过程。实例化过程中一般由类名 对象名 = new 类名(参数1,参数2...参数n)构成!
多数语言中,实例化一个对象就是为对象开辟内存空间,或者是不用声明,直接使用new 构造函数名,建立一个临时对象!
接口和类:在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式,从而来继承接口的抽象方法。
interface :
- Interfaces specify what a class must do and not how. It is the blueprint of the class.
- An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
- If a class implements an interface and does not provide method bodies for all functions specified in the interface, then class must be declared abstract.
- A Java library example is, Comparator Interface. If a class implements this interface, then it can be used to sort a collection
abstract class:
If a class includes abstract methods, then the class itself must be declared abstract
, as in:
public abstract class GraphicObject { // declare fields // declare nonabstract methods abstract void draw(); }
https://www.geeksforgeeks.org/differences-between-interface-and-class-in-java/
// Java program to demonstrate
// working of interface.
import java.io.*;
// A simple interface
interface in1 {
// public, static and final
final int a = 10;
// public and abstract
void display();
}
// A class that implements the interface.
class testClass implements in1 {
// Implementing the capabilities of
// interface.
public void display()
{
System.out.println("Geek");
}
// Driver Code
public static void main(String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
output: Geek 10