Java 中extends与implements使用方法

初学Java语言, 代码中的extends和implements让我感到很迷惑,现在终于弄明白它们之间的区别和用法了。

  1. //定义一个Runner接口
  2. public inerface Runner
  3. {
  4. int ID = 1;
  5. void run ();
  6. }

  1. //定义一个interface Animal,它继承于父类Runner
  2. interface Animal extends Runner
  3. {
  4. void breathe ();
  5. }

  1. //定义Fish类,它实现了Animal接口的方法run()和breather()
  2. class Fish implements Animal
  3. {
  4. public void run () //实现了Animal方法run()
  5. {
  6. System.out.println("fish is swimming");
  7. }
  8. public void breather()
  9. {
  10. System.out.println("fish is bubbing");
  11. }
  12. }
  13. //定义了一个抽象类LandAnimal,它实现了接口Animal的方法。
  14. abstract LandAnimal implements Animal
  15. {
  16. public void breather ()
  17. {
  18. System.out.println("LandAnimal is breathing");
  19. }
  20. }
  21. //定义了一个类Student,它继承了类Person,并实现了Runner接口的方法run()。
  22. class Student extends Person implements Runner
  23. {
  24. ......
  25. public void run ()
  26. {
  27. System.out.println("the student is running");
  28. }
  29. ......
  30. }
  31. //定义了一个接口Flyer
  32. interface Flyer
  33. {
  34. void fly ();
  35. }
  36. //定义了一个类Bird,它实现了Runner和Flyer这两个接口定义的方法。
  37. class Bird implements Runner , Flyer
  38. {
  39. public void run () //Runner接口定义的方法。
  40. {
  41. System.out.println("the bird is running");
  42. }
  43. public void fly () //Flyer接口定义的方法。
  44. {
  45. System.out.println("the bird is flying");
  46. }
  47. }
  48. //TestFish类
  49. class TestFish
  50. {
  51. public static void main (String args[])
  52. {
  53. Fish f = new Fish();
  54. int j = 0;
  55. j = Runner.ID;
  56. j = f.ID;
  57. }
  58. }

接口实现的注意点:

a)实现一个接口就是要实现该接口的所有的方法(抽象类除外)。
b)接口中的方法都是抽象的。
c)多个无关的类可以实现同一个接口,一个类可以实现多个无关的接口。

 

extends与implements的区别:

extends是继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承,JAVA中不支持多重继承,但是可以用接口来实现,这样就用到了implements,继承只能继承一个类,但implements可以实现多个接口,用逗号分开就行了。

比如:

class A extends B implements C,D,E {} (class 子类名 extends 父类名 implenments 接口名)

父类与子类继承关系上的不同:

A a = new B(); 结果a是一个A类的实例,只能访问A中的方法,那么又和A a = new A();有什么区别呢?

***********************************************************************************************

class B extends A
继承过后通常会定义一些父类没有的成员或者方法。
A a = new B();
这样是可以的,上传。
a是一个父类对象的实例,因而不能访问子类定义的新成员或方法。

***********************************************************************************************

假如这样定义:
class A

{
int i;
void f(){}
}
class B extends A

{
int j;
void f(){} //重写
void g(){}
}
然后:
B b = new B();
b就是子类对象的实例,不仅能够访问自己的属性和方法,也能够访问父类的属性和方法。诸如b.i,b.j,b.f(),b.g()都是合法的。此时b.f()是访问的B中的f()


A a = new B();
a虽然是用的B的构造函数,但经过upcast,成为父类对象的实例,不能访问子类的属性和方法。a.i,a.f()是合法的,而a.j,a.g()非法。此时访问a.f()是访问B中的f()

***********************************************************************************************

A a = new B(); 这条语句,实际上有三个过程:
(1) A a;
将a声明为父类对象,只是一个引用,未分配空间
(2) B temp = new B();
通过B类的构造函数建立了一个B类对象的实例,也就是初始化
(3) a = (A)temp;
将子类对象temp转换未父类对象并赋给a,这就是上传(upcast),是安全的。
经过以上3个过程,a就彻底成为了一个A类的实例。
子类往往比父类有更多的属性和方法,上传只是舍弃,是安全的;而下传(downcast)有时会增加,通常是不安全的。

***********************************************************************************************

a.f()对应的应该是B类的方法f()
调用构造函数建立实例过后,对应方法的入口已经确定了。
如此以来,a虽被上传为A类,但其中重写的方法f()仍然是B的方法f()。也就是说,每个对象知道自己应该调用哪个方法。
A a1 = new B();
A a2 = new C();
a1,a2两个虽然都是A类对象,但各自的f()不同。这正是多态性的体现。

***********************************************************************************************

原文地址:https://www.cnblogs.com/aipan/p/6294979.html