Abstract Methods and Classes
阅读了Java的官方Doc,在此总结如下。
Abstract Class & Method
In jave, "abstract" can be a modifier to class and method.
Abstract class:
A class that is declared abstract—it may or may not include abstract methods.
Abstract classes cannot be instantiated, but they can be subclassed.
Abstract method:
a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);
Relationship:
If a class includes abstract methods, then the class itself must be declared abstract.
If subclass of an abstract class does not implement all of the abstract methods, then the subclass must also be declared abstract.
Abstract Class vs Interface
Same:
Cannot be inistantiated.
Difference:
1. With abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods.
With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public.
2. You can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.
3. A class that implements an interface must implement all of the interface's methods.
When to use Abstract Class:
1. You want to share code among several closely related classes.
2. You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
3. You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
When to use Interface:
1. You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
2. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
3. You want to take advantage of multiple inheritance of type.
Abstract Methods and Classes的更多相关文章
- HeadFIrst Ruby 第二章总结 methods and classes
HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...
- Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field
一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...
- TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错
报错Can't instantiate abstract class Ultraman with abstract methods sttack 通过非常仔细的排查,发现错误如下: 1.单词拼写错误是 ...
- [Ruby] LEVEL 2 Methods and Classes
Optional Arguments Set default arguments, when we don't need to call it, we can simply skip it. def ...
- Java 面向对象概念
Interface 接口 An interface defines a protocol of communication between two objects. An interface decl ...
- Java学习笔记之方法重载,动态方法调度和抽象类
一.方法重载 如果子类中的方法与它的超类中的方法有相同的方法名,则称子类中的方法重载超类中的方法,特别是当超类和子类中的方法名和参数类型都相同时,在子类中调用该方法时,超类中的方法会被隐藏.考虑下面程 ...
- 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记
注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...
- 【译】Android API 规范
[译]Android API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android API Guidelines. 58e9b5f Project ...
- python 用abc模块构建抽象基类Abstract Base Classes
见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...
随机推荐
- 使用strace查看C语言级别的php源码
XCACHE XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接 ...
- SWFLoader交互
主应用程序: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx=& ...
- JavaScript新手学习笔记4——我记不住的几个坑:短路逻辑、按值传递、声明提前
1.短路逻辑 逻辑运算中,如果前一个条件已经可以得出最终结论,则后续所有条件不再执行!这里的逻辑运算指的是逻辑与和逻辑或. 我们要理解逻辑与是两个条件都为真的时候,才为真,如果第一个就是假的,那么后面 ...
- Unity3D性能优化总结
一.程序方面 01.务必删除脚本中为空或不须要的默认方法: 02.仅仅在一个脚本中使用OnGUI方法. 03.避免在OnGUI中对变量.方法进行更新.赋值,输出变量建议在Update内. 04.同一脚 ...
- Python对文件的操作(转)
一.文件对象 我理解的文件对象就是一个接口,通过这个接口对文件进行相关操作. <Python 核心编程>上说的很晦涩,这里没有深刻理解到,希望有人能解释给我听. >>> ...
- ftp nfs samba比较
首先从字面意思上区分一下:1. FTP(文件传输协议)2. NFS(网络文件系统)3. samba 即smb(服务信息块)协议其中FTP 是TCP/IP协议栈所提供的一种子协议,该子协议具体可以实现在 ...
- 【转】使用 Eclipse 调试 Java 程序的 10 个技巧
你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...
- Java ----------- 正则表达式(更新中。。。。。。)
RegEx:Regular Expression. 元字符 描述 \ 将下一个字符标记符.或一个向后引用.或一个八进制转义符.例如,“\\n”匹配\n.“\n”匹配换行符.序列“\\”匹配“\”而“\ ...
- win7 安装 IIS
和xp不同,xp安装 IIS需要下载额外的软件包. 步骤 1,依次打开 "控制面板" -> "程序" ->"打开或关闭windows功能& ...
- IIS上部署网站404错误
新装的系统上部署.net网站遇到403.404错误,可能原因记录: 1.应用程序池选择错误,一般选择4.0的 2.ASP.NET4.0应用程序池未安装(一般先安装了framework4.0,后安装ii ...