java学习笔记 --- 抽象类

一、抽象类  

 (1)定义:  把多个共性的东西提取到一个类中,这是继承的做法。
      但是呢,这多个共性的东西,在有些时候,方法声明一样,但是方法体。
      也就是说,方法声明一样,但是每个具体的对象在具体实现的时候内容不一样。
          所以,我们在定义这些共性的方法的时候,就不能给出具体的方法体。
               而一个没有具体的方法体的方法是抽象的方法。
          在一个类中如果有抽象方法,该类必须定义为抽象类。

  (2)抽象类特点:

    1、抽象类和抽象方法都需要被abstract修饰。抽象方法一定要定义在抽象类中。

    2、抽象类不可以创建实例,原因:调用抽象方法没有意义。

    3、只有覆盖了抽象类中所有的抽象方法后,其子类才可以实例化。否则该子类还是一个抽象类。

    4、抽象类中不一定有抽象方法(可以有具体方法),但是有抽象方法的类一定是抽象类

    5、抽象类的子类
         a:是一个抽象类。
         b:是一个具体类。这个类必须重写抽象类中的所有抽象方法。

    6、抽象类的实例化其实是靠具体的子类实现的。
         Animal a = new Cat();

//abstract class Animal //抽象类的声明格式
abstract class Animal {
    //抽象方法
    //public abstract void eat(){} //空方法体,这个会报错。抽象方法不能有主体
    public abstract void eat();
    
    public Animal(){}
}

//子类是抽象类
abstract class Dog extends Animal {}

//子类是具体类,重写抽象方法
class Cat extends Animal {
    public void eat() {
        System.out.println("猫吃鱼");
    }
}

class AbstractDemo {
    public static void main(String[] args) {
        //创建对象
        //Animal是抽象的; 无法实例化
        //Animal a = new Animal();
        //通过子类方式实现
        Animal a = new Cat();
        a.eat();
    }
}

 (3)抽象类的注意事项:   

  1、抽象类一定是个父类? 

        是的,因为不断抽取而来的。

  2、抽象类是否有构造函数?

         有,虽然不能给自己的对象初始化,但是可以给自己的子类对象初始化。

         抽象类和一般类的异同点:

         相同:

              1、它们都是用来描述事物的。

              2、它们之中都可以定义属性和行为。

         不同:

              1、一般类可以具体的描述事物。抽象类描述事物的信息不具体

              2、抽象类中可以多定义一个成员:抽象函数。

              3、一般类可以创建对象,而抽象类不能创建对象。

  3、抽象类中是否可以不定义抽象方法。

         是可以的,那这个抽象类的存在到底有什么意义呢?仅仅是不让该类创建对象。

  4、抽象关键字abstract不可以和哪些关键字共存?      

             1、final:fianl修饰的类是无法被继承的,而abstract修饰的类一定要有子类。            

         final修饰的方法无法被覆盖,但是abstract修饰的方法必须要被子类去实现的。

     2、static:静态修饰的方法属于类的,它存在与静态区中,和对象就没关系了。而抽象方法没有方法体,使用类名调用它没有任何意义。

     3、private:私有的方法子类是无法继承到的,也不存在覆盖,而abstract和private一起使用修饰方法,abstract既要子类去实现这个方法,而private修饰子类根本无法得到父类这个方法。互相矛盾。

二、接口

  当抽象类中的所有方法都是抽象方法时,这时就可以用另外一种机制来体现:接口。接口相当于抽象方法的一个集合。

    

interface class Demo
{
    abstract void show1();
    abstract void show2();
}

  (1)、接口的特点

     A:接口用关键字interface修饰
         interface 接口名 {}
     B:类实现接口用implements修饰
       class 类名 implements 接口名 {}
     C:接口不能实例化
     D:接口的实现类
         a:是一个抽象类。
         b:是一个具体类,这个类必须重写接口中的所有抽象方法。

//定义动物培训接口
interface AnimalTrain {
    public abstract void jump();
}

//抽象类实现接口
abstract class Dog implements AnimalTrain {
}

//具体类实现接口
class Cat implements AnimalTrain {
    public void jump() {
        System.out.println("猫可以跳高了");
    }
}

class InterfaceDemo {
    public static void main(String[] args) {
        //AnimalTrain是抽象的; 无法实例化
        //AnimalTrain at = new AnimalTrain();
        //at.jump();
        
        AnimalTrain at = new Cat();
        at.jump();
    }
}

  (2)接口中成员的特点

    A:成员变量
         只能是常量
         默认修饰符:public static final
     B:构造方法
         没有构造方法
     C:成员方法
         只能是抽象的
         默认修饰符:public abstract

    D:接口中的成员都是公共的

interface Inter {
    public int num = 10;//默认为常量,及时没有写static final
    public final int num2 = 20;//
    public static final int num3 = 30;
    
    //错误: 需要<标识符>,没有构造方法
    //public Inter() {}
    
    //接口方法不能带有主体
    //public void show() {}

    //abstract void show(); //默认public
    public void show(); //默认abstract
}

//接口名+Impl这种格式是接口的实现类格式
/*
class InterImpl implements Inter {
    public InterImpl() {
        super();
    }
}
*/

class InterImpl extends Object implements Inter {
    public InterImpl() {
        super();
    }
    
    public void show() {}
}

//测试类
class InterfaceDemo2 {
    public static void main(String[] args) {
        //创建对象
        Inter i = new InterImpl();
        System.out.println(i.num);
        System.out.println(i.num2);
        //i.num = 100;
        //i.num2 = 200;
        //System.out.println(i.num); //无法为最终变量num分配值
        //System.out.println(i.num2);//无法为最终变量num2分配值
        System.out.println(Inter.num);
        System.out.println(Inter.num2);
        System.out.println("--------------");
    }
}

  (3)类与类,类与接口,接口与接口的关系
      A:类与类
         继承关系,只能单继承,可以多层继承
      B:类与接口
         实现关系,可以单实现,也可以多实现。
         还可以在继承一个类的同时,实现多个接口
      C:接口与接口
         继承关系,可以单继承,也可以多继承

      

interface Father {
    public abstract void show();
}

interface Mother {
    public abstract void show2();
}

interface Sister extends Father,Mother {

}

//class Son implements Father,Mother //多实现
class Son extends Object implements Father,Mother {
    public void show() {
        System.out.println("show son");
    }
    
    public void show2() {
        System.out.println("show2 son");
    }
}

class InterfaceDemo3 {
    public static void main(String[] args) {
        //创建对象
        Father f = new Son();
        f.show();
        //f.show2(); //报错
    
        Mother m = new Son();
        //m.show(); //报错
        m.show2();
    }
}

 (4)接口和抽象类的区别
    A:成员区别
       抽象类:
          成员变量:可以变量,也可以常量
          构造方法:有
          成员方法:可以抽象,也可以非抽象(具体的方法)
       接口:
          成员变量:只可以常量

          构造方法:没有
          成员方法:只可以抽象
  
    B:关系区别
       类与类
          继承,单继承
       类与接口
          实现,单实现,多实现
       接口与接口
          继承,单继承,多继承
  
    C:设计理念区别
       抽象类 被继承体现的是:”is a”的关系。抽象类中定义的是该继承体系的共性功能。
       接口 被实现体现的是:”like a”的关系。接口中定义的是该继承体系的扩展功能。

补充:

    抽象类和接口的相同点:          

            都位于继承的顶端,用于被其他实现或继承;

            都不能实例化;

            都包含抽象方法,其子类都必须覆写这些抽象方法;

    

    二者的选用:

          优先选用接口,尽量少用抽象类;

          需要定义子类的行为,又要为子类提供共性功能时才选用抽象类;

 

    

原文地址:https://www.cnblogs.com/flei/p/6605665.html