Abstract Classes and Interfaces Flashcards
16 Questions
100 Views

Abstract Classes and Interfaces Flashcards

Created by
@NiftySard6198

Questions and Answers

Is it correct that the class ABC is declared as abstract without any abstract methods?

True

Why does the class AbstractClass show a compilation error?

Because abstract methods must not have a body.

Which class is instantiable?

  • Class B (correct)
  • Class A
  • What must class B implement to avoid compilation errors?

    <p>Class B must implement inherited abstract method A.add() or else it must be declared as abstract.</p> Signup and view all the answers

    What will be the value of the 'result' variable if the program runs correctly?

    <p>result = 20235</p> Signup and view all the answers

    Can we write explicit constructors in an abstract class?

    <p>True</p> Signup and view all the answers

    What is the error in the code where abstractMethod is private?

    <p>Abstract methods can't be private.</p> Signup and view all the answers

    Can we declare protected methods in an interface?

    <p>False</p> Signup and view all the answers

    What will be the output of the given abstract class program?

    <p>FIRST, THIRD, SECOND</p> Signup and view all the answers

    What will be the output of the program using class X and its subclass Y?

    <p>ONE, TWO, THREE</p> Signup and view all the answers

    For every interface written in a Java file, a .class file will be generated after compilation.

    <p>True</p> Signup and view all the answers

    What is the error in the code declaring a private field in an interface?

    <p>Illegal modifier for field i. Only public, static and final are allowed.</p> Signup and view all the answers

    What will be the output of the code using interfaces and classes?

    <p>My Method</p> Signup and view all the answers

    Can a class implement more than one interface?

    <p>True</p> Signup and view all the answers

    What is causing the compile-time error in the implementation of interface X?

    <p>Interface methods must be implemented as public.</p> Signup and view all the answers

    Does the code compile successfully when trying to change the value of the interface field?

    <p>No, because interface fields are static and final by default.</p> Signup and view all the answers

    Study Notes

    Abstract Classes

    • An abstract class can be declared without any abstract methods; it can still provide complete method implementations.
    • A compilation error occurs if an abstract method contains a body; abstract methods must be declared without implementation.

    Instantiation and Implementation

    • Only non-abstract classes can be instantiated; thus, Class B (subclass of abstract Class A) is instantiable.
    • When extending an abstract class, the subclass must implement all inherited abstract methods or also be declared abstract.

    Between Abstract Classes and Interfaces

    • Abstract classes can contain constructors; they are not limited in the number of constructors they can define.
    • Abstract methods in an abstract class cannot be private; they must be accessible for implementation by subclasses.

    Interfaces

    • Protected methods cannot be declared in interfaces; only public methods are permitted.
    • An interface generates a separate .class file after the compilation of each interface in Java.

    Method Implementations

    • Interface methods must always be declared public; reducing visibility during implementation results in a compilation error.

    Field Modifiers

    • Interface fields are inherently public, static, and final, meaning their values cannot be modified after initialization.
    • Declaring a private field in an interface is prohibited due to legal modifier restrictions.

    Example Code Behavior

    • In the provided abstract class demonstration, calling methods results in the output: "FIRST", "THIRD", "SECOND" based on method chaining.
    • In another example, constructing an instance of Class Y from abstract Class X outputs "ONE", "TWO", and executing the abstract method outputs "THREE".

    General Knowledge

    • A class can implement multiple interfaces, promoting polymorphism and code reusability.
    • Ensuring method signatures maintain public visibility when implementing interface methods is crucial to avoid compilation errors.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Test your knowledge about abstract classes and interfaces with these flashcards. Explore concepts like abstract methods, class declarations, and their implications in programming. Perfect for students learning Java or object-oriented programming.

    More Quizzes Like This

    Use Quizgecko on...
    Browser
    Browser