Questions and Answers
All classes share a single root, the Object class, but there is no single root for interfaces.
True
In _____ class, variables and methods have no restrictions.
abstract
In Interface, all variables must be '' and all methods must be '__ methods'.
public static final, public abstract
Java allows only single _______ for class extension but allows multiple extensions for ______.
Signup and view all the answers
An interface can inherit other interfaces using the ______ keyword. Such an interface is called a ______.
Signup and view all the answers
An interface can extend other interfaces but not ______. A class can extend its ______ and implement multiple ______.
Signup and view all the answers
What defines a type in Java and allows casting to its subclass?
Signup and view all the answers
In naming convention, class names are ______. Interface names may be ______ or nouns.
Signup and view all the answers
What can be used to define common behavior for classes (including unrelated classes)?
Signup and view all the answers
What does an interface define for classes (including unrelated classes)?
Signup and view all the answers
An abstract class can be used to create objects.
Signup and view all the answers
An abstract class can contain abstract what?
Signup and view all the answers
What is an abstract class referred to when it cannot be used to create any specific instances?
Signup and view all the answers
An abstract method is defined without implementation.
Signup and view all the answers
A class that contains abstract methods must be defined as what?
Signup and view all the answers
The constructor in the abstract class is defined as what?
Signup and view all the answers
An abstract method can be contained in a nonabstract class.
Signup and view all the answers
If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined as what?
Signup and view all the answers
In a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented.
Signup and view all the answers
Abstract methods are nonstatic.
Signup and view all the answers
Abstract methods are static.
Signup and view all the answers
An abstract class cannot be instantiated using what operator?
Signup and view all the answers
An abstract class cannot be instantiated using the new operator, so you can't define its constructors.
Signup and view all the answers
It is possible to define an abstract class that doesn't contain any abstract methods.
Signup and view all the answers
What cannot be created of an abstract class using the new operator?
Signup and view all the answers
A subclass can override a method from its superclass to define it as abstract. In this case, the subclass must be defined as abstract.
Signup and view all the answers
A subclass can be abstract even if its superclass is concrete.
Signup and view all the answers
An abstract class can be used as what?
Signup and view all the answers
Is the following class a legal abstract class: class A { abstract void unfinished() { }}
Signup and view all the answers
Is the following class a legal abstract class: public class abstract A { abstract void unfinished(); }
Signup and view all the answers
Is the following class a legal abstract class: class abstract A { abstract void unfinished(); }
Signup and view all the answers
Is the following class a legal abstract class: abstract class A { protected void unfinished(); }
Signup and view all the answers
Is the following class a legal abstract class: abstract class A { abstract void unfinished(); }
Signup and view all the answers
Is the following class a legal abstract class: abstract class A { abstract int unfinished(); }
Signup and view all the answers
An abstract class can be used just like a nonabstract class except that you cannot use the new operator to create an instance from the abstract class.
Signup and view all the answers
An abstract class can be extended.
Signup and view all the answers
A subclass of a nonabstract superclass cannot be abstract.
Signup and view all the answers
A subclass cannot override a concrete method in a superclass to define it as abstract.
Signup and view all the answers
An abstract method must be nonstatic.
Signup and view all the answers
What is an abstract superclass for numeric wrapper classes, BigInteger, and BigDecimal?
Signup and view all the answers
What kind of error does the following code cause? Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef;
Signup and view all the answers
What kind of error does the following code cause at runtime? Number[] numberArray = new Integer; numberArray = new Double(1.5);
Signup and view all the answers
What is the output of the following code: public class test1 { public static void main(String[] args) throws FileNotFoundException { Number x = 3; System.out.print(x.intValue() + ', '); System.out.println(x.doubleValue()); }}
Signup and view all the answers
What kind of error does the following program cause? public class Test { public static void main(String[] args) { Number x = new Integer(3); System.out.println(x.intValue()); System.out.println(x.compareTo(new Integer(4))); } }
Signup and view all the answers
In which phase is the member access operator (.) executed relative to the casting operator?
Signup and view all the answers
What kind of subclass is GregorianCalendar of the abstract class Calendar?
Signup and view all the answers
Can you create a Calendar object using the Calendar class?
Signup and view all the answers
Calendar is an abstract class, so we can't create a Calendar object using the Calendar class.
Signup and view all the answers
Which method in the Calendar class is abstract?
Signup and view all the answers
True or False: You can create a Calendar object for the current time by using the GregorianCalendar class's no-arg constructor.
Signup and view all the answers
For a Calendar object c, how do you get its year?
Signup and view all the answers
For a Calendar object c, how do you get its month?
Signup and view all the answers
For a Calendar object c, how do you get its date?
Signup and view all the answers
For a Calendar object c, how do you get its hour?
Signup and view all the answers
For a Calendar object c, how do you get its day number within the week, with 1 for Sunday?
Signup and view all the answers
For a Calendar object c, how do you get its day number in the year, with 1 for the first day of the year?
Signup and view all the answers
For a Calendar object c, how do you get its week number within the month, with 1 for the first week?
Signup and view all the answers
For a Calendar object c, how do you get its week number within the year, with 1 for the first week?
Signup and view all the answers
An interface contains only what types of methods?
Signup and view all the answers
An interface is treated like a special class 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
What is the relationship between the class and the interface called?
Signup and view all the answers
What is the intent of an interface?
Signup and view all the answers
In UML, the interface name and the method names are what?
Signup and view all the answers
All fields are what in an interface?
Signup and view all the answers
All methods are what in an interface?
Signup and view all the answers
Since all data fields are public static final and all methods are public abstract in an interface, Java allows these modifiers to be omitted.
Signup and view all the answers
Can you create an instance using new A() if A is an interface?
Signup and view all the answers
Can you declare a reference variable x with type A like this? A x;
Signup and view all the answers
The following is not a correct interface: interface A { void print() { }} because the print() method has a?
Signup and view all the answers
The following is not a correct interface: abstract interface A { abstract void print() { }} because the interface cannot have the?
Signup and view all the answers
Is the following a correct interface? abstract interface A { print(); }
Signup and view all the answers
Is the following a correct interface? interface A { void print(); }
Signup and view all the answers
Is the following a correct interface? interface A { default void print() { }}
Signup and view all the answers
Is the following a correct interface? interface A { static int get() { return 0; }}
Signup and view all the answers
The code causes an error because all methods defined in an interface are what?
Signup and view all the answers
When a class implements the interface, the method must be declared as what?
Signup and view all the answers
The interface defines the 'compareTo' method for what?
Signup and view all the answers
In Comparable interface, the 'compareTo' method determines the order of this object with the specified object o and returns what?
Signup and view all the answers
What is displayed by the code: System.out.println(new Integer(3).compareTo(new Integer(5))); ?
Signup and view all the answers
What is displayed by the code: System.out.println("ABC".compareTo("ABE")); ?
Signup and view all the answers
What is displayed by the code: java.util.Date date1 = new java.util.Date(2013, 1, 1); java.util.Date date2 = new java.util.Date(2012, 1, 1); System.out.println(date1.compareTo(date2)); ?
Signup and view all the answers
If a class implements Comparable, the object of the class can invoke the compareTo method.
Signup and view all the answers
Which of the following is the correct method header for the compareTo method in the String class?
Signup and view all the answers
Can the following code be compiled? Integer n1 = new Integer(3); Object n2 = new Integer(4); System.out.println(n1.compareTo(n2));
Signup and view all the answers
By implementing the Comparable interface, the object of the class can be what?
Signup and view all the answers
Can the following code be executed? public class Test { public static void main(String[] args) { Person[] persons = {new Person(3), new Person(4), new Person(1)}; java.util.Arrays.sort(persons); }} class Person { private int id; Person(int id) { this.id = id; }}
Signup and view all the answers
The interface specifies that an object can be what?
Signup and view all the answers
The Cloneable interface is what kind of interface?
Signup and view all the answers
To define a custom class that implements the Cloneable interface, the class must override what method?
Signup and view all the answers
A class that implements the Cloneable interface is marked cloneable, and its objects can be cloned using the clone() method defined in the Object class.
Signup and view all the answers
What gets copied if the field is of a primitive type in the clone method?
Signup and view all the answers
What gets copied if the field is of an object type in the clone method?
Signup and view all the answers
A class invokes super.clone() when implementing the clone() method if the class does not implement java.lang.Cloneable; however, it does not actually clone the object.
Signup and view all the answers
Show the output of the following code: java.util.Date date = new java.util.Date(); java.util.Date date1 = date; java.util.Date date2 = (java.util.Date)(date.clone()); System.out.print(date == date1 + ', '); System.out.print(date == date2 + ', '); System.out.println(date.equals(date2));
Signup and view all the answers
Show the output of the following code: ArrayList list = new ArrayList(); list.add("New York"); ArrayList list1 = list; ArrayList list2 = (ArrayList)(list.clone()); list.add("Atlanta"); System.out.print(list == list1 + ', '); System.out.print(list == list2 + ', '); System.out.println("list is " + list);
Signup and view all the answers
Show the output of the following code: ArrayList list = new ArrayList(); list.add("New York"); ArrayList list1 = list; ArrayList list2 = (ArrayList)(list.clone()); list.add("Atlanta"); System.out.println("list1 is " + list1 + ", "); System.out.println(list2.get(0) + ", "); System.out.println(list2.size());
Signup and view all the answers
What kind of error occurs in the following code at runtime? public class Test { public static void main(String[] args) { GeometricObject x = new Circle(3); GeometricObject y = x.clone(); System.out.println(x == y); } }
Signup and view all the answers
What kind of error occurs in the following code at runtime? public class Test5 { public static void main(String[] args) throws CloneNotSupportedException { Test5 x = new Test5(); GeometricObject y = (GeometricObject)x.clone(); } }
Signup and view all the answers
A class can implement multiple interfaces, but it can only extend one superclass.
Signup and view all the answers
A class can extend multiple superclasses, but it can only implement one interface.
Signup and view all the answers
Study Notes
Abstract Classes
- A superclass defines common behavior for related subclasses within an inheritance hierarchy.
- Abstract classes cannot be instantiated directly using the
new
operator. - An abstract class may contain abstract methods, which must be implemented in concrete subclasses.
- A class with abstract methods must itself be declared as abstract.
- An abstract class can only be used as a data type but not instantiated.
- Abstract methods do not have implementations and are defined in abstract classes only.
Interfaces
- An interface specifies common behavior for related and unrelated classes.
- Contains only constants (public static final) and abstract methods (public abstract).
- Interfaces cannot be instantiated, nor can they have methods with a body.
- The interface concept allows multiple inheritance in Java, enabling a class to implement multiple interfaces.
Constructors
- Constructors in abstract classes are typically protected to restrict their access to subclasses.
- Although an abstract class can't be instantiated, its constructors can be defined and utilized by subclasses.
Method Implementation
- Subclasses must implement all abstract methods from their abstract superclasses, unless they also declare as abstract themselves.
- All methods defined in an interface must be declared as public to ensure visibility.
Comparable Interface
- The
Comparable
interface defines thecompareTo
method, which determines the order of objects. - The
compareTo
method returns a negative integer, zero, or a positive integer based on object comparison. - Classes implementing
Comparable
must define thecompareTo
method to be compatible with sorting algorithms.
Cloning
- The
Cloneable
interface indicates that a class can be cloned, requiring an override of theclone()
method in the Object class. - The
clone()
method creates a copy of the original object, copying primitive values directly and object references for object types.
Single Inheritance vs. Multiple Interfaces
- Java supports single inheritance for classes (one superclass) but allows multiple interfaces to be implemented.
- An interface can extend other interfaces to form subinterfaces, but it cannot extend classes.
Key Java Concepts
- Abstract classes support method overriding, while classes cannot change a concrete method to abstract.
- All classes extend a single root class,
Object
, while interfaces do not share a common ancestor. - An interface can be used as a data type variable in code, similar to a superclass in class hierarchies.
Naming Conventions
- Class names are typically nouns, while interface names are often adjectives or abstract concepts to denote their behavior.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge on Abstract Classes and Interfaces in Java with these flashcards. This quiz covers essential concepts such as superclass behavior, interfaces, and the differences between abstract classes and regular classes. Perfect for anyone looking to deepen their understanding of object-oriented programming in Java.