interface abstract 区别
2015-08-19 11:33
253 查看
java中interface接口与abstract class抽象类的区别 interface和abstract class是Java中实现多态的基础,都很有用,但也有一些区别:
interface被继承时用的关键字是implements,而且可以同时implements多个interface,而abstract class被继承时用的关键字是extends,而且只能同时extends一个abstract class。
interface内定义的方法都是public的,而abstract class内的则不一定。
interface内定义的成员对象都是static & final的,而abstract class不是
interface的方法不能有默认实现,只有一个申明,而abstract class内申明abstract的方法不能有实现,非申明abstract的方法可以有默认实现。
interface被implements时,所有方法都必须被实现,必须申明是public的,而abstract class被extends时只有abstract的方法才必须被实现,别的有默认实现的直接被继承过来。
例子:
public interface Instrument {
int i = 5; //static & final
//can't have method definitions:
void play(); //automatically public
void adjust();
}
public abstract class AbstractCachedTable {
public Hashtable table = new Hashtable();
abstract public void refresh();
public Object get(Object o) {
Object target = null;
target = table.get(o);
return target;
}
}
一般情况下,建议使用interface(因为能同时implements多个interface的特性,能更好地实现多态),当需要有默认实现和成员对象时才考虑abstract class。
来源:ava中interface接口与abstract class抽象类的区别 interface和abstract class是Java中实现多态的基础,都很有用,但也有一些区别:
interface被继承时用的关键字是implements,而且可以同时implements多个interface,而abstract class被继承时用的关键字是extends,而且只能同时extends一个abstract class。
interface内定义的方法都是public的,而abstract class内的则不一定。
interface内定义的成员对象都是static & final的,而abstract class不是
interface的方法不能有默认实现,只有一个申明,而abstract class内申明abstract的方法不能有实现,非申明abstract的方法可以有默认实现。
interface被implements时,所有方法都必须被实现,必须申明是public的,而abstract class被extends时只有abstract的方法才必须被实现,别的有默认实现的直接被继承过来。
例子:
public interface Instrument {
int i = 5; //static & final
//can't have method definitions:
void play(); //automatically public
void adjust();
}
public abstract class AbstractCachedTable {
public Hashtable table = new Hashtable();
abstract public void refresh();
public Object get(Object o) {
Object target = null;
target = table.get(o);
return target;
}
}
一般情况下,建议使用interface(因为能同时implements多个interface的特性,能更好地实现多态),当需要有默认实现和成员对象时才考虑abstract class。
来源:http://guixian.iteye.com/blog/185763
interface被继承时用的关键字是implements,而且可以同时implements多个interface,而abstract class被继承时用的关键字是extends,而且只能同时extends一个abstract class。
interface内定义的方法都是public的,而abstract class内的则不一定。
interface内定义的成员对象都是static & final的,而abstract class不是
interface的方法不能有默认实现,只有一个申明,而abstract class内申明abstract的方法不能有实现,非申明abstract的方法可以有默认实现。
interface被implements时,所有方法都必须被实现,必须申明是public的,而abstract class被extends时只有abstract的方法才必须被实现,别的有默认实现的直接被继承过来。
例子:
public interface Instrument {
int i = 5; //static & final
//can't have method definitions:
void play(); //automatically public
void adjust();
}
public abstract class AbstractCachedTable {
public Hashtable table = new Hashtable();
abstract public void refresh();
public Object get(Object o) {
Object target = null;
target = table.get(o);
return target;
}
}
一般情况下,建议使用interface(因为能同时implements多个interface的特性,能更好地实现多态),当需要有默认实现和成员对象时才考虑abstract class。
来源:ava中interface接口与abstract class抽象类的区别 interface和abstract class是Java中实现多态的基础,都很有用,但也有一些区别:
interface被继承时用的关键字是implements,而且可以同时implements多个interface,而abstract class被继承时用的关键字是extends,而且只能同时extends一个abstract class。
interface内定义的方法都是public的,而abstract class内的则不一定。
interface内定义的成员对象都是static & final的,而abstract class不是
interface的方法不能有默认实现,只有一个申明,而abstract class内申明abstract的方法不能有实现,非申明abstract的方法可以有默认实现。
interface被implements时,所有方法都必须被实现,必须申明是public的,而abstract class被extends时只有abstract的方法才必须被实现,别的有默认实现的直接被继承过来。
例子:
public interface Instrument {
int i = 5; //static & final
//can't have method definitions:
void play(); //automatically public
void adjust();
}
public abstract class AbstractCachedTable {
public Hashtable table = new Hashtable();
abstract public void refresh();
public Object get(Object o) {
Object target = null;
target = table.get(o);
return target;
}
}
一般情况下,建议使用interface(因为能同时implements多个interface的特性,能更好地实现多态),当需要有默认实现和成员对象时才考虑abstract class。
来源:http://guixian.iteye.com/blog/185763
相关文章推荐
- B树学习总结
- poj 1741 树分治
- centos下把python版本升级成2.7
- 开发日记(7)如何让一个activity更新application中的数据,返回后更新呢?
- win10更新定点设置后电脑Fn键失灵该怎么办?
- HDU2602
- android浏览器插件介绍
- Spring+Hibernate 数据库配置信息的加密
- IOS UITableView拖动排序功能
- hibernate注解方式实现一对多映射
- 数组的练习
- 2015 HUAS Summer Trainning #6~O
- 【iOS学习】八、JSON数据学习
- JS实现仿新浪微博发布内容为空时提示功能代码
- ubuntu下安装 oracle客户端
- Leetcode -- Ugly Number II
- 项目运行:The Tomcat server configuration at \Servers\Tomcat v6.0 Server at localhost-config is missing.
- K3固定资产卡片新增提示“SetBlob数据类型不符 = 0
- 用批处理如何实现telnet登陆并进行一些简单命令
- 我在工作中遇到的JS问题汇总