Section 7 - 8 polymorphsim
2015-06-30 05:38
169 查看
Inheritance:
Members of a class: instance variables and methods.
Subclass extends the superclass. The subclass inherits from the superclass.
Instance variables are not overridden.
The lowest method in the inheritance tree wins.
IS-A relationship, extends, implements.
HAS-A relationship.
A subclass inherits all public instance variables and methods of the superclass, but not the private instance variables and methods.
Polymorphism:
1. Reference type can be a superclass of the actual object type.
2. Polymorphic arrays.
3. Polymorphic arguments and return types.
Non-extensive class:
1. non-public class can be subclassed only by classes in the same package.
2. final class
3. private constructor. Cannot be instantiated.
Non-overridden method:
1. final class
2. final method
The overridden method can't be less accessible.
Overloading a method:
Methods with same name but different arguments lists. No polymorphism. Method signature: name and arugments.
1. Argument lists must be different.
2. Return types can be different but should not be the only difference.
Abstract class and Concrete class:
Abstract class cannot be instantiated.
Abstract methods must be overridden. The class also has to be abstract.
Abstract class can have abstract and non-abstract methods.
Abstract class can implement inherited abstract methods.
Any class that doesn't explicitly extend another class, implicitly extends Object.
The compiler decides whether you can call a method based on the reference type, not the actual object type. The reference is a Remote Control which refers to the corresponding object.
Use instanceof to check the class type.
Java does not allow multiple inheritance.
A java interface only has abstract methods. Interface methods are implicitly public and abstract, and not encouraged to type these words.
Extends one parent. Interface are the roles to play.
接口往往是对外展示的,方法默认为public和abstract。static方法只供interface定义内使用。default可以定义函数body,可以被复写。
Members of a class: instance variables and methods.
Subclass extends the superclass. The subclass inherits from the superclass.
Instance variables are not overridden.
The lowest method in the inheritance tree wins.
IS-A relationship, extends, implements.
HAS-A relationship.
A subclass inherits all public instance variables and methods of the superclass, but not the private instance variables and methods.
Polymorphism:
1. Reference type can be a superclass of the actual object type.
Animal a = new Dog();
2. Polymorphic arrays.
Animal[] a;
3. Polymorphic arguments and return types.
public void takeAnimal(Animal animal);// can take a Dog as arguments.
Non-extensive class:
1. non-public class can be subclassed only by classes in the same package.
2. final class
3. private constructor. Cannot be instantiated.
Non-overridden method:
1. final class
2. final method
The overridden method can't be less accessible.
Overloading a method:
Methods with same name but different arguments lists. No polymorphism. Method signature: name and arugments.
1. Argument lists must be different.
2. Return types can be different but should not be the only difference.
Abstract class and Concrete class:
Abstract class cannot be instantiated.
Abstract methods must be overridden. The class also has to be abstract.
Abstract class can have abstract and non-abstract methods.
Abstract class can implement inherited abstract methods.
Any class that doesn't explicitly extend another class, implicitly extends Object.
The compiler decides whether you can call a method based on the reference type, not the actual object type. The reference is a Remote Control which refers to the corresponding object.
Use instanceof to check the class type.
if(o instanceof Dog) { Dog d = (Dog) o; }
Java does not allow multiple inheritance.
A java interface only has abstract methods. Interface methods are implicitly public and abstract, and not encouraged to type these words.
Extends one parent. Interface are the roles to play.
接口往往是对外展示的,方法默认为public和abstract。static方法只供interface定义内使用。default可以定义函数body,可以被复写。
相关文章推荐
- HackerRank - "The Coin Change Problem"
- The Venetian Macau h
- 数列、集合、逻辑学起码常识暴露课本一系列重大错误 ——数列起码常识否定5千年“常识”:无最大自然数
- 让程序员不再苦逼的四大神器
- hdu 5270 ZYB loves Xor II
- LeetCode "Roman to Integer"
- ORACLE 如何查询被锁定表及如何解锁释放session
- LeetCode "Integer to Roman"
- 点赞和吐糟Adblock Plus~进阶教程
- House Robber II
- 【Leetcode】【hard】Binary Tree Postorder Traversal
- House Robber
- PMC Flashtec + Memblaze PBlaze 4 = 性能惊人的NVM Express SSD!
- UML期末复习题——2.5:System Sequence Diagram & Post-condition
- summary ranges
- CentOS 6.5配置nfs服务
- CentOS 6.5配置nfs服务
- linux操作系下RAR的使用
- linux操作系下RAR的使用
- BLOB二进制对象(blob.c/h)