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

(#1 - #6) True or False. Explain your answers 1. You can define constructors for

ID: 3595404 • Letter: #

Question

(#1 - #6) True or False. Explain your answers

1. You can define constructors for a Java interface.

2. Classes implement interfaces.

3. Classes extend interfaces.

4. A class that implements an interface can include methods that are not required by that interface.

5. A class that implements an interface can leave out methods that are required by an interface.

6. You can instantiate objects of an interface.

7. A friend of yours is having trouble instantiating an object of type FigureInterface. His code includes the statement:

FigureInterface myFig = new FigureInterface (27.5);

What do you tell your friend?

Explanation / Answer

1. You can define constructors for a Java interface. - False

Exp: Java interfaces cannot have constructors becuase constructors cannot be instantiated but they can be implemented.

2. Classes implement interfaces. - True

Exp: Java classes implement the interfaces and extend the classes. Below are the basic rules please remember..

Class can extend another class

Class can implement interface

Interface can extend implement, but cannot extend class

3. Classes extend interfaces. - False

Exp: Classes cannot extend interfaces but classes can extend another class

4. A class that implements an interface can include methods that are not required by that interface. - True

Exp: A class can have methods that are not required by that interface, which means it can have additional method that are not declared in interface. But a class must override all the methods that are declared in interface.

5. A class that implements an interface can leave out methods that are required by an interface.- False

Exp: A class can leave out methods that are not required by that interface, it is not mandatory to have additional methods which not declared in interface. But a class must override all the methods that are declared in interface.

6. You can instantiate objects of an interface. - False

Exp: Interface can never be instantiated as it will not have constructor. Interfaces are only meant to be implemented by any class or to be extended by any other interfaces.

7. A friend of yours is having trouble instantiating an object of type FigureInterface. His code includes the statement:

FigureInterface myFig = new FigureInterface (27.5);

Exp: There are Four possible erros because of which your friend is not able to instantiate the type FigureInterface.

First one:

The type FigureInterface may be an interface, which cannot be instantiated because as I already explained above interfaces won't have constructor ther are meant to be implemented by class or to be extended by any other interface. If this is the case tell him not to try to instantiate interface.

Second one:

It is not mandatory that the class name contains the term interface, it should be an interface. It can be class also.

Assuming this we will move on to the next possible mistake.

The type FigureInterface may be a class which is not having the constructor with the parameter of type double.

If this is the case, tell him to define one constructor with double type inside the the class FigureInterface as shown below:

FigureInterface (double d){

//Some code

}

Third one:

The type FigureInterface might be an abstract class, which can have a constructor but cannot be instantiated using the keyword new.

Tell him not to instantiate abstract class never ever, rather to extend in some other class and give a call to super(27.5) from the constructor of the class which is extending FigureInterface.

Fourth one:

The constructor of the class FigureInterface might be having the access restriction. For suppose if a costructor is defined with modifier private then call to the constructor can be done only in the same class, but from other classes we can't instantiate. This rule will be followed for all the access modfiers according to their scope. So tell him to check for the access modifier of the constructor and to do the code modifications according to that.