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 ...
随机推荐
- 组播MAC地址转换关系及唯一性处理
组播地址与其对应的组播MAC换算关系如下: 组播MAC=组播MAC标识+组播IP后23位对应的二进制位(32位的IP地址取后23位导致32组IP地址对应的多播IP相同) (IANA把01:00:5 ...
- html中的banner自适应屏幕代码
<html> <head> <title>Title</title> <style> .bannerbox { width:100%; po ...
- 最牛逼的的shell命令
参考 远程diff [root@jiangyi01.sqa.zmf /home/ahao.mah/ALIOS_QA/tools/iperf] #ssh ahao.mah@dnstest02.tbc c ...
- MYSQL触发器学习笔记
课程学至金色晨曦科技公司技术总监沙利穆 触发器 1. 什么是触发器 触发器是一种特殊类型的存储过程,不由用户直接调用.创建触发器时会对其进行定义,以便在对特定表或列作特定类型的数据修改时执 ...
- Apache HttpComponents Client 4.0快速入门/升级-2.POST方法访问网页
Apache HttpComponents Client 4.0已经发布多时,httpclient项目从commons子项目挪到了HttpComponents子项目下,httpclient3.1和 h ...
- [置顶] 对于最新的Android病毒,Smack完全可以抵御
我写的有关Smack和Android系统结合的技术博客,希望有志之士可以参透其中奥妙,Smack作为Linux内核安全模块,已经可以移植到Android系统中,如果大家弄清我写的Smack安全策略,可 ...
- cocos2dx 利用CCOrbitCamera实现扑克牌翻牌效果
[cpp] view plaincopy #include "HelloWorldScene.h" #include "SimpleAudioEngine.h" ...
- 淘宝内部大量使用的开源系统监控工具--Tsar
Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用 它不仅可以监控CPU.IO.内存.TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务器状态 ...
- C程序设计的抽象思维-算法分析-大多数元素
[问题] 请编写下面函数 int MajorityElement(int array[],int n); 该函数返回数组array中的多数元素.多数元素是指在占绝对多数(至少51%)的一个值.假设多数 ...
- 台式电脑部署xen虚拟化的各种问题
本打算用一台台式机做xen虚拟化,搞了一天搞得焦头烂额还是没搞定,中间遇到各种奇葩问题,这里mark一下 1.计划用三块2TB的SATA硬盘,然后装centos5 做虚拟化,结果忘记了centos5最 ...