Chapter15_Abstract Classes and Interfaces

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the root class for all classes?

"Object" Class

What are the two thing that can be defined inside an interface?

1) constants 2) abstract methods

What is the use of the Cloneable interface and how does it work?

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. Meaning ==> It can create copies of objects.

What are the differences between the variables defined inside an abstract class vs an interface?

Abstract class : No restrictions Interface : Must be public static final - constants

What are the differences between the constructors defined inside an abstract class vs an interface?

Abstract class: Constructors are invoke by only the subclasses through constructor chaining. Interface: No constructors. Both abstract classes and interfaces cannot be instantiated through the "new" operator

What are the differences between the methods defined inside an abstract class vs an interface?

Abstract class: No restrictions Interface: all methods must be public abstract instance methods. (public abstract) modifier is default and thus,is not compulsory to declare.

What are abstract methods?

Abstract methods are methods that are defined in abstract classes or interfaces without implementation. ------------------------------------------------------ Eg: public abstract double getArea(); => Abstract Class double getArea() => Interface ------------------------------------------------------

What is the syntax to define a class the implements an interface?

Add the modifier "implements" to the class header ------------------------------------------------------ Eg; public class Fruit implements Edible { ........ } ------------------------------------------------------

What is the syntax to define an interface?

Add the modifier "interface" to class header in place of "class" ------------------------------------------------------ Eg: public interface Edible{ .... } ------------------------------------------------------

What is an interface?

An interface is a class-like construct that contains only constants and abstract methods

Why are interfaces preferred over abstract classes?

Because an interface can define a common supertype of unrelated cases. Making it more flexible than abstract classes which require strong is-a relationships.

How can an interface inherits other interfaces?

By using the keyword, "extends" ------------------------------------------------------ Eg: public interface NewInterface extends Interface1,.....InterfaceN { ............ ............ } ------------------------------------------------------

What are the abstract methods declared inside the Cloneable interface?

Cloneable interface is a mark/empty interface. Nothing is defined inside of it.

What should the programmer do if a subclass of an abstract superclass does not implement all the abstract methods?

Define the subclass as abstract. *In an non-abstract subclass extended from an abstract superclass, ALL abstract methods HAVE to be implemented.

True or False. A class containing abstract methods NEED NOT be abstract.

False

True or False. An abstract class can be used to create objects.

False

True or False. An abstract method can be contained in a non-abstract class.

False

True or False. You can instantiate an interface using the 'new' operator.

False

True or False. An interface can extend both interfaces and classes.

False. An interface can extend other interfaces but not classes.

Where are the abstract methods implemented?

In the concrete subclasses that extends the abstract class or implements the interfaces.

What does it mean when it says that the Cloneable interface is a marker interface?

It means that the Cloneable interface is empty with no constants or abstract methods inside of it.

Can an abstract class be instantiated using the new operator?

No

Suppose A is an abstract class. Can you create an instance using new A( )?

No

Can you define instance variables in an interface?

No. Only static final variables can be defined by default, in an interface.

How do you decide whether to use an interface or a class?

Strong is-a relationship that describes a parent-child relationship ==> Classes Weak is-a relationship/ is-kind-of relationship ==> Interfaces

What is the root interface for all interfaces?

There is no single root for interfaces.

What are the constructors inside an abstract class used for?

These are invoked by the subclasses during the process of instantiation of the subclasses.

How are the names of abstract classes and abstract methods represented in UML graphic notation?

They are italicized.

What are the restrictions of the methods in an interface?

They must be public abstract instance methods. ------------------------------------------------------ Eg: public abstract double getArea(); ------------------------------------------------------

True or False. A class can extends A SINGLE superclass and MULTIPLE interfaces.

True

True or False. A subclass can be abstract even if the superclass is concrete.

True

True or False. All abstract methods are nonstatic.

True

True or False. A subclass can override a method from a superclass to make it abstract.

True.

True or False. You can use an interface as a data type for a reference variable.

True.

How is the "implements" displayed in the UML notation?

Using dotted arrow line, origination from the subclass to the superclass/interface.

How are abstract classes denoted?

Using the "abstract" modifer in the class header. ------------------------------------------------------ Eg: public abstract class GeometryObject { ......... ........ } ------------------------------------------------------

How are abstract methods denoted?

Using the "abstract" modifer in the method header. ------------------------------------------------------ Eg: public abstract class GeometryObject { ......... ........ public abstract double getArea(); } ------------------------------------------------------

Class design should ensure that a superclass contains common features of its subclasses. Sometimes, a supereclass becomes so general/abstract that it cannot have any specific instances. Such a class is referred to as an abstract class.

What is an abstract class?

Can a programmer create an abstract class without any abstract methods?

Yes

Suppose A is an abstract class. Can you declare a reference variable x, with type A like this ? A x;

Yes

Can an abstract class be used as a data type?

Yes. ------------------------------------------------------ Eg: Shape [ ] objects = new Shape [10]; objects[0] = new Circle(); ------------------------------------------------------

Class names are nouns. Interface names may be a_______ or n_________.

adjectives nouns

What are the default modifiers for data fields and methods in an interface?

data fields ==> public static final methods ==> abstract ------------------------------------------------------ Thus 'T1' and 'T2' are equivalent. public interface T1{ public static final int K =1; public abstract void p( ); } ------------------------------------------------------ public interface T2{ int K = 1; void p( ); } ------------------------------------------------------

A class can implement ___(i)______ interface(s) and extend ____(ii)____ superclass(es).

i) multiple ii) at most one

Java allows ___(i)___ inheritance for classes but allows ___(ii)___ inheritance for interfaces.

i) single ii) multiple ------------------------------------------------------ Eg: public class NewClass extends BaseClass implements Interface1,.........,InterfaceN { .............. } ------------------------------------------------------

Click to see how to clone/make a copy of an array.

int [ ] list1 = {1 , 2}; int [ ] list2 = list1.clone( ); list1 [0] = 7; list2 [1] =8 ; System.out.println ("List 1 is "+ list1[0] + "," + list1[1]); System.out.println ("List 2 is "+list2[0] +"," + list2[1]); ------------------------------------------------------ Output : List 1 is 7,2 List 2 is 1,8

What is the relationship between a subclass and interface called?

interface inheritance / inheritance

What is an empty interface referred to as?

marker interface -- does not contain any constants or abstract methods

What modifier do the constructors in the abstract classes use and why?

modifier ==> protected reason ==> The constructors are only used by subclasses during the process of instantiation.


संबंधित स्टडी सेट्स

Biology Long Answer Exam Questions

View Set

16-3 Guided Reading Activity The civil War

View Set