Podcast
Questions and Answers
Which of the following class definitions defines a legal abstract class?
Which of the following class definitions defines a legal abstract class?
Which of the following declares an abstract method in an abstract Java class?
Which of the following declares an abstract method in an abstract Java class?
Which of the following statements regarding abstract methods are true?
Which of the following statements regarding abstract methods are true?
Which of the following statements regarding abstract methods are true?
Which of the following statements regarding abstract methods are true?
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?
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?
Signup and view all the answers
What is the output of running the class Test?
What is the output of running the class Test?
Signup and view all the answers
What occurs during the casting of numberRef to Double?
What occurs during the casting of numberRef to Double?
Signup and view all the answers
What happens during this assignment: Number[] numberArray = new Integer; numberArray = new Double(1.5);?
What happens during this assignment: Number[] numberArray = new Integer; numberArray = new Double(1.5);?
Signup and view all the answers
What is the reason for the compile error in the code provided?
What is the reason for the compile error in the code provided?
Signup and view all the answers
Why does the program have a compile error related to member access and casting?
Why does the program have a compile error related to member access and casting?
Signup and view all the answers
Which of the following statements are correct?
Which of the following statements are correct?
Signup and view all the answers
What is the compile error related to java.util.Calendar?
What is the compile error related to java.util.Calendar?
Signup and view all the answers
Assume Calendar calendar = new GregorianCalendar().__________ returns the month of the year.
Assume Calendar calendar = new GregorianCalendar().__________ returns the month of the year.
Signup and view all the answers
Assume Calendar calendar = new GregorianCalendar().__________ returns the week of the year.
Assume Calendar calendar = new GregorianCalendar().__________ returns the week of the year.
Signup and view all the answers
Assume Calendar calendar = new GregorianCalendar().__________ returns the number of days in a month.
Assume Calendar calendar = new GregorianCalendar().__________ returns the number of days in a month.
Signup and view all the answers
Which of the following is a correct interface?
Which of the following is a correct interface?
Signup and view all the answers
Which of the following is incorrect?
Which of the following is incorrect?
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
toDouble
. - Compile-time errors can arise when using methods that do not exist for the object type, as seen with
compareTo
forNumber
.
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 likeGregorianCalendar
. - 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.
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.