1、Reusing classes,Composition syntax,Simply place object references inside new classes. /SprinklerSystem.javaReference initialization. /Bath.javaAt the point the objects are defined;In the constructor for that class;Right before you actually need to use the object. (lazy initialization)Using instance
2、initialization.,Inheritance syntax,extends keyword. /Detergent.java“this new class is like that old class.”You automatically get all the fields and methods in the base class.super keyword.Refers to the “superclass” that the current class inherits.,Initializing the base class,When you create an objec
3、t of the derived class, it contains within it a subobject of the base class.Java automatically inserts calls to the base-class constructor in the derived-class constructor. /Cartoon.java,Constructors with arguments,If there is no default base-class constructor, or if you want to call a base-class co
4、nstructor that has arguments, you must explicitly write a call to the base-class constructor using the super keyword and the appropriate argument list. /Chess.java,Delegation,Delegation is midway between inheritance and composition.You can place a member object in the class youre building.You can al
5、so expose all the methods from the member object in your new class. /SpaceShipControls.java; SpaceShipDelegation.java,Combining composition and inheritance,It is very common to use composition and inheritance together. /PlaceSetting.javaAlthough the compiler forces you to initialize the base classes
6、, it doesnt watch over you to make sure that you initialize the member objects, so you must remember to pay attention to that.,Guaranteeing proper cleanup,If you want something cleaned up for a class, you must explicitly write a special method to do it. /CADSystem.javaFirst perform all the cleanup w
7、ork specific to your class, in the reverse order of creation. Then call the base-class cleanup method.,Name hiding,If a Java base class has a method name thats overloaded several times, redefining that method name in the derived class will not hide any of the base-class version. /Hide.javaOverride,C
8、hoosing composition vs. inheritance,Composition is generally used when you want the functionality of an existing class inside your new class, but not its interface. /Car.javaWhen you inherit, you take an existing class and make a special version of it.The is-a relationship is expressed with inherita
9、nce, and the has-a relationship is expressed with composition.,protected,protected: available to anyone who inherits from this class or anyone else in the same package. /Orc.javaAlthough its possible to create protected fields, the best approach is to leave the fields private; you should always pres
10、erve your right to change the underlying implementation.,Upcasting,/Wind.javaCasting from a derived type to a base type moves up on the inheritance diagram, so its commonly referred to as upcasting.Upcasting is always safe.,The final keyword,final: “This cannot be changed”There are three places wher
11、e final can be used: data, methods, and classes.,final data,A constant is useful for two reasons:It can be a compile-time constant that wont ever change.It can be a value initialized at run time that you dont want changed.In java, these sorts of constants must be primitives and are expressed with th
12、e final keyword.A value must be given at the time of definition of such a constant.,A field that is both static and final has only one piece of storage that cannot be changed.With an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never
13、be changed to point to another object. /FinalData.java,Blank finals,Java allows the creation of blank finals, which are fields that are declared as final but are not given an initialization value.The blank final must be initialized before it is used.A blank final field inside a class can be differen
14、t for each object. /BlankFinal.java,final arguments,Java allows you to make arguments final by declaring them as such in the argument list.This means that inside the method you cannot change what the argument reference points to. /FinalArguments.java,final methods,There are two reasons for final met
15、hods:Prevent any inheriting class from changing its meaning.Efficiency.Any private methods in a class are implicitly final.Confusion. /FinalOverridingIllusion.java,final classes,When you say that an entire class is final, you state that you dont want to inherit from this class or allow anyone else to do so. /Jurassic.javaThe fields of a final class can be final or not.All methods in a final class are implicitly final.,To be continued,