Lecture 5 (1)
Lecture 5 (1)
Programming
with Java II
Email: [email protected]
Lecture outcomes
• The protected Data and Methods
• Visibility Modifiers
• Polymorphism
• Dynamic Binding
• Casting Objects
Visibility increases
package p2;
public
protected -
default - -
private - - -
• The final modifier indicates that a class is final and cannot be a parent class.
• The String, StringBuilder, and StringBuffer classes are also final classes.
public final class A {
• The inheritance relationship enables a subclass to inherit features from its superclass with
additional new features.
• Every instance of a subclass is also an instance of its superclass, but not vice versa.
• For example, every student is a person object, but not every person object is a
student.
• For example, the toString() method is defined in the Object class and overridden in Person.
• The type that declares a variable is called the variable’s declared type.
• A variable of a reference type can hold a null value or a reference to an instance of the
declared type.
• The instance may be created using the constructor of the declared type or its subtype.
• The actual type of the variable is the actual class for the object referenced by the variable.
• Here o’s actual type is Person, because o references an object created using new Person().
• The statement Object o = new Person(), known as implicit casting, is legal because
an instance of Person is an instance of Object.
Dr. Mohamed K. Hussein 12
Casting Objects
• Consider the following statement: Person b = o;
• In this case a compile error would occur.
• Why does the statement Object o = new Person() work but Person b = o doesn’t?
• Even though you can see that o is really a Person object, the compiler is not clever enough to know
it.
• To tell the compiler that o is a Person object, use explicit casting.
• The syntax is similar to the one used for casting among primitive data types.
• Person b = (Person)o; // ExplicitDr.casting
Mohamed K. Hussein 13
instanceof
• For the casting to be successful, you must make sure that the object to be cast is an instance of the
subclass.
• If an object is not an instance of person, it cannot be cast into a variable of object.
• It is a good practice to ensure that the object is an instance of another object before attempting a
casting.
• This can be accomplished by using the instanceof operator.
Object myObject = new Person();
/** Perform casting if myObject is an instance of Circle */
if (myObject instanceof Person) {
System.out.println("The perso age is " + ((Person)myObject).getAge());
}
• This implementation checks whether two reference variables point to the same object
using the == operator.
• You should override this method in your custom class to test whether two distinct objects
have the same content.
fixed. +add(o: E) : void Appends a new element o at the end of this list.
Adds a new element o at the specified index in this list.
+add(index: int, o: E) : void
• Java provides the ArrayList Removes all the elements from this list.
+clear(): void
Returns true if this list contains the element o.
• The following statement creates an ArrayList +contains(o: Object): boolean
Returns the element from this list at the specified index.
and assigns its reference to variable cities. +get(index: int) : E
Returns the index of the first matching element in this
+indexOf(o: Object) : int
list.
• This ArrayList object can be used to store +isEmpty(): boolean
Returns true if this list contains no elements.
strings. +lastIndexOf(o: Object) : int
Returns the index of the last matching element in this
+remove(o: Object): boolean list.
• ArrayList<String> cities = new
+size(): int Removes the element o from this list.
ArrayList<String>();
+remove(index: int) : boolean Returns the number of elements in this list.
for (int i = People.size() - 1; i >= 0; i––) // Display the contents in the list in reverse order
System.out.println( “The age of the person? " + People.get(i).getAge() );
People.clear();
}
} Dr. Mohamed K. Hussein 21
Assignment 5
• The Account class is defined to model a bank account. An account class has:
• Add a field name of the String type to store the name of the customer.
• Other attributes include account number, balance, annual interest rate, and date created,
• A no-arg constructor that creates a default account.
• A constructor that constructs an account with the specified name, id, and balance.
• Add a data field named transactions whose type is ArrayList that stores the transaction for the
accounts. Each transaction include.
• The date of this transaction.
• The type of the transaction, such as 'W' for withdrawal, 'D’ for deposit.
• The amount of the transaction.
• The new balance after this transaction.
• Construct a Transaction with the specified date, type, balance, and description.
• Methods to deposit and withdraw funds that adds the transaction to the arrayList.
• Create two subclasses for checking and saving accounts.
• A checking account has an overdraft limit, but a savings account cannot be overdrawn.
Dr. Mohamed K. Hussein 22
Assignment 5
1. Draw the UML diagram for the classes and then implement them.
2. Write a test program that creates objects of Account, SavingsAccount, and CheckingAccount
and invokes their toString() methods.
3. Creates an Account with annual interest rate 1.5%, balance 1000, id 1122, and name Sarah .
4. Deposit $30, $40, and $50 to the account and withdraw $5, $4, and $2 from the account.
5. Print an account summary that shows account holder name, interest rate, balance, and all
transactions.