[ Pobierz całość w formacie PDF ]
Q' Polymorphism applies to overriding, not to overloading
Q' Object type determines which overridden method is used at runtime.
Q' Reference type determines which overloaded method will be used at
compile time.
Instantiation and Constructors (Sun Objectives 6.3 and 1.3)
Q' Objects are constructed:
Q' You cannot create a new object without invoking a constructor.
Q' Each superclass in an object s inheritance tree will have a constructor called.
Q' Every class, even abstract classes, has at least one constructor.
Q' Constructors must have the same name as the class.
Q' Constructors do not have a return type. If there is a return type, then it is
simply a method with the same name as the class, and not a constructor.
Q' Constructor execution occurs as follows:
Q' The constructor calls its superclass constructor, which calls its superclass
constructor, and so on all the way up to the Object constructor.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 5
Composite Default screen
Chapter 5: Object Orientation, Overloading and Overriding, Constructors, and Return Types
44
Q' The Object constructor executes and then returns to the calling
constructor, which runs to completion and then returns to its
calling constructor, and so on back down to the completion of
the constructor of the actual instance being created.
Q' Constructors can use any access modifier (evenprivate!).
Q' The compiler will create a default constructor if you don t create any
constructors in your class.
Q' The default constructor is a no-arg constructor with a no-arg call to
super().
Q' The first statement of every constructor must be a call to eitherthis()
(an overloaded constructor) orsuper().
Q' The compiler will add a call tosuper()if you do not, unless you
have already put in a call tothis().
Q' Instance methods and variables are only accessible after the super
constructor runs.
Q' Abstract classes have constructors that are called when a concrete
subclass is instantiated.
Q' Interfaces do not have constructors.
Q' If your superclass does not have a no-arg constructor, you must create a
constructor and insert a call tosuper()with arguments matching those
of the superclass constructor.
Q' Constructors are never inherited, thus they cannot be overridden.
Q' A constructor can be directly invoked only by another constructor (using
a call tosuper()orthis()).
Q' Issues with calls tothis():
Q' May appear only as the first statement in a constructor.
Q' The argument list determines which overloaded constructor is called.
Q' Constructors can call constructors can call constructors, and so on, but
sooner or later one of them better callsuper()or the stack will explode.
Q' this()andsuper()cannot be in the same constructor. You can
have one or the other, but never both.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 5
Composite Default screen
Two-Minute Drill
45
Return Types (Sun Objectives 1.4)
Q' Overloaded methods can change return types; overridden methods cannot.
Q' Object reference return types can acceptnullas a return value.
Q' An array is a legal return type, both to declare and return as a value.
Q' For methods with primitive return types, any value that can be implicitly
converted to the return type can be returned.
Q' Nothing can be returned from avoid, but you can return nothing. You re
allowed to simply sayreturn, in any method with a void return type, to
bust out of a method early. But you can t return nothing from a method with
a non-voidreturn type.
Q' For methods with an object reference return type, a subclass of that type can
be returned.
Q' For methods with an interface return type, any implementer of that interface
can be returned.
Color profile: Generic CMYK printer profile
CertPrs8(SUN) / Sun Certified Programmer & Developer for Java 2 Study Guide / Sierra / 222684-6 / Chapter 5
Composite Default screen
Chapter 5: Object Orientation, Overloading and Overriding, Constructors, and Return Types
46
SELF TEST
The following questions will help you measure your understanding of the material presented in this
chapter. Don t even think about skipping this test. You really need to see what the questions on the
exam can be like, and check your grasp and memorization of this chapter s topics.
Encapsulation, IS-A, HAS-A (Sun Objective 6.1)
1. Given the following,
[ Pobierz całość w formacie PDF ]