Java三个大特性
Posted 张紫娃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java三个大特性相关的知识,希望对你有一定的参考价值。
封装(encapsulation)
封装思想 :
类 → 本身类就是封装的思想
方法 → 本身方法就是封装的思想
保护属性 → 私有属性+get/set方法
继承(inheritance)
特点
- 子类通过extends关键字继承父类
- Java doesn’t support multiple inheritance,
继承只是单继承:
目的是为了让类变得更加的安全,如果继承了两个的话,调用父类方法,如果两个类都有这个方法,不知道继承的哪一个。可以通过 "传递" 的方式实现多继承的效果,后续还有多实现的方式
- 每一个类都有它继承的类
如果没写extends默认继承Object,如果写了extends就继承extend后面的类,每个类都会直接货间接的继承Object
- 子类可以调用父类中的成员:
- Private members of superclass are not directly accessible to subclass.but it can be indirectly accessible via getter and setter methods.
- We can call the superclass methods and access superclass variables using super keyword.
(public protected)权限修饰符修饰的属性和方法可以当做自己的来使用。- Superclass constructors are not inherited by subclass.
构造方法严格意义来说,不算作子类继承过来的,只是子类在调用构造方法时默认调用父类的构造方法- 程序块严格意义来说,不算作子类继承过来的,程序块没有名字 → 子类不能直接调 → 子类执行构造方法之前 → 默认调用了父类的构造方法 → 父类调用构造方法之前 → 自动执行父类的块
- We can override the method of Superclass in the Subclass. However we should always annotate overridden method with @Override annotation.
子类继承的父类的方法如果不能满足子类的需求时,可以在子类中重写(覆盖)父类该方法
- 子类可以添加自己独有的属性和方法
- We can create an instance of subclass and then assign it to superclass variable, this is called upcasting
- When an instance of Superclass is assigned to a Subclass variable, then it’s called downcasting
- We can use instanceof instruction to check the inheritance between objects.
- We can’t extend Final classes in java
参照
Inheritance in Java Example.
Multiple Inheritance in Java.
继承有哪些特点?
多态(polymorphism)
Java语言是如何实现多态的?
以上是关于Java三个大特性的主要内容,如果未能解决你的问题,请参考以下文章