资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
SCJP Study Notes: Chapter 2 Object OrientationPage 1 of 9Chapter 2 Object Orientation1. Encapsulation- keep instance var protected by using private- make public accessor method (getter and setter)2. Inheritance, IS-A, HAS-AEvery class in JAVA is a subclass of object classe.g. inherit equals, instanceof methodInheritance provide code reuse, polymorphism2.1 IS-Ao Through inheritance (extends)o Through implements (interface)e.g. BMW is-a Car BMW extends CarCar is-a Vehicle BMW is a vehicle (2 level)2.2 HAS-AClass A HAS-A B if code in Class A has a ref. to instance of Be.g. BMW IS A Car, BMW HAS-A Wheelpublic class BMW extends Car private Wheel myWheel;3. PolymorphismJava does NOT support multiple inheritancepublic interface Accelerable accelerate ( );class BMW extends Car implements AccelerableBMW can be treated polymorphically as:BMW bmw = new BMW( );Object O = bmw;Car car = bmw;Accelerable a = bmw;Car and bmw can invoke drive( ) method4. Overriding / Overloading4.1 Overridingwhen a class inherit method from superclass, have opportunity to override except that the method is marked finalpublic class TestCar public static void main(String args) Car a = new Car( );Car b = new BMW( ); / Car ref, BMW objectSCJP Study Notes: Chapter 2 Object OrientationPage 2 of 9a.drive( ); / car versionb.drive( ); / bmw versionclass Car public void drive( ) / Overridden methodclass BMW extends Car public void drive( ) / Overriding methodpublic void accelerate Car c = new Car( );c.accelerate( ); / ERRORcompiler looks only at ref. type, NOT instance typeThe overriding method cannot have a more restrictive access modifier than the method being overriddene.g. drive( ) in BMW cant be private or protectedRules for overriding a method1. Arg. list must exactly match the overridden method, O.W. end up with overload2. return type must be the same as, or subtype of the return type declared in the original overridden method in the superclass3. access level cant be more restrictive than the overridden method4. access level CAN be LESS restrictive 5. Instance method can be overridden only if they are inherit by subclass6. the overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declare the exception7. the overriding method MUST NOT throw checked exception that are new or broader than those declared by the overridden methode.g. FileNotFoundException cant be overrided by SQLException8. the overriding method CAN throw narrower or fewer exception9. CANNOT override a method marked final, static10. if method cant inherited, cant override it, e.g. private method.Invoke superclass method super.drive( );e.g. public class Car public void drive ( ) Illegal Override:private void drive( ) public void drive( ) throws IOException public void drive(String speed) public String drive ( ) 4.2 Overloaded Method- let you reuse same method name, with diff. arg (optionally return type)SCJP Study Notes: Chapter 2 Object OrientationPage 3 of 9Rules for Overload1. Overloaded method MUST change the arg. list2. Overloaded method CAN change the return type3. Overloaded method CAN change the access modifier4. Overloaded method CAN declare new or broader checked exception5. A method can be overloaded in the same class or in subclasse.g. public class Foo public void doStuff(int y, String s) class Bar extends Foo public void doStuff(int y, long s) throws IOException e.g. public void changeSize(int size, String name, float pattern) Legal overload:public void changeSize(int size, String name) public int changeSize(float pattern, String name) throws IOExceptionInvoking overloaded method that take obj. ref.class Car class BMW extends Car class UseCar public void doStuff(Car c) / Car public void doStuff(BMW b) / BMW public static void main(String args) UseCar uc = new UseCar( );Car carObj = new Car( );BMW bmwObj = new BMW( );uc.doStuff(carObj); / Caruc.doStuff(bmwObj); / BMWCar carRefToBMW = new BMW( );uc.doStuff(carRefToBMW);O/P: Car signature of method is NOT dynamically decided at runtime, the ref. type NOT obj type determine which overloaded method is invokede.g. public class Car public void drive ( ) / Generic Car public class BMW extends Car public void drive ( ) / BMW (Override) public void drive (String s) / BMW + s (Overload) 1. Car c = new Car ( ); / Generic Carc.drive( );No problem, since overload, rather than overideSCJP Study Notes: Chapter 2 Object OrientationPage 4 of 92. BMW b = new BMW( ); / BMWb.drive( );3.Car c = new BMW( ); / BMW polymorphismc.drive( );4. BMW b = new BMW( ); / BMW testb.drive(“test”);5. Car c = new Car ( ); / ERROR, car doesnt have drive(s)c.drive(“test”);6. Car c = new BMW( ); / ERROR, still look at Carc.drive(“test”);7. BMW b = new Car( ); / ERROROverload Method Overridden MethodArgu
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号