Questions and Answers
An abstract method cannot be contained in what type of class?
nonabstract class
If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract.
True
In a nonabstract subclass extended from an abstract class, all abstract methods must be implemented.
True
An abstract class cannot be instantiated using which operator?
Signup and view all the answers
A class that contains abstract methods must be abstract.
Signup and view all the answers
In the case of abstract classes, can you create instances using the new operator?
Signup and view all the answers
A subclass can be abstract even if its superclass is concrete.
Signup and view all the answers
For example, the Object class is concrete, but its subclasses, such as what, may be abstract?
Signup and view all the answers
A subclass can override a method from its superclass to define it abstract.
Signup and view all the answers
You cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type.
Signup and view all the answers
What is the correct statement for creating an array whose elements are of GeometricObject type?
Signup and view all the answers
An instance of java.util.Date represents what?
Signup and view all the answers
Subclasses of Calendar can implement specific calendar systems.
Signup and view all the answers
For example, java.util.GregorianCalendar is supported in which package?
Signup and view all the answers
You can use new GregorianCalendar() to construct a default GregorianCalendar with the current time.
Signup and view all the answers
The fields defined in the Calendar class are public final.
Signup and view all the answers
What does an interface in Java contain?
Signup and view all the answers
All data fields in an interface are public final and all methods are public abstract.
Signup and view all the answers
An interface can be distinguished from a class by following syntax: public interface InterfaceName {}.
Signup and view all the answers
What is the method defined in the Edible interface for specifying how an object can be eaten?
Signup and view all the answers
In Java, a class can implement multiple interfaces.
Signup and view all the answers
An interface is treated like what in Java?
Signup and view all the answers
Variables of an interface type can reference any instance of a class that implements that interface.
Signup and view all the answers
You can use an interface as a data type and cast a variable of an interface type to what?
Signup and view all the answers
Abstract classes and interfaces can both be used to model common features.
Signup and view all the answers
In Java, what should a class use to hide its data from direct access from clients?
Signup and view all the answers
A class should present two contracts: one for the users and one for the extenders. True or False?
Signup and view all the answers
Match the following Java concepts with their definitions:
Signup and view all the answers
Study Notes
Abstract Classes and Methods
- An abstract method is exclusive to abstract classes; it cannot exist in nonabstract classes.
- A subclass must also be abstract if it fails to implement all inherited abstract methods.
- Nonabstract subclasses must implement all abstract methods from their abstract superclass, even if these methods remain unused.
- Abstract classes cannot be instantiated using the
new
operator, but their constructors can be defined and invoked in subclasses. - An abstract class may exist without abstract methods, but it still cannot be instantiated.
Class Instances and Relationships
- Abstract classes serve as base classes for subclasses, preventing direct instantiation.
- Subclasses can be declared abstract even when their superclasses are concrete.
- It's possible to define abstract subclasses with concrete superclasses, exemplified by the Object class and its subclasses like GeometricObject.
- Abstract methods in subclasses can override methods from superclasses when a superclass method becomes irrelevant, necessitating that the subclass be abstract.
Interfaces and Implementation
- An interface is a construct containing only constants and abstract methods, aimed at defining common behavior among objects.
- Interfaces allow classification, such as specifying whether an object is comparable or edible via appropriate methods.
- The interface syntax mandates public methods and constant declarations without implementation specifics.
- A class can implement multiple interfaces but may face errors if conflicting definitions arise.
Java Collections and Comparable Interface
- Java utilizes wrapper classes to provide operations on primitive types, where each wrapper class implements the Comparable interface, enabling comparison through the
compareTo
method. - The
java.util.Arrays.sort(array)
method necessitates elements implementing Comparable for sorting. - Marker interfaces signify classes possessing certain properties without defining constants or methods, such as Cloneable enabling cloning capabilities in Java classes.
Designing Reliable Classes
- Class design promotes reuse and flexibility, allowing incorporation in various contexts without imposing restrictions.
- Good practice entails a public no-argument constructor, as well as overriding essential methods like
equals
andtoString
. - Class fields should remain private, only accessed via public getter and setter methods to safeguard data integrity.
- Fields meant solely for internal class use should be kept private to avoid direct interactions with clients.
Class Structure and Responsibilities
- It is beneficial to separate responsibilities into distinct classes, ensuring clarity and manageability.
- User access should be controlled through explicit public methods for modifying private data, preventing undesired changes.
- A clear distinction between class contracts exists for users and extenders, emphasizing encapsulation and method visibility strategies.
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 methods in Java with these flashcards. Each card presents a crucial concept to help you understand the fundamentals of object-oriented programming in Java. Perfect for revision or quick learning!