Chapter 10: Interfaces

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the difference between an interface and class?

1. An interface has no instance variables 2. All methods are abstract (they have the signature, name, parameters, and return type but have no implementation 3. All methods are automatically public 4. Has no constructor, you cannot make an interface object

What is an inner class? What are they used for?

A class declared within a class. They are used as utility classes and are not seen outside the scope of a class because they play a small part.

How can a class use an interface?

A class declares an interface type by implementing the interface type as such: public class BankAccount implements Measurable *** the word "implements" is reserved in Java

What is a callback method?

A method that specifies code to be executed at a later time.

What is a Java interface type?

A set of declared methods that does not provide their implementations Ex: public interface Measurable { double getMeasure(); }

What is an event source and an event listener?

An event source if an event that is caused by the user, a click, movement, etc. An event listener is an object that contains many methods for how react with certain events.

What is an anonymous object or anonymous class?

An object or class that does not a reference name. They are usually anonymous if they are only used once.

What is the clone method?

If you have two different references pointing to the same object, it can make an identical object for one of the references BankAccount clonedAccount = (BankAccount) account.clone() The clone method returns an object type and must be casted. If the object passed to clone is pointing to the same object as another reference, then the method makes a copy of the reference and not a new object.

If an object is of an interface type, how does it know which method to perform?

It knows because of dynamic method lookup, the class of the actual object will be looked at first, if it doesn't have it it will ascend to higher classes until it find a method.

What is the point of an interface of it has no implementation in its methods?

It specifies the behavior that a class should supply, even though the class supplies the implementation itself. Its used to process objects of different classes in a common way.

What is a mock object? What is it used for?

Like a certain object, but simpler. Mock objects are used for when you want to test a class before a program is finished.

If the methods of an interface are always public, will the methods of the class that implement the interface be public?

No, they will have the package access. You should always specify the access modifier of a method.

Why use an interface? What is the point?

So that similar methods of many different types of objects can share the same method. If a class implements an interface, the object is of the interface type (whatever the name of the interface is). Here's an example: Say you have a bunch of animal types, if you try to use the speak() method on an animal type that are different from each other, they won't work BUT, if you use the method from the interface and the objects have their own implementation of that method, it will work.

It can be good to use an event listener class as an inner class, why?

The event listener classes tend to be very simple and they can access the instance variables and methods of the classes that they are in. The instance variables to be accessed however, must be final or they must belong to the surrounding class.

What are all the variables in an interface automatically?

They are automatically public, static, and final.

Can you cast/convert a certain object type to and interface type?

Yes, if the object type implements the interface type. Ex: Country uruguay = new Country("Uruguay", 176220); Measurable meas = uruguay

If you have an object of an interface type, how can you change it back to another object type that is not of an interface type?

You can using casting, but you must be SURE that the object refers to that specific type. If you are wrong, a run-time error occurs, the compiler does not catch the mistake because it may be syntactically possible.


Conjuntos de estudio relacionados

MedSurg ch 29 practice questions

View Set

MedSurg HESI Preparation Questions

View Set

CHAPTER 6. Entrepreneurship and Starting a Small Business

View Set

ITN 200 FINAL QUESTIONS STUDY GUIDE

View Set

Customer Relationship Management

View Set

Strategic Management Quiz 6 Exam 1

View Set