cspp51036javaprogramming内容摘要:
ains abstract methods and final variables example: interface FooInterface{ void foo1()。 int foo2(double x)。 } abstract interface FooInterface{ public abstract void foo1()。 public abstract int foo2(double x)。 } Both are correct! the abstract and public keywords are implied, so the shorthand is remended. Interfaces, cont. Unlike a class, an interface cannot be instantiated! Rather, an interface is implemented by some other class: class FooClass implements FooInterface{ .... } This means one thing only: FooClass must contain versions of both foo1 and foo2 that actually do something useful. We say that FooClass must provide implementations for all of the methods in FooInterface. Interfaces, cont. When a class implements an interface, think of the class as entering a contract to provide meat for all methods in the interface. The piler will check that this contract is adhered to. Otherwise, the class implementing the interface can contain anything else that a regular class contains. Again: do not try to instantiate an interface – this is meaningless! More interface rules A class may implement any number of interfaces as: class FooClass implements A, B, C{ ... } An interface may also contain public static final variables. Usually, these qualifiers are left off. We just say: interface MyConstants{ double PI =。 double E =。 } can be acessed as either or just PI by any class that implements the interface Certificationtype questions What happens when multiple interface implementation results in name conflicts? – if the methods have different signatures, they are considered overloaded and there is no problem – if the methods have the same signature and the same return type, they are considered to be the same method. – if they have the same signature and different return types, a pilation error will result. – If they have same signature/return type but throw different exceptions, they are considered to be same, and resulting throws list is union of original two Marker Interfaces Some interfaces contain no methods or constants at all (ie they are empty). These are called “marker interfaces”. Examples are Cloneable and Serializable in java library. We will understand these better once we understand subtypesupertype relationships. Subtyping with Interfaces Understanding mechanics of interfaces is only about 1/3 of the story. Next, we have to understand how interfaces allow for polymorphism. Finally, we study probably the hardest part – how to best use polymorphism to really write superior code. Rules of subtyping If class C implements interface I, then C is a subtype of I. What does this imply? We can do things like: C aC。 aC = new C()。 I aC。 aC = new C()。 In the latter case, we often say that the runtime type of aC is C, but the static or declared type is I. In the former case, both types are C This is the regular stuff Can do this also! Substitutability of Types Rule: A value of a subtype can appear wherever a value of its supertype is expected. In other words, a value of a sybtype can always substitute for a value of a supertype. To rephrase for objects: An instance of a subclass can appear wherever an instance of a superclass is expected. Note: superclass here refers to interface at this point. We will make more general soon. Generic examples of subtyping class Circle implements Drawable, Scalable{ ... Circle aCircle。 Drawable aDrawable。 Scalable aScalable。 aCircle = new Circle()。 //ok aCircle = new Drawable()。 //BAD! aDrawable = new Circle()。 //ok aScalable = new Circle()。 //ok 1,3,4 are ok because a Circle object is created and it is assigned to a declared type of either Circle or one of its supertypes. More examples Drawable aDrawable。 Circle aCircle。 aCircle = new Circle()。 //fine aDrawable = aCircle。 //fine – aDrawable is type superclass aDrawable = new Circle()。 aCircle = aDrawable。 //NO。 cannot assign more general to //more specific without an explicit //cast aCircle = (Circle) aDrawable。 //this is ok – explicit cast! Widening and Narrowing The conversion of a subtype to one of its supertypes is called widening. The conversioning of a supertype to one of its subtypes is called narrowing. Widening happens automatically during an assignment. Nothing special is required. This is also typically called upcasting. Narrowing requires a proper explicit cast, otherwise the piler will plain. This is an example of Java‟s very strong typing. It is your best friend. Narrowing is also known as “downcasting”. ClassCasts and instanceof The java piler will allow any cast. If the cast is illegal, a runtime exception will be thrown (ClassCastException). There are two ways to safeguard against this. 1. with a trycatch block (later) 2. Using the instanceof operator as: if (aDrawable instanceof Circle){ aCircle = (Circle) aDrawable } instanceof tells the actual type of an object rather than its declared type. Why on earth do this? How could this ever be used to your advantage? Why not simlply type all Circles as Circle, etc. In other words, why ever do: Drawable aCircle = new Circle()。 vs. Circle aCircle = new Circle()。 Much easier to understand if we use upcasting in method calls. Using Upcasting in method calls Imagine there is a class Renderer that has a method Render that can operate on any object that knows how to morph any two objects that can draw themselves. .。 class Renderer{ ... public void morph(Drawable o1, Drawable o2){ //。cspp51036javaprogramming
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。
用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。