阅读了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的更多相关文章

  1. HeadFIrst Ruby 第二章总结 methods and classes

    HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...

  2. Java SE 8 docs——Static Methods、Instance Methods、Abstract Methods、Concrete Methods和field

    一.Static Methods.Instance Methods.Abstract Methods.Concrete Methods ——Static Methods:静态方法 ——Instance ...

  3. TypeError:Can't instantiate abstract class Ultraman with abstract methods sttack 报错

    报错Can't instantiate abstract class Ultraman with abstract methods sttack 通过非常仔细的排查,发现错误如下: 1.单词拼写错误是 ...

  4. [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 ...

  5. Java 面向对象概念

    Interface 接口 An interface defines a protocol of communication between two objects. An interface decl ...

  6. Java学习笔记之方法重载,动态方法调度和抽象类

    一.方法重载 如果子类中的方法与它的超类中的方法有相同的方法名,则称子类中的方法重载超类中的方法,特别是当超类和子类中的方法名和参数类型都相同时,在子类中调用该方法时,超类中的方法会被隐藏.考虑下面程 ...

  7. 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记

    注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...

  8. 【译】Android API 规范

    [译]Android API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android API Guidelines. 58e9b5f Project ...

  9. python 用abc模块构建抽象基类Abstract Base Classes

    见代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/01 16:58 from abc import ABCMet ...

随机推荐

  1. 转载——Struts2中的constant详解

    http://bhw1015.iteye.com/blog/1258441 通过对这些属性的配置,可以改变Struts 2 框架的一些默认行为,这些配置可以在struts.xml文件中完成,也可以在s ...

  2. nginx 配置自签名的ssl证书

    最近要搭一个https的测试环境,使用nginx做反向代理. 网上找过不少资料,但过程不是很完整,吃了不少亏,故把自己的操作过程总结下来.如果你刚好遇到这个问题,希望对你有帮助! ********** ...

  3. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  4. mac下Apache添加限速模块mod_bw

    官方文档: Apache2 - Mod_bw v0.7 Author : Ivan Barrera A. (Bruce) HomePage : Http://Ivn.cl/apache & h ...

  5. Lesson2.1:LinkedList、ConcurrentLinkedQueue、LinkedBlockingQueue对比分析

    写这篇文章源于我经历过的一次生产事故,在某家公司的时候,有个服务会收集业务系统的日志,此服务的开发人员在给业务系统的sdk中就因为使用了LinkedList,又没有做并发控制,就造成了此服务经常不能正 ...

  6. 【架构师之路】依赖注入原理---IoC框架

    1 IoC理论的背景    我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑.  图1:软件系统中耦合的对象 如果我们 ...

  7. MC, MCMC, Gibbs採样 原理&amp;实现(in R)

    本文用讲一下指定分布的随机抽样方法:MC(Monte Carlo), MC(Markov Chain), MCMC(Markov Chain Monte Carlo)的基本原理,并用R语言实现了几个样 ...

  8. NSUserDefaults读取和写入自定义对象

    NSUserDefaults可以存取一些短小的信息. 比如存入再读出一个字符串到NSUserDefaults: - NSString *string = [NSString stringWithStr ...

  9. UIColor各种颜色转换

    1.Hex值颜色转换 #import <UIKit/UIKit.h> @interface UIColor (Extension) // 根据无符号的32位整数转换为对应的RGB颜色 + ...

  10. uva 105 - The Skyline Problem

    一.用数组储存该位置的最高点即可(图形的连续点离散化),注意左边界及右边界的情况: 注意:无论建筑物最左边是盖到哪里,你都得从1开始输出(输入输出都是integer,所以才能离散化): #includ ...