《SCJP_Ch02 Object Orientation》由会员分享,可在线阅读,更多相关《SCJP_Ch02 Object Orientation(9页珍藏版)》请在金锄头文库上搜索。
1、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, instanc
2、eof 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
3、 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;Accelerabl
4、e 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( ); /
5、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( ); / E
6、RRORcompiler 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
7、 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
8、 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. FileNotFoundEx
9、ception 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 (
10、 ) 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 O
11、verload1. 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
12、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 IOE
13、xceptionInvoking 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); / Ca
14、ruc.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
15、 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