interfaces and inner classes Flashcards
what is an interface
extreme case of an abstract class not a class a type that can be satisfied by any class that implements that interface
what does an interface do
specifies a set of methods that any class that implements the interface must have
what does an abstract class have
- contains method headings and constant definitions only
how does java approximate multiple inheritance
through interfaces
how do methods with interface parameter types work
that parameter will accept as an argument any class that implements the interface
how must a concrete class implement an interface
implements InterfaceName
must implement all the method headings listed in the definitions of the interfaces
abstract class implementing interfaces
may implement one or more interfaces
Any method headings given in the interface that are not given definitions are made into abstract methods
A concrete class must give definitions for all the method headings given in the abstract class and the interface
derived interfaces
extends BaseInterface
A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface
interface pitfall
runtime and compiler systems dont check that the interface body is consistent with the intended meaning
If the method body does not satisfy the specified semantics, then software written for classes that implement the interface may not work correctly
what rules must total ordering satisfy?
irreflexivity - for no object o does o come before o
Trichotomy - for any objects o1 and o2 only of of the following are true: o1 comes before o2, o1 comes after o2, o1 == o2
transivity - if o1 comes before o2 and o2 comes before o3 the o1 comes before o3
what inconsistencies happen when a class implements two interfaces
One type of inconsistency will occur if the interfaces have constants with the same name, but with different values
Another type of inconsistency will occur if the interfaces contain methods with the same name but different return types
when will the class definition using more than 1 interface be illegal?
implements two inconsistent interfaces
what is the serializable interface?
- no method headings or constants - completely empty
- used merely as a type tag that indicates to the system that it may implement file I/O in a particular way
what is the cloneable interface
no method heading or contained constants
used to indicate how the method clone (inherited from the Object class) should be used or redefined
when implementing Cloneable for primitive or immutable classes
just use the existing Object.clone
when implementing Cloneable for mutable classes
First invoke the clone method of the base class Object (or whatever the base class is) Then reset the values of any new instance variables whose types are mutable class types This is done by making copies of the instance variables by invoking their clone methods
what are inner classes
classes that are defined within other classes An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members -An inner class is local to the outer class definition -The name of an inner class may be reused for something else outside the outer class definition -If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class
advantages of inner classes
make outer classes more self contained since that are defined inside a class both of their methods have access to each other's private methods and instance variables - inner class as a helping class - if helping class the inner class should be made private
can inner and outer classes access each other’s private members?
in definition of the inner class method definitions:
legal to reference private instance variables of the outer class legal to invoke private method of the outer class
in definition of the outer class method definitions:
legal to reference a private instance variable of the inner class on the an object of the inner class - legal to invoke a non static method of the inner class as long as an object of the inner class is used as a calling object
true or false: within the definition of the inner or outer classes, the modifiers private and public are equivalent
true
naming files with multiple classes
Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more) .class files Such as ClassName.class and ClassName$InnerClassName.class
what is it that allows an inner class definition to reference an instance variable or invoke a method of the outer class
A normal inner class has a connection between its objects and the outer class object that created the inner class object
in which situations must an inner class be static?
- if an object of the inner class is created withing a static method of the outer class
- if the inner class must have static members
Since a static inner class has no connection to an object of the outer class, within an inner class method
- Instance variables of the outer class cannot be referenced
- Nonstatic methods of the outer class cannot be invoked
To invoke a static method or to name a static variable of a static inner class within the outer class, preface each with the name of the inner class and a dot
what if an inner class is named public
it can be used outside of the outer class as well
what must be done in the case of a non static inner class
it must be created using an object of the outer class
BankAccount account = new BankAccount(); BankAccount.Money amount = account.new Money("41.99");
The new object amount can now invoke methods from the inner class, but only from the inner class
what must be done in the case of a static inner class
OuterClass.InnerClass innerObject = new OuterClass.InnerClass();
Note that all of the following are acceptable
innerObject.nonstaticMethod();
innerObject.staticMethod();
OuterClass.InnerClass.staticMethod();
nesting classes
Given class A, which has public inner class B, which has public inner class C, then the following is valid: A aObject = new A(); A.B bObject = aObject.new B(); A.B.C cObject = bObject.new C();
what happens with inner classes and inheritance
Given an OuterClass that has an InnerClass -Any DerivedClass of OuterClass will automatically have InnerClass as an inner class -In this case, the DerivedClass cannot override the InnerClass An outer class can be a derived class An inner class can be a derived class also
what is an anonymous class
If an object is to be created, but there is no need to name the object's class, then an anonymous class definition can be used -The class definition is embedded inside the expression with the new operator
when are anonymous classes used
when they are to be assigned to a variable of another type The other type must be such that an object of the anonymous class is also an object of the other type The other type is usually a Java interface