Questions and Answers
What is an abstract method in an abstract class?
An abstract method cannot be contained in a nonabstract class. If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract.
An object can be created from an abstract class.
False
Can an abstract class contain no abstract methods?
Yes, it is possible to define an abstract class that contains no abstract methods.
A subclass can be abstract even if its superclass is concrete.
Signup and view all the answers
What happens when a subclass overrides a concrete method to be abstract?
Signup and view all the answers
How can an abstract class be used in Java?
Signup and view all the answers
What does java.util.Calendar represent?
Signup and view all the answers
How do you create a new GregorianCalendar instance?
Signup and view all the answers
What does the get(int field) method do in the Calendar class?
Signup and view all the answers
What is an interface in Java?
Signup and view all the answers
Why is an interface useful?
Signup and view all the answers
Define the structure of an interface in Java.
Signup and view all the answers
You cannot create an instance from an interface using the new operator.
Signup and view all the answers
How can the Edible interface be used?
Signup and view all the answers
What modifiers are omitted in interfaces?
Signup and view all the answers
Study Notes
Abstract Methods and Classes
- An abstract method cannot exist in a nonabstract class; a subclass of an abstract class must implement all abstract methods unless it is defined as abstract itself.
- Abstract classes cannot be instantiated directly with the new operator, but they can have constructors, which are called by their subclasses.
- It is possible to define an abstract class without any abstract methods; however, instances of such classes cannot be created with the new operator.
Concrete and Abstract Relationships
- A subclass may be abstract even if its superclass is concrete; for example, GeometricObject can be an abstract subclass of the concrete Object class.
- A concrete method can be overridden to become abstract in a subclass if its implementation becomes invalid, necessitating the subclass to be defined as abstract.
Using Abstract Classes
- Abstract classes can be used as data types even though instances cannot be created with them; for instance, an array can be declared with an abstract class type.
Calendar and Date Management
- The java.util.Calendar class is an abstract class for extracting detailed date and time information and can have subclasses like GregorianCalendar.
- java.util.GregorianCalendar supports the Gregorian calendar system and can be initialized with the current time or specific date values (note: month is 0-based).
Calendar Class Methods
- The get(int field) method in the Calendar class extracts date and time information, with fields being predefined constants.
Interfaces
- An interface is a class-like construct that contains only constants and abstract methods, used to specify common behaviors across objects.
- Unlike abstract classes, interfaces focus on behavior specification, allowing for comparisons and functional definitions like edible and cloneable.
Interface Structure
- Interfaces are defined with constant declarations and abstract method signatures.
- A typical interface declaration follows the structure:
public interface InterfaceName { // constants and method signatures }
Interface Characteristics
- Interfaces are treated as separate classes in Java, compiled into their own bytecode files.
- Similar to abstract classes, interfaces cannot have instances created via the new operator, but they can be utilized for variables and type casting.
Implementing Interfaces
- Classes can implement interfaces to indicate a shared characteristic, such as being edible, using the implements keyword.
- Example: Classes like Chicken and Fruit can implement the Edible interface to denote their edibility.
Modifiers in Interfaces
- All fields in an interface are implicitly public, final, and static; methods are implicitly public and abstract, requiring no explicit modifiers to be stated.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge with these flashcards from Java Chapter 13. Explore key concepts such as abstract methods and their roles in abstract classes. Ideal for students looking to reinforce their understanding of object-oriented programming in Java.