Abstract Classes and Interface Questions
- What is one advantage of interfaces?
Answer:
It allows you to take radically different clases and given them a common characteristic.
- What is the main difference between the methods of an abstract class and an interface?
- What is the main difference between the methods of an abstract class and an interface?
Answer:
An abstract class can contain both abstract and non-abstract methods. An interface can only contain abstract methods.
- What are 2 characteristics of interface methods?
Answer:
interface methods are 1) abstract and 2) public.
- What are the 3 characteristics of interface variables?
Answer:
They are
- public
- static and
- final.
In other words, interfaces can declare only constants, and not instance variables.
- How did the reference type interface change in Java SE 8 when compared to Java SE 7?
Answer:
Java SE 8 introduced significant changes to reference type interfaces compared to Java SE 7, adding new features and capabilities:
- Static Methods:
- Java SE 7: Interfaces could only have abstract methods, meaning they needed an implementation in a concrete class.
- Java SE 8: Interfaces can now have static methods that provide utility functionality without requiring implementation in a class. This promotes code reuse and organization, especially for common operations related to the interface.
- Default Methods:
- Java SE 7: Interfaces could only define abstract methods, leaving implementation entirely to concrete classes.
- Java SE 8: Interfaces can now have default methods with optional implementations. These methods provide a default behavior that concrete classes can inherit or override if needed. This allows for consistent behavior across implementations while enabling flexibility for specific needs.
- Functional Interfaces:
- Java SE 7: Interface functionality was primarily for defining contracts and behavior inheritance.
- Java SE 8: The concept of functional interfaces was introduced. These interfaces have a single abstract method, making them suitable for representing operations like functions or lambdas. This enabled the use of lambda expressions and functional programming paradigms in Java.
- Improved Type Safety:
- Java SE 7: Interfaces didn't have explicit type information for their methods.
- Java SE 8: Interfaces can now use generics to specify type parameters for their methods, improving type safety and flexibility.
- Enhanced Java Collections Framework:
- Java SE 7: Collections lacked methods directly utilizing lambdas or streams.
- Java SE 8: New methods were added to many collection classes that leverage lambdas and streams for concise and efficient data processing. These methods often utilize default methods defined in the corresponding interfaces.
Overall, these changes in reference type interfaces in Java SE 8 significantly enhanced their capabilities and made them more versatile tools for developers. The addition of static and default methods, functional interfaces, and improved type safety allows for more expressive, reusable, and efficient code.
The only thing an interface can extend is another interface.
- Can an interface implement another interface?
Answer:
No. An interface can extend another interface but cannot implement another interface or class.
- Does a class that implements an interface have direct access to its constants?
Answer:
Yes. Any class that implements an interface has direct access to the constants.
- What is true about any variable declared in an interface?
Answer:
The variable is a public constant.
- How many access control levels do data members of a class have?
Answer:
- private
- default also known as "package"
- protected
- public
A top-level class can use just 2 of the 4 access control levels which are 1) public and 2) default.
- What are the 2 different access issues?
Answer:
- Whether the "method code" in one class can access a member of another class.
- Whether a subclass can inherit a member of its superclass.