Chap 9 Interface And Package Flashcards
What’s difference between abstract class and an interface?
A class can implement more than one interface, but can only inherit one superclass.
What is value of an interface
Can specify what a class must do, but not how.
A consistent way to invoke similar behavior in different objects
Specifies the method, but body is blank
How are interface created
Access interface name {
Method one();
Method two();
}
Body is blank
Write a class that implements an interface class IntyFc
class TestClass implements IntyFc
{}
The methods inside must be public
If implementing an interface, do the methods inside must be public?
Yes
Can you create a reference variable of the interface type and call the method defined in the subclass?
Yes. But only the ,ethics that were defined in the interface
Can you implement an interface but not implement the methods required?
Yes but you have to label it abstract
What access level must interfaces be?
Either public, or defaulted to its package level
Can you declare an interface within a class?
Yes, called a nested interface
Can interfaces extend other interfaces?
Yes
Explain default interface methods
An interface can define the body of a method as its default, if it’s not overloaded in the class. It’s useful if a popular interface adds a new method requirement, having the default won’t break the subclasses if they don’t update.
What is the defining difference between an interface and a class
Interface cannot maintain state information, a class can.
Example code: define a default method in an interface
default String methodOne ()
{ return String;
}
Can an implementing class override a default method?
Yes, it’s common
Hierarchy of inheriting interface methods
Class implementation takes priority
Then an inheriting class
If implement two interfaces with same method name, it’ll throw error