ebook img

Java: Classes in Java Applications PDF

118 Pages·2017·3.796 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Java: Classes in Java Applications

David Etheridge Java: Classes in Java Applications – An Introduction to Java Programming 2 Download free eBooks at bookboon.com Java: Classes in Java Applications – An Introduction to Java Programming © 2009 David Etheridge & Ventus Publishing ApS ISBN 978-87-7681-495-3 3 Download free eBooks at bookboon.com Java: Classes in Java Applications Contents Contents 1. Using the Java Application Programming Interface 7 1.1 Documentation in Developer-Written Java Classes 7 1.2 Documentation in the Java Application Programming Interface 15 2. Flow Control 19 2.1 Introduction to Flow Control 19 2.2 Sequential Flow 19 2.3 Conditional Flow 19 2.4 Making Decisions 20 2.5 Controlling the Repetition of Blocks of Code 33 2.6 Deciding Which Construct to Use 41 2.7 Branching Statements 42 2.8 Handling Exception Objects 43 3. Extending Classes by Means of Inheritance 45 3.1 What Does Inheritance Mean? 45 3.2 Overriding and Hiding Methods in a Subclass 53 3.3 Invoking a Parent Class Constructor from a Subclass Constructor 56 3.4 fi nal and abstract Classes 57 3.5 What Does Type Compatibility Mean? 60 www.sylvania.com We do not reinvent the wheel we reinvent light. Fascinating lighting offers an infinite spectrum of possibilities: Innovative technologies and new markets provide both opportunities and challenges. An environment in which your expertise is in high demand. Enjoy the supportive working atmosphere within our global group and benefit from international career paths. Implement sustainable ideas in close cooperation with other specialists and contribute to influencing our future. Come and join us in reinventing light every day. Light is OSRAM 4 Click on the ad to re ad more Download free eBooks at bookboon.com Java: Classes in Java Applications Contents 3.6 Virtual Method Invocation 64 3.7 Controlling Access to the Members of a Class 65 3.8 Summary of Inheritance 68 4. Errors in Java Programmes 70 4.1 Categories of Error 70 4.2 What are Unexpected Error Conditions? 71 4.3 Checked Exceptions 73 4.4 try … catch … fi nally Blocks 82 4.5 Throwing Exceptions 84 4.6 Exceptions in the Themed Application 87 360° 4.7 Summary of Exceptions 92 . 5. Java Interfaces 93 thinking 5.1 What is a Java Interface? 93 360° 5.2 Defi ning and Implementing a Java Interface 97 5.3 The Role of Interfaces as a Means to Introduce Behaviour to a Class 101 . 5.4 Interfaces as Types 103 thinking 5.5 Summary of Java Interfaces 106 6. Grouping Classes Together in a Java Application 107 6.1 An Introduction to Java Packages 107 360° . thinking 360° . thinking Discover the truth at www.deloitte.ca/careers Discover the truth at www.deloitte.ca/careers © Deloitte & Touche LLP and affiliated entities. Discover the truth at www.deloitte.ca/careers © Deloitte & Touche LLP and affiliated entities. © Deloitte & Touche LLP and affiliated entities. 5 Discover the truth at www.deloitte.ca/careers CClliicckk oonn tthhee aadd ttoo rree aadd mmoorree Download free eBooks at bookboon.com © Deloitte & Touche LLP and affiliated entities. Java: Classes in Java Applications Contents 6.2 Creating Packages 107 6.3 Naming Convention 111 6.4 Packages in the Java Language 111 6.5 Using and Accessing Package Members 114 6.6 Compiling and Running Package Members 117 We will turn your CV into an opportunity of a lifetime Do you like cars? Would you like to be a part of a successful brand? Send us your CV on We will appreciate and reward both your enthusiasm and talent. www.employerforlife.com Send us your CV. You will be surprised where it can take you. 6 CCCllliiiccckkk ooonnn ttthhheee aaaddd tttooo rrreee aaaddd mmmooorrreee Download free eBooks at bookboon.com Java: Classes in Java Applications Using the Java Application Programming Interface 1. Using the Java Application Programming Interface Chapter One takes examples from the Java Application Programming Interface (API) and the themed application in order to emphasise the critical importance of documentation. The examples are used to show how documentation is organised in the API and how it is inserted into developer’s code. 1.1 Documentation in Developer-Written Java Classes Previous chapters include a number of examples of classes (or partial classes) from the themed application, the main purpose of which is to illustrate programming concepts. It should not have escaped the notice of the reader that these examples often include material other than Java statements, usually in a non-bold font. The inclusion of such material raises a question: what is the purpose of documentation is a class definition? Perhaps one way to address this question is to consider figures 1.1 and 1.2 below. Source: http://www.cvr-it.com/PM_Jokes.htm Figure 1.1 A view of an application as explained by the users 7 Download free eBooks at bookboon.com Java: Classes in Java Applications Using the Java Application Programming Interface Source: http://www.cvr-it.com/PM_Jokes.htm Figure 1.2 How the programme was documented While it is obvious that the images are meant to be amusing, they make a serious point: there is nothing worse than trying to maintain code written by someone else if there is little or no documentation. Documentation is an integral part of a Java class in that it is used, inter alia, to explain the purpose of members of the class (to the development team) and show consistency with the class diagram. 1.1.1 Documentation in the Themed Application The class diagrams of two of the classes of the themed application are shown in the figure on the next page. 8 Download free eBooks at bookboon.com Java: Classes in Java Applications Using the Java Application Programming Interface Figure 1.3 The Member and MembershipCard classes of the themed application The classes are related by a ‘has a’ relationship, as shown in the next figure. Figure 1.4 Each Member ‘has a’ MembershipCard The source code for the Member class follows next. Some of the Java statements, white space and single- line comments are omitted for the sake of brevity so that the learner can concentrate on Java documentation, rather than on the logic of the code. Documentation blocks are displayed in a bold font. 9 Download free eBooks at bookboon.com Java: Classes in Java Applications Using the Java Application Programming Interface /** * The purpose of the Member class is … < to be completed by the developer >. * @author David M. Etheridge. * @version 1.0, dated 29 October 2008. */ public class Member { private String firstName; private String lastName; private String userName; private String password; private int membershipNumber; private String noOfCards; private MembershipCard card; /** * This constructor is used to initalise the first four attributes. * @param fName The member's first name. * @param lName The member's last name. * @param uName The member's user name. * @param pWord The member's password. */ AXA Global Graduate Program Find out more and apply 10 CCCClllliiiicccckkkk oooonnnn tttthhhheeee aaaadddd ttttoooo rrrreeee aaaadddd mmmmoooorrrreeee Download free eBooks at bookboon.com

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.