1.abstract class 只能做为父类被extends,不能实例对象;
2.不一定有abstract function;
3.有abstract function的class一定是abstract class;
4.abstract function由子类实现;
public abstract class Employee
{
private int iAge;
private String strName;
public Employee(String strParamName, int iParamAge)
{
this.iAge = iParamAge;
this.strName = strParamName;
System.out.println("employee constructor\n");
}
public int getAge() {
return this.iAge;
}
public String getName() {
System.out.println("employee getName\n");
return this.strName;
}
public abstract double getPay();
}
public class Salary extends Employee {
private double dPay;
public Salary(String name, int age, double pay) {
super(name, age);
this.dPay = pay;
}
@Override
public String getName() {
System.out.println("Salary getName\n");
return super.getName();
}
public double getPay() {
System.out.println("Salary getPay\n");
return this.dPay;
}
}
public class HelloMain {
public static void main(String[] args) {
Salary as = new Salary("zhangsan", 30, 54321.2345);
as.getName();
as.getPay();
}
interface :
1.不能定义普通变量;
2.static、final变量可定义,并且final变量必须定义时赋值;
3.要实现方法体,方法前必须加default;
public interface Animal {
/* 接口中可定义static、final变量,不可定义普通变量 */
public static String name = "12121";
/* interface中 final变量必须赋值,实际使用场景少 */
public final int age = 1;
/* 方法体由继承的类实现 */
public int getXXX();
/* 加default可实现默认方法体 */
default public String getName(Employee xxx)
{
System.out.println("Animal getName");
return "abc";
}
}
public class Cat implements Animal {
/* 接口的方法实现 */
public int getXXX()
{
/* */
return 0;
}
/* default方法在interface中已实现,继续类可以重载 */
public String getName()
{
System.out.println("Cat getName");
return "a123";
}
}