Editing 1188: Bonding
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 19: | Line 19: | ||
#The Ball class [http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html extends] [http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html Throwable], making it possible to use an instance of Ball in [http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html exception handling]. In English, this means "a Ball is a kind of Throwable object". | #The Ball class [http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html extends] [http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html Throwable], making it possible to use an instance of Ball in [http://docs.oracle.com/javase/tutorial/essential/exceptions/handling.html exception handling]. In English, this means "a Ball is a kind of Throwable object". | ||
#The P class, representing a Person, which contains the following members (attributes): | #The P class, representing a Person, which contains the following members (attributes): | ||
− | + | :*a [http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html class variable] 'target' to point to another P to aim a Ball at. | |
− | + | :*a [http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html constructor] 'P' (in Java the constructor always has the same name as the class) used to create an instance of P and initialize its state (with a target). The keyword [http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html this] refers to the current instance of P. | |
− | + | :*a [http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html method] 'aim' that takes an instance of Ball named 'ball' as a parameter. This contains the code to actually throw, catch, and pass the ball onto the target. | |
− | + | :*a [http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html static] method 'main' which is called when executing this class. This is the code that starts the whole process. | |
The program executes in the following order: | The program executes in the following order: | ||
Line 28: | Line 28: | ||
##An instance of P named 'parent' is created without a target ({{w|Nullable_type|null}}) using the 'new' keyword. | ##An instance of P named 'parent' is created without a target ({{w|Nullable_type|null}}) using the 'new' keyword. | ||
##Another instance of P named 'child' is created with 'parent' as its target. | ##Another instance of P named 'child' is created with 'parent' as its target. | ||
− | ##The parent's target is assigned to be the child. | + | ##The parent's target is assigned to be the child. |
− | + | ##An instance of Ball is created and passed as a parameter to the aim method of parent. | |
− | #The aim method first sets up a [http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html try] block to handle exceptions | + | #The aim method first sets up a [http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html try] block to handle exceptions. |
− | #Next, the Ball instance 'ball' is [http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html thrown]. This signals an exception situation | + | #Next, the Ball instance 'ball' is [http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html thrown]. This signals an exception situation. |
− | #In the catch block | + | #The thrown exception (= the Ball instance) is handled in the [http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html catch] block with the matching exception class. |
− | #The | + | #In the catch block the aim method of the target of the P instance is called with the Ball instance (now referred to as 'b'). |
+ | #The code continues with step 6, but the current class instance ('this') has changed from parent to child (or vice versa). | ||
==Transcript== | ==Transcript== |