CS - 2013: 13.Java: Abstract Classes and Interfaces
101 Questions
100 Views

CS - 2013: 13.Java: Abstract Classes and Interfaces

Created by
@SpellboundEllipsis

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 ______.

<p>inheritance, interfaces</p> Signup and view all the answers

An interface can inherit other interfaces using the ______ keyword. Such an interface is called a ______.

<p>extends, subinterface</p> Signup and view all the answers

An interface can extend other interfaces but not ______. A class can extend its ______ and implement multiple ______.

<p>classes, superclass, interfaces</p> Signup and view all the answers

What defines a type in Java and allows casting to its subclass?

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

In naming convention, class names are ______. Interface names may be ______ or nouns.

<p>nouns, adjectives</p> Signup and view all the answers

What can be used to define common behavior for classes (including unrelated classes)?

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

What does an interface define for classes (including unrelated classes)?

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

An abstract class can be used to create objects.

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

An abstract class can contain abstract what?

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

What is an abstract class referred to when it cannot be used to create any specific instances?

<p>abstract class</p> Signup and view all the answers

An abstract method is defined without implementation.

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

A class that contains abstract methods must be defined as what?

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

The constructor in the abstract class is defined as what?

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

An abstract method can be contained in a nonabstract class.

<p>False</p> 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?

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

In a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented.

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

Abstract methods are nonstatic.

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

Abstract methods are static.

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

An abstract class cannot be instantiated using what operator?

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

An abstract class cannot be instantiated using the new operator, so you can't define its constructors.

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

It is possible to define an abstract class that doesn't contain any abstract methods.

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

What cannot be created of an abstract class using the new operator?

<p>instances</p> 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.

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

A subclass can be abstract even if its superclass is concrete.

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

An abstract class can be used as what?

<p>data type</p> Signup and view all the answers

Is the following class a legal abstract class: class A { abstract void unfinished() { }}

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

Is the following class a legal abstract class: public class abstract A { abstract void unfinished(); }

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

Is the following class a legal abstract class: class abstract A { abstract void unfinished(); }

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

Is the following class a legal abstract class: abstract class A { protected void unfinished(); }

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

Is the following class a legal abstract class: abstract class A { abstract void unfinished(); }

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

Is the following class a legal abstract class: abstract class A { abstract int unfinished(); }

<p>False</p> 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.

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

An abstract class can be extended.

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

A subclass of a nonabstract superclass cannot be abstract.

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

A subclass cannot override a concrete method in a superclass to define it as abstract.

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

An abstract method must be nonstatic.

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

What is an abstract superclass for numeric wrapper classes, BigInteger, and BigDecimal?

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

What kind of error does the following code cause? Number numberRef = new Integer(0); Double doubleRef = (Double)numberRef;

<p>runtime error</p> 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);

<p>casting error</p> 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()); }}

<p>3, 3.0</p> 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))); } }

<p>syntax error</p> Signup and view all the answers

In which phase is the member access operator (.) executed relative to the casting operator?

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

What kind of subclass is GregorianCalendar of the abstract class Calendar?

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

Can you create a Calendar object using the Calendar class?

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

Calendar is an abstract class, so we can't create a Calendar object using the Calendar class.

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

Which method in the Calendar class is abstract?

<p>1</p> 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.

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

For a Calendar object c, how do you get its year?

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

For a Calendar object c, how do you get its month?

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

For a Calendar object c, how do you get its date?

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

For a Calendar object c, how do you get its hour?

<p>3</p> 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?

<p>3</p> 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?

<p>1</p> 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?

<p>3</p> 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?

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

An interface contains only what types of methods?

<p>constants, abstract</p> Signup and view all the answers

An interface is treated like a special class in Java.

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

You cannot create an instance from an interface using the new operator.

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

What is the relationship between the class and the interface called?

<p>interface inheritance</p> Signup and view all the answers

What is the intent of an interface?

<p>specify common behavior for objects of related classes or unrelated classes.</p> Signup and view all the answers

In UML, the interface name and the method names are what?

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

All fields are what in an interface?

<p>public static final</p> Signup and view all the answers

All methods are what in an interface?

<p>public abstract</p> 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.

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

Can you create an instance using new A() if A is an interface?

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

Can you declare a reference variable x with type A like this? A x;

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

The following is not a correct interface: interface A { void print() { }} because the print() method has a?

<p>body</p> 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?

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

Is the following a correct interface? abstract interface A { print(); }

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

Is the following a correct interface? interface A { void print(); }

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

Is the following a correct interface? interface A { default void print() { }}

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

Is the following a correct interface? interface A { static int get() { return 0; }}

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

The code causes an error because all methods defined in an interface are what?

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

When a class implements the interface, the method must be declared as what?

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

The interface defines the 'compareTo' method for what?

<p>comparing objects</p> 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?

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

What is displayed by the code: System.out.println(new Integer(3).compareTo(new Integer(5))); ?

<p>-1</p> Signup and view all the answers

What is displayed by the code: System.out.println("ABC".compareTo("ABE")); ?

<p>negative value</p> 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)); ?

<p>positive value</p> Signup and view all the answers

If a class implements Comparable, the object of the class can invoke the compareTo method.

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

Which of the following is the correct method header for the compareTo method in the String class?

<p>1</p> 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));

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

By implementing the Comparable interface, the object of the class can be what?

<p>passed</p> 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; }}

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

The interface specifies that an object can be what?

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

The Cloneable interface is what kind of interface?

<p>marker interface</p> Signup and view all the answers

To define a custom class that implements the Cloneable interface, the class must override what method?

<p>clone()</p> 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.

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

What gets copied if the field is of a primitive type in the clone method?

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

What gets copied if the field is of an object type in the clone method?

<p>reference</p> 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.

<p>True</p> 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));

<p>False</p> 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);

<p>False</p> 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());

<p>False</p> 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); } }

<p>compile error</p> 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(); } }

<p>no error</p> Signup and view all the answers

A class can implement multiple interfaces, but it can only extend one superclass.

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

A class can extend multiple superclasses, but it can only implement one interface.

<p>False</p> 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 the compareTo 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 the compareTo method to be compatible with sorting algorithms.

Cloning

  • The Cloneable interface indicates that a class can be cloned, requiring an override of the clone() 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.

Quiz Team

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.

Use Quizgecko on...
Browser
Browser