Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10) If you write a class called Car, but do not write a toString( ) method. Then

ID: 3834503 • Letter: 1

Question

10) If you write a class called Car, but do not write a toString( ) method. Then in main( ) you create a Car object like this:

//assume a constructor exists that takes these parameter types
Car myCar = newCar(“Toyota”, “Corolla”, 1994, “blue”);

System.out.println(“This is myCar information: “ + myCar.toString( ) );

Explain what will happen and why that will happen.

11) Suppose you have a class called BankAccount that is abstract. Then you have a concrete class called SavingsAccount that extends BankAccount.
BankAccount has an abstract method called withdrawFunds(double amountToWithdraw) and a non-abstract method called depositFunds(double amountToDeposit ).


SavingAccount does NOT have its own version written for depositFunds(double);

There are four questions here.

a)Is this a valid declaration?

        BankAccount myAccount = new SavingsAccount( );

answer:

b)Is this a valid declaration?

       SavingsAccount myAccount = new BankAccount( );

answer:

Suppose you have this code:

c)

     SavingsAccount myAccount = new SavingsAccount( );

     myAccount.depositFunds(200);

What code (from which class) will execute as a result of that method call?

answer:

__________________________________________________________________

d)

   myAccount.withdrawFunds(100);

What code (from which class) will execute as a result of that method call?

answer:

Explanation / Answer

(10)
All classes in java implements Object class .
So toString() Function can be called on any type of object in java.
It is a valid statement.
toString() function returns textual representation of any object.
it will return string which represents the given object in text format.

(11)
a)Is this a valid declaration?

BankAccount myAccount = new SavingsAccount( );
      
Yes this is a valid declaration. As all saving accounts are bank accounts.

b)Is this a valid declaration?

SavingsAccount myAccount = new BankAccount( );
     
This is not a valid declaration as BankAccount is super class and also abstract class. we can not have object of type BankAccount.

Suppose you have this code:
c)
SavingsAccount myAccount = new SavingsAccount( );
myAccount.depositFunds(200);

What code (from which class) will execute as a result of that method call?

SavingsAccount doesnt have its own implementation for function depositFunds(). So depositFunds() of parent class, i.e BankAccount class will be implemented.

d)
myAccount.withdrawFunds(100);

What code (from which class) will execute as a result of that method call?
This statement will implement the fucntion withdrawFunds() that is defined inside class SavingAccount.
as it is abstract fucntion , its implementationmust be there in SavingAccount class.

if you have any doubts, you can ask in comment section.