Understanding Inheritance in Java Exercise A: Creating a ...

35 downloads 830 Views 167KB Size Report
Understanding Inheritance in Java This activity provides an exercise to enable you to create a basic java application that makes use of inheritance, ...
Understanding Inheritance in Java This activity provides an exercise to enable you to create a basic java application that makes use of inheritance, using the Food.java class from our previous activity and a new java class Apple.java. Exercise A: Creating a Basic Java Application Containing a Class In this activity, you will construct a basic Java application that utilizes a class (Food.java) and a class that inherits from our basis class (Apple.java). Our main class (AppleSnack.java) captures the interaction between these two classes (Food.java and Apple.java). In this activity, you will be provided with three java project files: 1. Food.java – contains the food class and objects 2. Apple.java – contains the apple food class, which inherits from the food class and objects 3. AppleSnack.java – main class The code for this activity needs to be entered into Netbeans for execution. The code may be typed directly into Netbeans, or you may import the existing .java file into the IDE. Creating this new project can be done in the following manner: 1. Create a new project in Netbeans from the file menu. 2. Please note that this project will have two classes (Food.java and Snack.java). 3. Type the provided AppleSnack.java code into your newly created Netbeans project. 4. Type the provided Food.java code into your Netbeans project by adding an additional file. 5. Type the provided Apple.java code into your Netbeans project by adding an additional file. 6. Execute the application code. The user input can be changed in the main class file. Required code file: Food.java, Apple.java and AppleSnack.java

Saylor URL: www.saylor.org/cs102 Sub-subunit 2.2.2

The Saylor Foundation

Saylor.org Page 1 of 2

Activity Questions As you work through these two exercises, please be sure to pay specific attention to the differences in coding structure between Food.java, AppleFood.java, and AppleSnack.java. Answer the following questions: 1. Which classes represent an inheritance relationship, and what is this relationship? 2. What method must be used to create the constructor for the Apple class? 3. What class keyword is used to denote that inheritance exists in java between classes?

Saylor URL: www.saylor.org/cs102 Sub-subunit 2.2.2

The Saylor Foundation

Saylor.org Page 2 of 2

Suggest Documents