• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/26

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

26 Cards in this Set

  • Front
  • Back

What is keywords are assumed (auto inserted) in this statement?




public interface canBurrow{


int MINIMUM_DEPTH = 2;


int getMaximumDepth();


}

Assumed keywords in bold.




public abstract interface canBurrow{


public static final int MINIMUM_DEPTH = 2;


public abstract int getMaximumDepth();


}

What is an interface include? What does function does it serve?

An interface is an abstract data type that defines a list of abstract public methods that any class implementing the interface must provide.




It may also include a list of constant variables and default methods.

What are the 5 rules for defining an interface?

The first three rules are from defining abstract classes.




1. Interaces cannot be directly instantiated with the keyword new.




2. An interface is not required to have any methods.




3. An interface may not be marked as final.




4. All top-level interfaces are assumed to have public or default access. (not rule not applied to inner interfaces)




5. All non-default methods in an interface are assumed to have the modifiers abstract and public in their definition.




Marking an interface or a method within an interface with private, protected, or final will trigger compile errors.



Why can't an interface or abstract classes/methods be marked with final?

By definition, interfaces and abstract classes/methods must be inherited and modified to be used.




The keyword final means that member cannot changed -- it's contradictory the definition of interfaces and abstract classes/methods.

Is the following valid?




private final interface canCrawl{


protected abstract double depth();


}

No, and for two reasons.




Interfaces must be either public or default access, and cannot be marked final.




Interface methods must only be public.



Is the following valid?




interface canCrawl{


double depth();


}

Yes.




If access modifiers are left off the compiler will automatically insert them.




after the compiler is finished, the code will look like this:




(default access) abstract interface canCrawl{


public abstract depth();


}



What are the two rules for inheriting an interface?




(what happens to the methods?)

1. An interface that extends another interface, as well as an abstract class that implements an interface , inherits all of the abstract methods as its own abstract methods.




2. The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all of the inherited abstract methods.

Is the following valid?




public interface canRun{}




public class Cheetah extends canRun{}

No.




A class may implement an interface, but not extend an interface




The only appropriate connection between class and interface is: class implements interface{}

Is the following valid?




public class Hyena{}




public interface HasFur extends Hyena{}

No.




An interface cannot extend a class. However, an interface may extend another interface.




The only appropriate connection between class and interface is: class implements interface{}

Is the following valid?




public interface canRun{}


public interface canJump{}




public class Cheetah implements canRun, canJump{}

Yes.




A class may implement multiple interfaces without issue.




A class may not extend multiple classes.

Is the following valid?




public interface Herbivore{int eatPlant();}


public interface Omnivore{void eatPlant();}


public class Bear implements Herbivore, Omnivore{


public int eatPlant(){


return 10;


}


public void eatPlant(){


System.out.println("nom nom");


}


}

No.




There's a compile error at the class declaration, and each of the class-method definitions.




If the method name and input parameters are the same but the return types are different, the class or interface attempting to inherit both interfaces will not compile.

Is the following valid?




public interface Herbivore{void eatPlant();}


public interface Omnivore{void eatPlant();}


public class Bear implements Herbivore, Omnivore{


public void eatPlant(){


System.out.println("nom nom"); }}

Yes.




The signatures for the two methods eatPlants() are compatible, so you can define a class that fulfills both interfaces simultaneously.

Is the following valid?



public interface Herbivore{


String eatPlant(int cnt);}


public interface Omnivore{


void eatPlant();}


public class Bear implements Herbivore, Omnivore{


public String eatPlant(int cnt){


return ("nom"+cnt+" times"); }


public void eatPlant(){ System.out.println("nom nom"); }


}

Yes.




If the method name is the same but the input parameters are different, there is no conflict because this is considered a method overload.



What does the compiler assume about interface variables?

Interface variables are assumed public, static and final and will be treated as such whether the keywords are present or not.




Anything but those keywords will throw a compiler error (private, protected, or abstract)

What scenarios are acceptable for a interface variable to be set?

An interface variable must be set when declared as it is assumed final.

Which of the following are valid interface variable declarations?




A. static int MAX_DEPTH = 100;


B. int MED_DEPTH = 100;


C. private int MIN_DEPTH = 100;


D. public static String TYPE;


E. String KIND = "Submersible";



A, B, E are valid




//must be public


B. int MED_DEPTH = 100;




// must be set when declared


D. public static String TYPE;

What is a default interface method?

A method defined within an interface which includes the default keyword, and defines a default implementation.




Keywords abstract, static, and final on a default method will throw a compiler error.




Class have the option to override the default method, but they are not required to do so. (contrast to an abstract method which has no body and is required to be implemented at first concrete class)

Is the following valid?




public interface IsWarmBlooded{


boolean hasScales();


public default double getTemp(){


return 10.0;}


}

Yes.




A default method is declared within an interface, it provides a method body, is declared public, and does not contain the keywords final, abstract, or static.

What are the access modifiers for these methods?




public interface IsWarmBlooded{


boolean hasScales();


default double getTemp(){


return 10.0;}


}

Both methods are public as interface methods are assumed public.




The keyword default refers to the default method body provided for classes. The first concrete class is not required to define getTemp() when implementing this interface. However, the first concrete class is required to define hasScales.

Is the following valid?




public abstract class IsWarmBlooded{


boolean hasScales();


public default double getTemperature(){


return 10.0;}


}

No.




A default method may only be declared within an interface.



Is the following valid?




public interface IsWarmBlooded{


boolean hasScales();


public default double getTemperature();


}

No.




A default method MUST provide a method body.




empty curly braces are accepted as a method body.

Is the following valid?




public interface IsWarmBlooded{


static void default hasScales( ){ }


abstract static default double getTemperature( ){


return 10.0;}


}

No.




A default method may not be marked abstract, final, or static as it may be either used or overridden by a class that implements the interface.

Do interface members require an instance to be invoked?
Instance variables do not, the are assumed static final (a class-constant).



Explicitly static instance methods also do not.




Abstract and Default interface methods do require an instance.

Is the following valid?




public interface Walk{


public default int getSpeed(){ return 5;} }


public interface Run{


public default int getSpeed(){ return 10;}}


public class Cat implements Walk, Run{


public static void main(String[] args){


new Cat().getSpeed();


}


}

No. error falls on the class definition.




The error is because the two default methods have the same name and signature and are being implemented into the same class. Java would does know which method to execute.




To correct, the class Cat could override the conflicting methods and create it's own definition.




public int getSpeed(){ return 1; }

Is the following valid?




public interface Hop{


static int getJumpHeight(){ return 8;}


}


public class Bunny implements Hop{


getJumpHeight();


}

No.




An interface's static methods must be referenced using the interfaces name.




Hop.getJumpHeight() is valid because it uses the interface's name to point Java in the right direction.

What are the three types of methods available in interfaces?

abstract (assumed, no method body allow)


default (method body required)


static (method body required, called with class reference)