`
wangmored
  • 浏览: 162372 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java interface and abstract

    博客分类:
  • java
阅读更多
java编程思想中说“万物皆对象”;   我们知道所有的对象都是通过类来描绘的,但是反过来却不是这样。并不是所有的类都是用来描绘对象的.
如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
抽象类是我们对问题领域进行分析、设计中得出的抽象概念,是对一系列看上去不同,但是本质上相同的具体概念的抽象。
比如:如果我们进行一个图形编辑软件的开发,就会发现问题领域存在着圆、 三角形这样一些具体概念,它们是不同的,但是它们又都属于形状这样一个概念,形状这个概念在问题领域是不存在的,它就是一个抽象概念。正是因为抽象的概念 在问题领域没有对应的具体概念,所以用以表征抽象概念的抽象类是不能够实例化的。


在面向对象领域,抽象类主要用来进行类型隐藏。
[类型隐藏:                             我们可以构造出一个固定的一组行为的抽象描述,但是这组行为却能够有任意个可能的具体实现方式。这个抽象描述就是抽象类,而这一组任意个可能的具体实现则表现为所有可能的派生类。]
如:动物是一个抽象类,人、猴子、老虎就是具体实现的派生类,我们就可以用动物类型来隐藏人、猴子和老虎的类型。
接口:一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为(功能)。接口是一种特殊形式的抽象类。
区别:
1.抽象类在Java语言中表示的是一种继承关系,要想使得继承关系合理,父类和派生类之间必须存在”is a”关系,即父类和派生类在概念本质上应该是相同的。对于接口来说则不然,并不要求接口的实现者和接口定义在概念本质上是一致的,仅仅是实现了接口定义的契约而已。接口表示的是”like a”关系。 一个类只能使用一次继承关系。但是,一个类却可以实现多个接口
2.抽象类的定义中,我们可以赋予方法的默认行为。但是在接口的定义中,方法却不能拥有默认行为,

3.使用抽象类来定义允许多个实现的类型,比使用接口有一个明显的优势:抽象类的演化比接口的演化要容易的多。在后续的发行版中,如果希望在抽象类中增加一个方法,只增加一个默认的合理的实现即可,抽象类的所有实现都自动提供了这个新的方法。对于接口,这是行不通的。虽然可以在骨架实现类中增加一方法的实现来解决部分问题,但这不能解决不从骨架实现类继承的接口实现的问题。由此,设计公有的接口要非常谨慎,一旦一个接口被公开且被广泛实现,对它进行修改将是不可能的。


======================================================================
类描述了一个实体,包括实体的状态,也包括实体可能发出的动作。
接口定义了一个实体可能发出的动作。但是只是定义了这些动作的原型,没有实现,也没有任何状态信息。 所以接口有点象一个规范、一个协议,是一个抽象的概念;而类则是实现了这个协议,满足了这个规范的具体实体,是一个具体的概念。
从程序角度,简单理解,接口就是函数声明,类就是函数实现。需要注意的是同一个声明可能有很多种实现。

1、接口中定义类方法的原型,但是不能说是空方法,因为空方法的意思是有实现体,只不过实现体是空操作。实际上接口没有定义任何实现体。具体的实现体都是在实现接口的类中,接口只是定义了这些方法的调用方式。

你当然也可以不用接口,直接在类里面写方法,但是如果你的一组方法需要在很多类里实现,那么把它们抽象出来,做成一个接口规范,不是更好么?

2、一个类描述了一个实体,这个实体可能是一个复杂的对象,它的动作很多,如果把这些动作分类,用接口a定义其中的某一组动作,接口b定义其中的另外一组动作,这样的结构,比较清楚。

这种方式具备了多继承的优点,避免了多继承的缺陷。实际上在历史上,接口在很大程度上,是为了解决多继承带来的种种问题而设计出来的。

抽象类不能被实例化,也就是说,不能创建抽象类的任何对象。必须创建抽象类的子类,然后再创建子类的对象。一个接口可以继承多个接口,一个类可以实现多个接口。
分享到:
评论

相关推荐

    Java.Crash.Course.1541019245.epub

    Chapter 12: Interfaces and Abstract Classes in Java Chapter 13: Packages Chapter 14: Debugging Chapter 15: Enums Chapter 16: Generic Types Chapter 17: Threading Chapter 18: Graphical User Interface) ...

    Interactive.Object.Oriented.Programming.in.Java

    Later chapters cover further Java programming concepts, such as abstract classes, packages, and exception handling. At each stage you’ll be challenged by the author to help you absorb the information...

    Java面试题和答案(英文版).doc

    Java面试题和答案(英文版),What is the difference between an Interface and an Abstract class

    java实验8(Playing.java)

    The interface has an abstract method called play(). Create classes called Child, Musician and Actor that implement Playing. Create a program that demonstrates the use of the classes. Playing.java ...

    Java Crash Course [2016]

    Chapter 12: Interfaces and Abstract Classes in Java Chapter 13: Packages Chapter 14: Debugging Chapter 15: Enums Chapter 16: Generic Types Chapter 17: Threading Chapter 18: Graphical User Interface) ...

    Java Swing, Second Edition

    When Java was first released, its user interface facilities were a significant weakness. The Abstract Window Toolkit (AWT) was part of the JDK from the beginning, but it really wasn't sufficient to ...

    java实验七

    The interface has an abstract method called play(). Create classes called Child, Musician and Actor that implement Playing. Create a program that demonstrates the use of the classes. Playing.java ...

    Learn.JAVA.PROGRAMMING.from.scratch.B0140CLLFG

    Chapter 5: Inheritance (Abstract class and Interface) Chapter 6: Data types Chapter 7: Object comparison Chapter 8: Data Structures and their operations Chapter 9: Exceptions and Errors Chapter 10: ...

    基于Java的日程管理系统的设计与实现.docx

    Therefore, people need a simple and clean interface management system to help users manage their schedules. This paper designs and implements a scheduling management system that uses Java language , ...

    java扫雷毕业

     JBuilder is an excellent integrated development environment Borlan company for rapid development of Java applications, and its friendly development interface, powerful component...

    基于Java的图形图像处理软件的设计与实现.doc

    关键词:Java 绘图板 图形图像编辑 滤镜处理 边缘处理 Image processing software design and implementation Abstract:With the rapid development of computer technology, graphic chart of image technology ...

    Addison.Wesley.The.Java.Programming.Language.4th.Edition.Aug.2005.chm

    Chapter 4Interfacesdescribes how to declare interface types that are abstract descriptions of behavior that provide maximum flexibility for class designers and implementors. Chapter 5Nested Classes ...

    30道英文Java面试题与答案(核心知识)

    What's the difference between an interface and an abstract class? Q30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access...

    基于java的雷电游戏的设计与实现.pdf

    Eclipse平台 i Design and Implementation of lighting Games based on Java Abstract Lightning is the history of the development of computer games in the early one of the most classic game, often on a ...

    基于Java的无人超市购物系统的设计与实现.docx

    This system has the characteristics of simple operation, easy to understand, clear and clear user interface and so on. Key words:Mysql;Servlet;Self-service supermarket 基于Java的无人超市购物系统的...

    JAVA面试题最全集

    abstract的,又被声明为final的。将变量或方法声明为final,可以保证它们在使用中不被改变。被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。被声明为final的方法也同样只能使用,...

    基于JAVA的BBS论坛的设计与实现.doc

    MySQL数据库 Design and implementation of JAVA based on the BBS Forum Abstract In today's era of the forum has become an important platform on the Internet, its main function is to provide a mutual ...

    基于JAVA-EE的自行车在线租赁管理系统的设计与实现.txt

    The rapid development of JAVA EE platform, the maturity of technology and the continuous expansion of application fields provide a good platform and convenient operation interface for the interaction...

    Java 高级特性.doc

    JAVA高级特性 1.静态导入:先举个离例子 。 import java.lang.Integer.parseInt; public class StaticImport { int x = parseInt("123"); System.out.println(x); } 这样的程序如果不在IDE 工具中输入,是很难...

Global site tag (gtag.js) - Google Analytics