Interfaces

Ace your homework & exams now with Quizwiz!

Can you have final and abstract in a method signature?

No. final on a method means you cannot override the method. Abstract methods are meant to be overridden since they have no implementation.

Can an interface and an abstract class be in the same file?

Nope.

What is an overloaded method?

Overloaded methods accept different lists of arguments. The argument lists can differ by: - Changes in the number of parameters that are accepted - Changes in the types of parameters that are accepted - Changes in the RETURN type

What would happen if a class implemented the below code? public interface Herbivore { public int eatPlants(int quantity); } public interface Omnivore { public void eatPlants(); }

The compiler would treat both methods as different methods since it is overloaded and both would need to be implemented. So if a method in two interfaces has the same name but different parameters, they must both be implemented. If a method has the same signature then only one of them need to be implemented. What about if they have different implementations? Interfaces only have ABSTRACT METHODS BUDDY.

What are interface variables assumed to be?

public, final, and static

Can a abstract class extend an interface?

No

Are all interface methods public, including default methods?

Yes

Can a abstract class implement multiple interfaces?

Yes

What will the compiler automatically convert this to: public interface CanSwim { int MAXIMUM_DEPTH = 100; final static boolean UNDERWATER = true; public static final String TYPE = "Submersible"; }

public abstract interface CanSwim { public static final int MAXIMUM_DEPTH = 100; public static final boolean UNDERWATER = true; public static final String TYPE = "Submersible"; }

What are the 4 interface method rules?

1. A default method may only be declared within an interface and not within a class or abstract class. 2. A default method must be marked with the default keyword. If a method is marked as default, it must provide a method body. 3. A default method is not assumed to be static, final, or abstract, as it may be used or overridden by a class that implements the interface. 4. Like all methods in an interface, a default method is assumed to be public and will not compile if marked as private or protected.

Name two rules for inheriting an interfaces

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.

Name 5 rules for defining an interface

1. Interfaces cannot be instantiated directly. 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, and they must include the abstract modifier in their definition. Therefore, marking an interface as private, protected, or final will trigger a compiler error, since this is incompatible with these assumptions. (This rule does not apply to inner interfaces) 5. All nondefault methods in an interface are assumed to have the modifiers abstract and public in their definition. Therefore, marking a method as private, protected, or final will trigger compiler errors as these are incompatible with the abstract and public keywords.

Rules to remember about static interface methods

1. Like all methods in an interface, a static method is assumed to be public and will not compile if marked as private or protected. 2. To reference the static method, a reference to the name of the interface must be used.

What is a default interface method?

A default method is a method defined within an interface with the default keyword in which a method body is provided. Contrast default methods with "regular" methods in an interface, which are assumed to be abstract and may not have a method body.

Why would java include default methods anyway? Give a real word scenario.

Basic: It is so people who have previously implemented your interface, wont have to update their code due to you breaking it by adding another abstract method. Default methods allow clients codes not to break and gives them the option to update their code on their own Imagine you have an interface that is shared among dozens or even hundreds of users that you would like to add a new method to. If you just update the interface with the new method, the implementation would break among all of your subscribers, who would then be forced to update their code. In practice, this might even discourage you from making the change altogether. By providing a default implementation of the method, though, the interface becomes backward compatible with the existing codebase, while still providing those individuals who do want to use the new method with the option to override it.

Why must an interface be public or default?

Because classes should extend interfaces. Private and protected would not allow this. Its not really default if you leave out the access modifier. The modifier will just assume that it is public.

Why doesnt this compile? public interface Carnivore { public default void eatMeat(); // DOES NOT COMPILE public int getRequiredFoodAmount() { // DOES NOT COMPILE return 13; }

Because the 1st method doesnt have any implementation and its a default method. The second one doesnt compile because it wasnt declared a default method. If it has a body it must be declared default method within an interface.

What keywords does the compiler assume for interfaces methods?

It assumes public and abstract even if you do not put it there.

What is output of this code: 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) { System.out.println(new Cat().getSpeed()); } }

It will not compile because two interfaces have the same method and signature for a default method.

Can a interface implement another interface?

No

Can a default interface method be private or protected? Can it be abstract or final or static?

No because it is meant to be implemented. No because abstract means there is no body which a default method has. final on a method means you cannot extend it. They cannot be static.

If a abstract class implements a interface, is the abstract class required to implement the interfaces abstract methods (since an abstract class can have both abstract and concrete methods)?

No its not required to implement them.

Can two interfaces be written in the same file?

No, compile error.

Are concrete classes forced to implement default interface methods?

No, they can override them if they want.

Is a static method defined in an interface inherited when a class implements it?

No, you must reference the name of the interface (interface.staticMethod) to use it.

public interface Herbivore { public void eatPlants(); } public interface Omnivore { public void eatPlants(); public void eatMeat(); } What happens when a class implements these ?

Remember that interface methods in this example are abstract and define the "behavior" that the class implementing the interface must have. If two abstract interface methods have identical behaviors—or in this case the same method signature— creating a class that implements one of the two methods automatically implements the second method. In this manner, the interface methods are considered duplicates since they have the same signature.

What happens if a concrete class does not override a default interface method?

The The default implementation will be used. A default interface method's body is basically concrete.

How do you write a default concrete method? How do you write a default interface method? Whats the difference?

The difference is that a default interface method uses the actual keyword "default" but its access modifier is actually always public. The concrete default method cannot use the keyword default. It has to use nothing for it to be default. Default Interface: public default int num() { return 5; } Class default method int num() { return 5; }

If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error. Whats the exception to this rule ?

The exception is if you decide to override the duplicated method within the class. This removes the ambiguity.

Explain why each of these do not compile: public interface CanDig { private int MAXIMUM_DEPTH = 100; protected abstract boolean UNDERWATER = false; public static String TYPE;}

The first example, MAXIMUM_DEPTH, doesn't compile because the private modifier is used, and all interface variables are assumed to be public. The second line, UNDERWATER, doesn't compile for two reasons. It is marked as protected, which conflicts with the assumed modifier public, and it is marked as abstract, which conflicts with the assumed modifier final. Finally, the last example, TYPE, doesn't compile because it is missing a value. Unlike the other examples, the modifiers are correct, but as you may remember from Chapter 4, you must provide a value to a static final member of the class when it is defined

How do static interface methods work?

The method getJumpHeight() works just like a static method as defined in a class. In other words, it can be accessed without an instance of the class using the Hop.getJumpHeight() syntax. Also, note that the compiler will automatically insert the access modifier public since all methods in interfaces are assumed to be public.

Define polymorphism in a simple sentence.

The property of an object to take on many different forms.

Which one will compile? public interface CanFly { void fly(int speed); abstract void takeoff(); public abstract double dive(); } public abstract interface CanFly { public abstract void fly(int speed); public abstract void takeoff(); public abstract double dive(); }

They both will since some keywords are assumed with interfaces.

Where would this code work? Explain why it works. public default int num(int x){ return x; }

This code would work within an interface. This is basically defining a default method in an interface which is completely different from a default access modifier in a class or abstract class. Look up flashcard that has default method definition for interfaces.

An interface can override another interfaces default method and make it abstract. True or False?

True

What would happen if a class implemented these interfaces: public interface Herbivore { public int eatPlants(); } public interface Omnivore { public void eatPlants(); }

Unfortunately, if the method name and input parameters are the same but the return types are different between the two methods, the class or interface attempting to inherit both interfaces will not compile. The reason the code doesn't compile has less to do with interfaces and more to do with class design, as discussed in Chapter 4. It is not possible in Java to defi ne two methods in a class with the same name and input parameters but different return types. MAIN POINT: If two methods have the same name, same parameters but different return type, then the code will NOT compile.

Can a interface extend "multiple" interface?

Yes

Can a interface extend another interface?

Yes

Does the compiler show an exception if you try to implement two interfaces with incompatible methods? How about if you extend two interfaces with incompatible methods?

Yes and Yes.

Can a class be private in java?

Yes if they are a inner class. If it is a top level class then no. An inner class another way of saying a nested class. Keep in mind ALL inner classes can have private.

Is this allowed: private static int num = 50;

Yes it just means only this class can access the class variable(which is num)

If two interfaces have the same static methods, and a class implements them both, will it compile?

Yes it will compile because static methods are not inherited. When you use each static method you have to call them by referencing their parents as well.

Are class static methods inherited? Can they be override?

Yes they are inherited but they are not inside of interfaces. No they cannot be overridden. If they are, then the static method int he parent class is hidden.

Does this compile: public interface WalksOnTwoLegs {}

Yes, abstract is assumed by compiler.

Is it it good practice to write out the assumed keywords in interfaces? Why or why not?

Yes, it makes the code clearer to read

Will compiler throw error if a variable in an interface is set as private or protected or abstact?

Yes. A interface variable is always public final and static.

True or false: This: public void abstract method(); is the same as this: public abstract void method(); If false, which one is correct?

false, abstract must come before the return type.


Related study sets

Division with a one digit divisor

View Set

Chapter 6 (Anxiety Disorders) Pre/Post Chapter Quizzes

View Set

Chapter 9: Care Coordination and Continuity in Health Care Settings and the Community

View Set

Point-Slope Form of a Line: Assignment

View Set

HUN - Chapter 11 : The Fat-Soluble Vitamins: A, D, E, and K

View Set

Chapter 18 Mastering Microbiology

View Set