阅读了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. 关于IE8导航串行的问题

    1.概述: 作为一个前端人员,多浏览器兼容是必须必备的技能,现在一般要求是兼容IE8及以上,如果兼容IE6的话,会麻烦一些,这里介绍的是在IE8状态下我们导航条错位的问题. 2.导航错位代码 < ...

  2. Java基础知识强化59:String(字符串)和其他类型的相互转化

    1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...

  3. BitmapFactory 加载图片到内存

    Bitmap占用内存分析 Android的虚拟机是基于寄存器的Dalvik,它的最大堆(单个进程可用内存)大小一般是16M,当然不同设备是不一样的,可以查看/system/build.prop文件,[ ...

  4. LINUX增加并管理用户

    使用groupadd命令增加新的用户组 使用useradd命令增加新用户: useradd -s /bin/bash -d /home/XXX -g group username 使用passwd修改 ...

  5. (转)js 中{},[]中括号,大括号使用详解

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如:var LangShen = {"Name":"Langshen",&qu ...

  6. 《第一行代码》学习笔记34-服务Service(1)

    1.服务是Android中实现程序后台运行的解决方案,适用于执行不需要和用户交互而且要长期运行的任务. 2.服务的运行不依赖于任何用户界面,或切到后台,或用户打开了另外一个应用程序,服务能够保持正常运 ...

  7. Swift - 43 - 继承, 多态, 析构函数

    import Foundation /* 什么叫继承: 可以简单理解为一个类可以从它的父类或者基类中直接拿属性或者方法去使用 冒号":"表示两者之间的继承关系 */ class P ...

  8. js带箭头左右翻动控制

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 【C#】.NET中设置代理服务器浏览网页的实现--转载

    目前很多种类的浏览器中都有代理服务器的设置,用户可以通过浏览器自定义更换自己的IP,实现在线代理翻(河蟹)墙浏览网页. 而在.NET中,亦可以通过调用API函数InternetSetOption来实现 ...

  10. Github提交代码及分支管理

    #提交代码cd <Cat>git initgit add .git statusgit commit -m "注释"git remote add origin < ...