Java Questions 11 - 20  «Prev  Next»

Abstract Classes and Interface Questions

  1. What is one advantage of interfaces?
    Answer:
    It allows you to take radically different clases and given them a common characteristic.
  2. What is the main difference between the methods of an abstract class and an interface?
  3. 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.
  4. What are 2 characteristics of interface methods?
    Answer:

    interface methods are 1) abstract and 2) public.
  5. What are the 3 characteristics of interface variables?
    Answer:
    They are
    1. public
    2. static and
    3. final.
    In other words, interfaces can declare only constants, and not instance variables.
  6. 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:
    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.


  7. Can an interface implement another interface?

    Answer:
    No. An interface can extend another interface but cannot implement another interface or class.
  8. 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.
  9. What is true about any variable declared in an interface?
    Answer:

    The variable is a public constant.
  10. How many access control levels do data members of a class have?
    Answer:
    1. private
    2. default also known as "package"
    3. protected
    4. public
    A top-level class can use just 2 of the 4 access control levels which are 1) public and 2) default.
  11. What are the 2 different access issues?
    Answer:
    1. Whether the "method code" in one class can access a member of another class.
    2. Whether a subclass can inherit a member of its superclass.

SEMrush Software