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 ...
随机推荐
- MyEclipse中用Maven创建Web项目(亲测有效)
new --> other 1.Wizards: mvaen 2.Maven Project 3.Next Use Default Workspace Location 1.weba ...
- 关于在centos7上安装vmtools的各种报错的问题处理
基本上如果安装vmtools时报错的错,首先记录一下报错信息 例如,本次遇见的就是 /tmp/modconfig-8mD7iy/vmhgfs-only/page.c:1625:23: 错误:提供给函数 ...
- eCharts的随笔
1.散点图中找最优记录 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- 【C#基础】HTTP发送POST二进制数据
//postdata为数组的请求方式 public byte[] POST(string Url, byte[] byteRequest) { byte[] responsebody; HttpWeb ...
- cavium octeon 处理器启动总线Bootbus 简介
cavium octeon 处理器启动总线Bootbus 简介: 韩大卫@吉林师范大学 Boot-bus(启动总线)是cavium octeon处理器的一种用于启动系统的硬件. CPU通过boot b ...
- ETL-Career RoadMap
RoadMap: 1.Tester:sql的单体或批处理测试: 2. Application Developer 2.1 批处理手动工具(如何使用.如何调度批处理.如何生成批处理脚本): 2.2 批处 ...
- parcel write boolean值
http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcela ...
- 数据对接—kettle使用之二
这一篇开始进入kettle的一些常用插件的使用介绍,通过实例介绍不同插件的功能.这一篇说(Data Grid和文本文件输出)的使用. 文本文件输出介绍(可以略过,一般用不着): 1.Run ...
- C++11 : 外部模板(Extern Template)
在C++98/03语言标准中,对于源代码中出现的每一处模板实例化,编译器都需要去做实例化的工作:而在链接时,链接器还需要移除重复的实例化代码.显然,让编译器每次都去进行重复的实例化工作显然是不必要的, ...
- Linux的VI/VIM
参考自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html 作者:iTech 出处:http://itech.cnblogs.co ...