Chapter 8 How to work with inheritance 1 You often need to k
Chapter 8: How to work with inheritance
1. You often need to know the ____________________ of the Java API to work with its classes and subclasses.
2. All objects have access to the methods of the ____________ class.
3. If two classes need to provide for some common elements, you can define those elements in a ____________.
4. If you want a member of a class to be accessible from a class that inherits it but not from other classes, you should declare it with the ____________ access modifier.
5. To call a constructor or method of a superclass from a subclass, you use the ____________ keyword.
6. A class that can be inherited by another class but that can’t be instantiated is called ____________ class.
7. A class that can’t be inherited by another class is called ____________ class.
Inheritance hierarchy 7-2
8. (Refer to inheritance hierarchy 7-2.) Write the code for the WaterCraft class. This class should include instance variables named id (int), type (string), and length (double) that can only be accessed by the class. Code a single constructor that assigns default values to the instance variables, and code get and set methods for the instance variables.
9. (Refer to inheritance hierarchy 7-2.) Write the code for the Sailboat class. This class should extend the WaterCraft class by adding an instance variable named sails (int) and get and set methods for this variable. It’s constructor should initialize the instance variables defined by the WaterCraft class by calling the constructor of that class, and it should assign a default value to the sails instance variable.
10. (Refer to inheritance hierarchy 7-2.) Assume that you want to add a static variable named count (int) to the WaterCraft class that can be accessed by the Sailboat class but not by other classes. Code the declaration for this variable so that it’s initialized to a value of 0.
11. (Refer to inheritance hierarchy 7-2.) Code a public method for the WaterCraft class that overrides the toString method of the Object class. This method should return a string that contains the id, type, and length variables in this format:
ID: 1234
Type: Sailboat
Length: 19\'
12. (Refer to inheritance hierarchy 7-2.) Code a public method for the Sailboat class that overrides the toString method of the WaterCraft class. This method should return a string that contains the information returned by the toString method of the WaterCraft class appended with the number of sails in this format:
Sails: 3
13. (Refer to inheritance hierarchy 7-2.) Assume that you have created a Sailboat object named s and assigned it to a WaterCraft object named w. Write a statement that will print the type of the WaterCraft object to the console.
14. (Refer to inheritance hierarchy 7-2.) Assume that you have created a WaterCraft object named w. Write an if clause that will check if the object is a Sailboat object, using the easiest technique available.
15. (Refer to inheritance hierarchy 7-2.) Code an equals method for the WaterCraft class that overrides the equals method of the Object class. This method should accept an Object object and return a boolean that indicates if the object is a WaterCraft object with the same id, type, and length values as the current object.
16. (Refer to inheritance hierarchy 7-2.) Assume that the WaterCraft class has been defined as abstract. Add an abstract method to this class named getDisplayText that accepts no parameters and returns a string.
17. (Refer to inheritance hierarchy 7-2.) Write the code for a method in the Sailboat class that overrides the abstract getDisplayText method in the WaterCraft class (see problem 9). The value that’s returned by this method should include the length and type fields from the WaterCraft class and the sails field from the Sailboat class formatted like this:
19.0\' Sailboat with 3 sails
Declare this method so that it can’t be overridden by any class that inherits this class.
WaterCraft Sailboat CanoeSolution
From 8 to 15:
public class InheritExample {
public static void main(String[] args) {
// 13.
WaterCraft w = new WaterCraft();
Sailboat s = new Sailboat();
w = s;
System.out.println(w.getClass());
// 14
if (w instanceof Sailboat) {
System.out.println(\"w refers sailboat\");
} else {
System.out.println(\"w not refers sailboat\");
}
}
}
public class WaterCraft {
private int id;
private String type;
private double length;
protected static int count;
/**
* @param id
* @param type
* @param length
*/
public WaterCraft() {
this.id = 0;
this.type = \"\";
this.length = 0.0d;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type
* the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length
* the length to set
*/
public void setLength(double length) {
this.length = length;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return \"id: \" + id + \"\ type: \" + type + \"\ length: \" + length;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
if (o instanceof WaterCraft) {
WaterCraft w = (WaterCraft) o;
if (this.getId() == w.getId() && this.getLength() == w.getLength()
&& this.getType() == w.getType())
return true;
else
return false;
} else
return false;
}
// 15
}
public class Sailboat extends WaterCraft {
private int sails;
/**
* @param sails
*/
public Sailboat() {
super();
super.count = 0;
this.sails = 0;
}
/**
* @return the sails
*/
public int getSails() {
return sails;
}
/**
* @param sails
* the sails to set
*/
public void setSails(int sails) {
this.sails = sails;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + \"\ Sails: \" + sails;
}
}



