Abstract Classes and Interfaces Flashcards
17 Questions
100 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following class definitions defines a legal abstract class?

  • public class A { void method(); }
  • abstract class A { abstract void unfinished(); } (correct)
  • class A { abstract void unfinished(); }
  • public abstract class A { void method(); }
  • Which of the following declares an abstract method in an abstract Java class?

  • void method();
  • public void method();
  • public abstract void method(); (correct)
  • abstract void method();
  • Which of the following statements regarding abstract methods are true?

  • A subclass of a non-abstract superclass can be abstract. (correct)
  • An abstract class can be used as a data type. (correct)
  • An abstract class can be extended. (correct)
  • A subclass can override a concrete method in a superclass to declare it abstract. (correct)
  • Which of the following statements regarding abstract methods are true?

    <p>It is possible to declare an abstract class that contains no abstract methods.</p> Signup and view all the answers

    Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a no-arg constructor. Which of the following is correct?

    <p>B b = new B();</p> Signup and view all the answers

    What is the output of running the class Test?

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

    What occurs during the casting of numberRef to Double?

    <p>A runtime class casting exception occurs.</p> Signup and view all the answers

    What happens during this assignment: Number[] numberArray = new Integer; numberArray = new Double(1.5);?

    <p>Compile error.</p> Signup and view all the answers

    What is the reason for the compile error in the code provided?

    <p>x does not have the compareTo method.</p> Signup and view all the answers

    Why does the program have a compile error related to member access and casting?

    <p>Member access operator is executed before the casting operator.</p> Signup and view all the answers

    Which of the following statements are correct?

    <p>Double i = 4.5;</p> Signup and view all the answers

    What is the compile error related to java.util.Calendar?

    <p>The error occurs because java.util.Calendar is an abstract class.</p> Signup and view all the answers

    Assume Calendar calendar = new GregorianCalendar().__________ returns the month of the year.

    <p>calendar.get(Calendar.MONTH)</p> Signup and view all the answers

    Assume Calendar calendar = new GregorianCalendar().__________ returns the week of the year.

    <p>calendar.get(Calendar.WEEK_OF_YEAR)</p> Signup and view all the answers

    Assume Calendar calendar = new GregorianCalendar().__________ returns the number of days in a month.

    <p>calendar.getActualMaximum(Calendar.DAY_OF_MONTH)</p> Signup and view all the answers

    Which of the following is a correct interface?

    <p>interface A { void print();}</p> Signup and view all the answers

    Which of the following is incorrect?

    <p>Abstract classes can be instantiated.</p> Signup and view all the answers

    Study Notes

    Abstract Classes

    • An abstract class can define abstract methods that must be implemented by subclasses.
    • An abstract class may contain no abstract methods at all.
    • Constructors are allowed in abstract classes, and they can have private access.
    • Abstract classes can be extended by concrete classes or other abstract classes.
    • A subclass of a non-abstract superclass can itself be abstract.

    Abstract Methods

    • An abstract method cannot exist in a non-abstract class.
    • A subclass can override a concrete method from a superclass and declare it as abstract, requiring overriding in further subclasses.
    • Abstract methods must be declared with the keyword "abstract."

    Concrete Classes

    • A concrete subclass of an abstract class must implement all abstract methods in its superclass to be instantiated.
    • If a class has a valid no-argument constructor, it can be instantiated even if it inherits from an abstract superclass.

    Java Interfaces

    • Interfaces can contain method declarations but cannot have constructors.
    • A correct interface definition must declare one or more methods with no implementations.

    Java Exceptions

    • A runtime class casting exception occurs if an object is cast to an incompatible type, such as trying to cast from Number to Double.
    • Compile-time errors can arise when using methods that do not exist for the object type, as seen with compareTo for Number.

    Java Collections and Array Behavior

    • An array of a specific type (e.g., Integer[]) cannot accept an object of a different type (e.g., Double).
    • Attempting to assign an array of one type to another incompatible type will produce a runtime error.

    Calendar Class in Java

    • The Calendar class in Java is abstract, and cannot be instantiated directly; it requires a subclass like GregorianCalendar.
    • Methods in the Calendar class can retrieve specific date information, such as the month, week of the year, or maximum days in a month.

    Common Statements about Java Types

    • Type declarations can vary; for example, a statement like Double i = 4.5; is valid, indicating polymorphism within Java.
    • Incorrect type usages, such as declaring a variable of an abstract class type without initialization, can lead to compile-time errors.

    Compile Errors

    • Certain operations result in compile errors due to misunderstandings of method availability and the order of operations regarding casting and member access.

    Studying That Suits You

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

    Quiz Team

    Description

    Test your knowledge of abstract classes and interfaces in Java with these informative flashcards. Each card presents a scenario or definition to help reinforce your understanding of these fundamental concepts in object-oriented programming.

    More Like This

    Java OOPS (medium)
    30 questions

    Java OOPS (medium)

    AwedExuberance avatar
    AwedExuberance
    Java's Approach to Multiple Inheritance
    5 questions
    Use Quizgecko on...
    Browser
    Browser