Summary: class, interface, abstract class

1, classes and relations: the extends Inheritance / single inheritance / single inheritance
-- 继承的意义:为了提高代码的复用性,减少了代码的编写提高开发效率。

-- 方法重写的意义:在不修改父类源码的前提下,在子类中重写业务,从此使用的就是重写后的功能。

-- 要求子类的方法声明和父类一样,只要改方法体。

-- 有了继承有了重写就产生了多态,多态的意义:为了统一程序的调用标准,标准就是父类。

-- 多态 也就是向上转型/向上造型。

-- 向下造型的意义:很少用,相当于想要使用子类的特殊功能,还不如直接创建子类对象简单。

-- class A extends B 

-- 其中,A和B都是类,A是子类,B是父类,A就拥有了B的所有功能(除了私有的和构造方法) 

-- 其他知识点:this 和super  ,构造方法,各种代码块...
2, the relationship between classes and interfaces: the implements realize / implement Single / Multi implemented
-- class A implements B,C

-- 其中,A是实现类,B和C是接口

-- 要求A 可以把 B 和C 接口里的所有 抽象方法 都重写掉,否则 A 就是抽象类

-- 接口不能创建对象

-- 接口里没有构造方法,接口里都是常量,接口里都是抽象方法
3, interface and its relationship: inheritance extends / single inheritance / multiple inheritance
-- 接口的多继承的关系,打破了java单继承的局限性

-- interface A  extends  B,C

-- 其中,A B C 都是接口,A是子接口,同时拥有B和C接口里的所有功能

-- class AImpl implements  A

-- 要求AImpl需要重写A接口里的所有方法(是包含B和C接口的所有方法),否则就是抽象类
4, the difference between interfaces and abstract classes! ! !
-- 相同点:都是抽象层,都不能实例化                       

-- 不同点:

   -- 1、抽象类用abstract关键字描述,接口用interface

   -- 2、子类和抽象类之间是extends关系,实现类和接口之间是implements关系

   -- 3、抽象类中 可以  有构造方法 ,接口里 不能 出现 构造方法

   -- 4、抽象类里可以有 变量,接口里没有变量全都是静态的常量

   -- 5、接口里定义常量的语法:public static final String NAME="jack",会为变量自动拼接public static final

   -- 6、抽象类里 可以有普通方法  也可以有 抽象方法,接口都是抽象方法

   -- 7、抽象类和子类之间是继承关系,而且java中,只支持单继承

   -- 8、接口突破了java单继承的局限性,因为接口可以多继承也可以多实现,甚至可以继承的同时多实现

   -- 9、接口的复杂用法

       -- 多继承: interface A  extends  B,C  其中A是子接口,同时拥有自己的和BC的功能

       -- 多实现: class AImpl implements M,N,O,P 其中AImpl是实现类,需要同时重写MNOP的所有抽象方法,否则就是一个抽象类

       -- 继承的同时多实现: class AImpl extends Object implements M,N 一定是先继承后实现
Published 36 original articles · won praise 13 · views 1071

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104761716