[Java]--Abstract Class vs Interfaces

本文详细介绍了Java中抽象类和接口的基本概念与使用方法。包括抽象类的特点:不能实例化但可以被继承;抽象方法必须在子类中实现;以及接口的概念:不能被实例化,包含公共静态最终字段及默认或静态方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Abstract Class

Definition

  1. An abstract class is a class that is declared abstract—it may or may not include abstract methods.
  2. An abstract classes cannot be instantiated.
  3. An abstract classes can be subclassed.
  4. If a class include abstract methods, the class itself must be declared abstract.
    Some Code example:
public abstract class GraphicObject {
   // declare fields
   // declare nonabstract methods
   abstract void draw();
}

2. Abstract Methods

The subclass of the abstract class must implement all the abstract methods in its parent class. IF NOT, then the subclass must be declared abstract too

3. Interface

  1. Interface cannot be instantiated either.
  2. Interface may also contain a mix of methods declared with or without implementation .
  3. All fields are automatically public, static, and final.
  4. All methods that you declare or define (as default methods) are public.
  5. Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Reference

  1. https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
  2. http://stackoverflow.com/questions/1320745/abstract-class-in-java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值