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

Q1. Define an interface with its syntax. Give differences between an abstract cl

ID: 3590560 • Letter: Q

Question

Q1. Define an interface with its syntax. Give differences between an abstract class and an interface.

Q2.   Consider the following problem description and write the Java code.

(a) Create two interfaces Interest and Balance.

(b) Define in each interface one method used in order to get the rate of interest and the balance of the bank account implementing them respectively.

(c) A BankAccount class that implements these two interfaces.

Note: use the appropriate instance variables, constructors, and methods.

     (d). Call both the methods in the main() method.

Q3.

(a) Define Inheritance. Give an Example.

(b) Also give the structure/definition of the base class from your example and a derived class stating the fields and methods of both the classes and the methods overridden in the base class. You may declare Abstract class as well if required.

Q4.

Consider the following code snippet:

public class Motorcycle extends Vehicle

{

   . . .

   public Motorcycle (int numberAxles)

   {

    super(numberAxles);

   }

}

What does the super keyword do?

Explanation / Answer

Below are some of the differences beetween abstract class and Interface:

1. Abstract class can contains the methods with implementation and only signatures, Interface contains only signatures of the methods.

2. A child class can extend only one abstract class, a child class can implement any number of interfaces.

3. Uses key word extends to use abstract class, implements keyword is used to use interface in any class.

Q2.   Consider the following problem description and write the Java code.

(a) Create two interfaces Interest and Balance.

(b) Define in each interface one method used in order to get the rate of interest and the balance of the bank account implementing them respectively.

Interest interface with method to get the rate of interest

public interface Interest {

public int getInterest();

}

Balance interface with method to get balance

public interface Balance {
public float getBalance();
}

(c) A BankAccount class that implements these two interfaces.

BankAccount.java implements both interfaces and defines methods getBalance and getInterest, main method is created to create BankAccount object and prints the balance and rate of interest

public class BankAccount implements Interest, Balance {

@Override

public float getBalance() {

return 1000;

}

@Override

public int getInterest() {

return 3;

}

public static void main(String[] args) {

BankAccount account =new BankAccount();

System.out.println("Balance Amount is "+account.getBalance()+"$");

System.out.println("Intereset Rate is "+account.getInterest()+"%");

}

}

Q3.

(a) Define Inheritance. Give an Example.

Inheritance is the property where a class can get all the behavior and properties of the parent class.

Example: Consider BankAccount class as a parent class which contains getBalance and getInterest methods, we can create subclass SavingsAccount which extends BankAccount class.

public class BankAccount{

public float getBalance() {

return 1000;

}

public int getInterest() {

return 3;

}

}

SavingsAccount.java extends BankAccount class, by doing this SavingsAccount object obtains all the properties of BankAccount class which is parent class. Main method is defined in Savings Account where savings account object is created and getBalance of BankAccount class is called.

public class SavingsAccount extends BankAccount{

public int getAccountNumber(){

return 1234;

}

public static void main(String[] args) {

SavingsAccount saving = new SavingsAccount();

System.out.println("Balance in savings Account "+saving.getBalance());

}

}

Q4.

Consider the following code snippet:

public class Motorcycle extends Vehicle

{ . . .

   public Motorcycle (int numberAxles)

   {

    super(numberAxles);

   }

}

super keyword here refers to the value of numberAxles variable of the parent class Vehicle. For example if numberAxles value in Vehicle is assigned as "10", if we create an object of Motorcycle numberAxles variable has value as "10".