CH 13 - Abstract Classes and Interfaces

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is an interface used for?

Defining common behavior for classes (including unrelated classes)

True or False: An abstract class can be instantiated using the new operator.

False!

True or False: An abstract method can be contained in a nonabstract class.

False!

True or False: An object can be created from an abstract class.

False!

True or false: An abstract class can be used to create objects

False!

True or false: You can use the new operator to create a new instance from an interface.

False!

Where are the constructors for abstract classes invoked?

In the constructors of its subclasses.

What is the difference between inheritance and aggregation?

Inheritance: is-a relationship Aggregation: has-a relationship

When do you use interfaces/abstract classes and normal classes?

Interfaces/abstract classes : weak is-a relationship Normal classes: strong is-a relationship

What is interface inheritance?

It is the relationship between the class and the interface. This is often referred to simply as inheritance.

If the GeometricObject class is abstract, is this code allowed? GeometricObject[ ] objects = new GeometricObject[10];

Yes! An abstract class can be used as a data type

What is the naming convention for classes and interfaces?

class names are nouns interface names may be adjectives or nouns

What is clarity in class design?

A clear contract that is easy to explain and understand.

What is an abstract method?

A method in a superclass with no implementation, requiring implementation from subclasses. Ex: getArea(); getPerimeter(); are overridden in Circle and Rectangle subclasses

What is a static method?

A method that can be called without creating an instance of a class. ex: setT2(2);

Why are interfaces more flexible than classes?

An interface can define a common supertype for unrelated classes.

How many classes can we extend?

Just one!

How many classes can we extend for interfaces?

Multiple!

What is cohesion?

A class should describe one entity.

What is an abstract class?

A super class so abstract that it cannot be used to create any specific instances. ex: GeometricObject class contains getArea(); and getPerimeter(); with no implementation because the same method won't work for different geometric objects.

What is a static variable?

A variable shared by the whole class.

What is the difference between an abstract class and an interface?

An interface specifies common behavior for objects of related or unrelated classes. ex: comparable edible cloneable

True or False: An interface has constructors like an abstract class

False! It has no constructors

True or false: The constructor in the abstract class is defined as public.

False! It is defined as protected because it is only accessed by its subclasses.

True or false: An abstract class must contain abstract methods.

False! This kind of abstract class is used as a base class for defining subclasses.

What is consistency in class design?

Following name conventions and being consistent with naming things. Consistently place a no-arg constructor for constructing a default instance.

What is going on in the following code: class Chicken extends Animal implements Edible{ //blahblah }

The Chicken is a subclass of Animal, and is denoted as Edible.

True or False: A class that contains abstract methods must be abstract.

True!

True or False: A class that contains abstract methods must be defined as abstract.

True!

True or False: Number is an abstract class

True!

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

True! ex: the Object class is concrete, but one of its subclasses GeometricObject may be abstract.

What is encapsulation in class design?

Use private modifier to hide data from clients getter and setter methods if needed

A class can implement multiple ___________ but it can only extend one ___________.

interfaces superclass

What kind of methods can an interface have?

public abstract instance methods public default public static

A variable that is shared by all the instances of a class should be declared as __________

static


Ensembles d'études connexes

7 Antibacterial Drugs That Disrupt the Bacterial Cell Wall

View Set

Flashcards from HTML and CSS lesson

View Set

Dilations ( assignment&quiz ) 100% and 70%

View Set

Lifespan G&D Final Review Unit 4

View Set

2.3 Given a scenario, configure and deploy common Ethernet switching features.

View Set