Subclass references can be converted to superclass references but not vice
versa. The instanceof operator tests whether an object belongs to a particular
type.
R10.2 /**Constructs a bank account with a given interest rate. @param rate the interest rate */ public SavingsAccount(double rate) { interestRate = rate; }
Inherited methods. public void deposit(double amount) { . . . } public void withdraw(double amount) { . . . } public double getBalance() { . . . }
R10.3 Subclass references can be converted to superclass references but not vice versa. The instanceof operator tests whether an object belongs to a particular type. R10.4 a. Employee, Manager (Super, Sub) b. Polygon, Triangle (Super, Sub) c. GraduateStudent, Student (Sub, Super) d. Person, Student (Super, Sub) e. Employee, GraduateStudent (Super, Sub) f. BankAccount, CheckingAccount (Super, Sub) g. Vehicle, Car (Super, Sub) h. Vehicle, Minivan (Super, Sub) i. Car, Minivan (Super, Sub) j. Truck, Vehicle (Sub, Super) R10.5. a. R10.6. Object
Classroom
Person Employee
Instructor
Student
R10.7. Vehicle
Bicycle
Motorcycle
Car
Sedan
Coupe
SUV
Truck
Minivan
PickupTruck
R10.8. Student extends Person Professor extends Employee TeachingAssistant extends Professor Employee Extends Person Secretary extends Employee DepartmentChair extends Professor Janitor extends Employee SeminarSpeaker extends Professor Person extends Object Course extends Object Seminar extends Course Lecture extends Course ComputerLab extends Course R10.9. All – {a,c} R10.10. a. Call the constructor of the superclass b. Reference to superclass, example: super.method() The constructor is a special method. a. Call the constructor parameters of the same class b. Reference to the fields of the same class Both super and this return a reference to a class. Super return a reference to a superclass, this return a reference to the same class. R10.11. Both R10.13. Shallow copy Copying only the reference to an object.
Object.clone method copies the references to the cloned object and does not clone the objects to which they refer. Such a copy is called a shallow copy. If the class does not implement the Cloneable interface, a CloneNotSupportedException is thrown. Subclasses should redefine this method to make a deep copy. R10.14. Private Public Static Are constants R10.16. No. R10.17. Because any user can change its value without restrictions. Yes, static public variables are so dangerous because can affect many objects.
P10.10. import java.awt.Rectangle; import java.util.Scanner;
public class Square extends Rectangle{ public Square(int x, int y, int length){ super(x,y,length,length); } public void setSize(int length){ super.setSize(length,length); } /** * * @param args */ public int getArea(){ return super.width*super.height; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int x; int y; int length; Square S = new Square(20,20,10);
System.out.println("Insert x: "); x = in.nextInt(); System.out.println("Insert y: "); y = in.nextInt(); System.out.println("Insert length: "); length = in.nextInt(); S.setLocation(x, y); S.setSize(length); System.out.println(S.toString()); System.out.println("Area: " + S.getArea()); } }