抽象类:包含有抽象方法的类都是抽象类
1. 有抽象方法的类一定是抽象类
2. 抽象类必须使用abstract关键字修饰
3. 抽象类不可以实例化
4. 抽象类中可以没有抽象方法,这样做只是不让该类被实例化
5. 抽象类被继承后必须全部实现其抽象方法, 或者依旧定义该类为抽象类
抽象方法:抽象方法是没有方法体的方法
package com.mjlf.myBatis.accessControl.animal;
/**
* Created by a123 on 16/12/17.
*/
public class AbstractTest extends AbstractClass {
public void test(){
System.out.println("yes");
}
public static void main(String[] args){
new AbstractTest().test();
}
}
abstract class AbstractClass{
public abstract void test();
public void test1(){
System.out.println("test1");
}
}
接口:
1. 接口用interface关键词修饰
2. 接口中的方法全是抽象方法
3. 接口中的方法默认全是public的, 因为他们必须全部被重写才能使用
4. 接口中的属性全是public static 修饰的,默认是隐藏这两个关键字的
5. 接口间使用继承, 接口与类见使用实现
6. 接口和继承的名字冲突陷阱
interface I1{
void f();
}
interface I2{
int f(int i);
}
interface I3{
int f();
}
class C {
public int f(){
return 1;
}
}
class C2 implements I1, I2{
public void f() {
}
public int f(int i) {
return 0;
}
}
class C3 extends C implements I2{
public int f(int i) {
return 0;
}
}
class C4 extends C implements I3{
}
class C5 extends C implements I1{
}//编译错误, 实现接口是不知道方法f()应该选择什么返回值类型
抽象类和接口被大量的运用在各种设计模式中,如工厂模式,适配模式,策略模式等中, 关于这些设计模式以后再细说!